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