github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/cmd/gomobile/gendex.go (about)

     1  // Copyright 2015 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ignore
     6  
     7  // Gendex generates a dex file used by Go apps created with gomobile.
     8  //
     9  // The dex is a thin extension of NativeActivity, providing access to
    10  // a few platform features (not the SDK UI) not easily accessible from
    11  // NDK headers. Long term these could be made part of the standard NDK,
    12  // however that would limit gomobile to working with newer versions of
    13  // the Android OS, so we do this while we wait.
    14  //
    15  // Requires ANDROID_HOME be set to the path of the Android SDK, and
    16  // javac must be on the PATH.
    17  package main
    18  
    19  import (
    20  	//"bytes"
    21  	//"encoding/base64"
    22  	"errors"
    23  	"flag"
    24  	"fmt"
    25  	//"go/format"
    26  	"io/ioutil"
    27  	"log"
    28  	"os"
    29  	"os/exec"
    30  	"path/filepath"
    31  )
    32  
    33  var outfile = flag.String("o", "", "result will be written file")
    34  
    35  var tmpdir string
    36  
    37  func main() {
    38  	flag.Parse()
    39  
    40  	var err error
    41  	tmpdir, err = ioutil.TempDir("", "gendex-")
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  
    46  	err = gendex()
    47  	//os.RemoveAll(tmpdir)
    48  	if err != nil {
    49  		log.Fatal(err)
    50  	}
    51  }
    52  
    53  func gendex() error {
    54  
    55  	androidHome := os.Getenv("ANDROID_HOME")
    56  	if androidHome == "" {
    57  		return errors.New("ANDROID_HOME not set")
    58  	}
    59  
    60  	/*cmd1 := exec.Command(
    61  		"cp",
    62  		"-rf",
    63  		"/home/z/go-projects/src/github.com/democratic-coin/dcoin-go/xwalker.jar/",
    64  		tmpdir+"/work/",
    65  	)
    66  	fmt.Println(cmd1.Args)
    67  	
    68  	if out, err := cmd1.CombinedOutput(); err != nil {
    69  		os.Stderr.Write(out)
    70  		return err
    71  	}*/
    72  
    73  	if err := os.MkdirAll(tmpdir+"/work/org/golang/app", 0775); err != nil {
    74  		return err
    75  	}
    76  	javaFiles, err := filepath.Glob("/home/z/go-projects/src/github.com/democratic-coin/dcoin-go/*.java")
    77  	if err != nil {
    78  		return err
    79  	}
    80  	if len(javaFiles) == 0 {
    81  		return errors.New("could not find /home/z/go-projects/src/github.com/democratic-coin/dcoin-go/*.java files")
    82  	}
    83  	platform, err := findLast(androidHome + "/platforms")
    84  	if err != nil {
    85  		return err
    86  	}
    87  	cmd := exec.Command(
    88  		"javac",
    89  		"-source", "1.7",
    90  		"-target", "1.7",
    91  		"-bootclasspath", platform+"/android.jar:/home/z/go-projects/src/github.com/democratic-coin/dcoin-go/xwalk_core_library_java.jar",
    92  		"-classpath", "/home/z/go-projects/src/github.com/democratic-coin/dcoin-go/xwalk_core_library_java.jar:/home/z/go-projects/src/github.com/democratic-coin/dcoin-go/R.jar:"+androidHome+"/extras/android/m2repository/com/android/support/support-v4/22.2.1/support-v4-22.2.1-sources.jar",
    93  		"-d", tmpdir+"/work",
    94  	)
    95  	/*		"-classpath", "/home/z/go-projects/src/github.com/democratic-coin/dcoin-go/xwalk_core_library_java.jar:/home/z/go-projects/src/github.com/democratic-coin/dcoin-go/R.jar:"+androidHome+"/extras/android/support/v7/appcompat/libs/android-support-v7-appcompat.jar:"+androidHome+"/extras/android/support/v7/appcompat/libs/android-support-v4.jar",
    96  */
    97  	cmd.Args = append(cmd.Args, javaFiles...)
    98  	fmt.Println("cmd.Path:", cmd.Path)
    99  	fmt.Println("cmd.Args:", cmd.Args)
   100  	
   101  	if out, err := cmd.CombinedOutput(); err != nil {
   102  		fmt.Println(cmd.Args)
   103  		os.Stderr.Write(out)
   104  		return err
   105  	}
   106  	buildTools, err := findLast(androidHome + "/build-tools")
   107  	if err != nil {
   108  		return err
   109  	}
   110  	cmd = exec.Command(
   111  		buildTools+"/dx",
   112  		"--dex",
   113  		"--output="+tmpdir+"/classes.dex",
   114  		tmpdir+"/work",
   115  	)
   116  	if out, err := cmd.CombinedOutput(); err != nil {
   117  		os.Stderr.Write(out)
   118  		return err
   119  	}
   120  	src, err := ioutil.ReadFile(tmpdir + "/classes.dex")
   121  	if err != nil {
   122  		return err
   123  	}
   124  	err = ioutil.WriteFile("/home/z/go-projects/src/github.com/democratic-coin/dcoin-go/classes.dex", src, 0644)
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	/*data := base64.StdEncoding.EncodeToString(src)
   130  
   131  	buf := new(bytes.Buffer)
   132  	fmt.Fprint(buf, header)
   133  
   134  	var piece string
   135  	for len(data) > 0 {
   136  		l := 70
   137  		if l > len(data) {
   138  			l = len(data)
   139  		}
   140  		piece, data = data[:l], data[l:]
   141  		fmt.Fprintf(buf, "\t`%s` + \n", piece)
   142  	}
   143  	fmt.Fprintf(buf, "\t``")
   144  	out, err := format.Source(buf.Bytes())
   145  	if err != nil {
   146  		buf.WriteTo(os.Stderr)
   147  		return err
   148  	}
   149  
   150  	w, err := os.Create(*outfile)
   151  	if err != nil {
   152  		return err
   153  	}
   154  	if _, err := w.Write(out); err != nil {
   155  		return err
   156  	}
   157  	if err := w.Close(); err != nil {
   158  		return err
   159  	}*/
   160  	return nil
   161  }
   162  
   163  func findLast(path string) (string, error) {
   164  	dir, err := os.Open(path)
   165  	if err != nil {
   166  		return "", err
   167  	}
   168  	children, err := dir.Readdirnames(-1)
   169  	if err != nil {
   170  		return "", err
   171  	}
   172  	return path + "/" + children[len(children)-1], nil
   173  }
   174  
   175  var header = `// Copyright 2015 The Go Authors.  All rights reserved.
   176  // Use of this source code is governed by a BSD-style
   177  // license that can be found in the LICENSE file.
   178  
   179  // File is automatically generated by gendex.go. DO NOT EDIT.
   180  
   181  package main
   182  
   183  var dexStr = `