2015-07-10 16:47:46 -06:00
// 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.
package main
import (
"bytes"
"crypto/x509"
2015-07-08 10:58:53 -06:00
"encoding/base64"
2015-07-10 16:47:46 -06:00
"encoding/pem"
2016-08-02 04:11:57 -05:00
"encoding/xml"
2015-07-10 16:47:46 -06:00
"errors"
"fmt"
"go/build"
"io"
"io/ioutil"
2015-07-08 10:58:53 -06:00
"log"
2015-07-10 16:47:46 -06:00
"os"
"path"
"path/filepath"
"strings"
2016-08-02 04:11:57 -05:00
"golang.org/x/mobile/internal/binres"
2015-07-10 16:47:46 -06:00
)
2015-12-11 17:19:23 -05:00
func goAndroidBuild ( pkg * build . Package , androidArchs [ ] string ) ( map [ string ] bool , error ) {
cmd/gomobile: replace stripped NDK with external NDK
Gomobile has up until now used stripped NDKs hosted by Google. This
arrangement adds maintenance overhead and blocks the use of custom
NDKs or custom API levels. Also, as noted in issue 16211, the stripped
NDK is no longer tiny because Gomobile supports more platforms.
This CL removed the code for generating and packaging stripped NDKs and
adds support for using external NDKs to the gomobile tool.
gomobile init will now use the NDK installed by the Android SDK manager,
if present, or a user specified NDK if the -ndk flag is given. If no
NDK was found or specified, Android initialization is skipped. gomobile
will instruct the user to run init with a valid NDK if bind or build is
invoked without Android initialization.
gomobile init will also attempt to build OpenAL for Android if the -openal
flag specifies a source directory. It needs cmake and, on Windows, nmake
installed. If gomobile build is run on an app that requires
golang.org/x/mobile/exp/audio/al and OpenAL wasn't built by init, the user
is instructed to do so.
Tested on Linux, macOS, Windows.
Fixes golang/go#16211
Fixes golang/go#18522
Change-Id: Ia38f6e43e671a207dad562678c65225b426e7e3e
Reviewed-on: https://go-review.googlesource.com/35173
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-13 00:59:09 +01:00
if ndkRoot == "" {
return nil , errors . New ( "no Android NDK path is set. Please run gomobile init with the ndk-bundle installed through the Android SDK manager or with the -ndk flag set." )
}
2015-11-12 17:08:01 -05:00
appName := path . Base ( pkg . ImportPath )
libName := androidPkgName ( appName )
2015-08-09 13:46:46 -04:00
manifestPath := filepath . Join ( pkg . Dir , "AndroidManifest.xml" )
manifestData , err := ioutil . ReadFile ( manifestPath )
2015-07-10 16:47:46 -06:00
if err != nil {
if ! os . IsNotExist ( err ) {
2015-07-24 16:47:16 -04:00
return nil , err
2015-07-10 16:47:46 -06:00
}
2015-08-02 19:52:31 -04:00
2015-07-10 16:47:46 -06:00
buf := new ( bytes . Buffer )
buf . WriteString ( ` <?xml version="1.0" encoding="utf-8"?> ` )
err := manifestTmpl . Execute ( buf , manifestTmplData {
// TODO(crawshaw): a better package path.
2015-11-12 17:08:01 -05:00
JavaPkgPath : "org.golang.todo." + libName ,
Name : strings . Title ( appName ) ,
2015-07-10 16:47:46 -06:00
LibName : libName ,
} )
if err != nil {
2015-07-24 16:47:16 -04:00
return nil , err
2015-07-10 16:47:46 -06:00
}
manifestData = buf . Bytes ( )
if buildV {
fmt . Fprintf ( os . Stderr , "generated AndroidManifest.xml:\n%s\n" , manifestData )
}
} else {
libName , err = manifestLibName ( manifestData )
if err != nil {
2015-08-09 13:46:46 -04:00
return nil , fmt . Errorf ( "error parsing %s: %v" , manifestPath , err )
2015-07-10 16:47:46 -06:00
}
}
2015-12-11 17:19:23 -05:00
libFiles := [ ] string { }
2015-12-15 10:54:54 -05:00
nmpkgs := make ( map [ string ] map [ string ] bool ) // map: arch -> extractPkgs' output
2015-12-10 15:52:40 -05:00
2015-12-11 17:19:23 -05:00
for _ , arch := range androidArchs {
env := androidEnv [ arch ]
toolchain := ndk . Toolchain ( arch )
libPath := "lib/" + toolchain . abi + "/lib" + libName + ".so"
libAbsPath := filepath . Join ( tmpdir , libPath )
if err := mkdir ( filepath . Dir ( libAbsPath ) ) ; err != nil {
return nil , err
}
err = goBuild (
pkg . ImportPath ,
env ,
"-buildmode=c-shared" ,
"-o" , libAbsPath ,
)
if err != nil {
return nil , err
}
nmpkgs [ arch ] , err = extractPkgs ( toolchain . Path ( "nm" ) , libAbsPath )
if err != nil {
return nil , err
}
libFiles = append ( libFiles , libPath )
2015-07-10 16:47:46 -06:00
}
2015-07-24 16:47:16 -04:00
2015-07-10 16:47:46 -06:00
block , _ := pem . Decode ( [ ] byte ( debugCert ) )
if block == nil {
2015-07-24 16:47:16 -04:00
return nil , errors . New ( "no debug cert" )
2015-07-10 16:47:46 -06:00
}
privKey , err := x509 . ParsePKCS1PrivateKey ( block . Bytes )
if err != nil {
2015-07-24 16:47:16 -04:00
return nil , err
2015-07-10 16:47:46 -06:00
}
if buildO == "" {
2015-11-12 17:08:01 -05:00
buildO = androidPkgName ( filepath . Base ( pkg . Dir ) ) + ".apk"
2015-07-10 16:47:46 -06:00
}
if ! strings . HasSuffix ( buildO , ".apk" ) {
2015-07-24 16:47:16 -04:00
return nil , fmt . Errorf ( "output file name %q does not end in '.apk'" , buildO )
2015-07-10 16:47:46 -06:00
}
var out io . Writer
if ! buildN {
f , err := os . Create ( buildO )
if err != nil {
2015-07-24 16:47:16 -04:00
return nil , err
2015-07-10 16:47:46 -06:00
}
defer func ( ) {
if cerr := f . Close ( ) ; err == nil {
err = cerr
}
} ( )
out = f
}
var apkw * Writer
if ! buildN {
apkw = NewWriter ( out , privKey )
}
2015-09-10 14:00:14 -04:00
apkwCreate := func ( name string ) ( io . Writer , error ) {
2015-07-10 16:47:46 -06:00
if buildV {
fmt . Fprintf ( os . Stderr , "apk: %s\n" , name )
}
if buildN {
return ioutil . Discard , nil
}
return apkw . Create ( name )
}
2015-09-10 14:00:14 -04:00
apkwWriteFile := func ( dst , src string ) error {
w , err := apkwCreate ( dst )
if err != nil {
return err
}
if ! buildN {
f , err := os . Open ( src )
if err != nil {
return err
}
defer f . Close ( )
if _ , err := io . Copy ( w , f ) ; err != nil {
return err
}
}
return nil
}
2015-07-10 16:47:46 -06:00
2016-08-02 04:11:57 -05:00
w , err := apkwCreate ( "classes.dex" )
2015-07-08 10:58:53 -06:00
if err != nil {
2015-07-24 16:47:16 -04:00
return nil , err
2015-07-08 10:58:53 -06:00
}
dexData , err := base64 . StdEncoding . DecodeString ( dexStr )
if err != nil {
log . Fatal ( "internal error bad dexStr: %v" , err )
}
if _ , err := w . Write ( dexData ) ; err != nil {
2015-07-24 16:47:16 -04:00
return nil , err
2015-07-08 10:58:53 -06:00
}
2015-12-11 17:19:23 -05:00
for _ , libFile := range libFiles {
if err := apkwWriteFile ( libFile , filepath . Join ( tmpdir , libFile ) ) ; err != nil {
return nil , err
}
2015-07-10 16:47:46 -06:00
}
2015-12-11 17:19:23 -05:00
for _ , arch := range androidArchs {
toolchain := ndk . Toolchain ( arch )
2015-12-15 10:54:54 -05:00
if nmpkgs [ arch ] [ "golang.org/x/mobile/exp/audio/al" ] {
2016-01-21 10:34:33 -05:00
dst := "lib/" + toolchain . abi + "/libopenal.so"
cmd/gomobile: replace stripped NDK with external NDK
Gomobile has up until now used stripped NDKs hosted by Google. This
arrangement adds maintenance overhead and blocks the use of custom
NDKs or custom API levels. Also, as noted in issue 16211, the stripped
NDK is no longer tiny because Gomobile supports more platforms.
This CL removed the code for generating and packaging stripped NDKs and
adds support for using external NDKs to the gomobile tool.
gomobile init will now use the NDK installed by the Android SDK manager,
if present, or a user specified NDK if the -ndk flag is given. If no
NDK was found or specified, Android initialization is skipped. gomobile
will instruct the user to run init with a valid NDK if bind or build is
invoked without Android initialization.
gomobile init will also attempt to build OpenAL for Android if the -openal
flag specifies a source directory. It needs cmake and, on Windows, nmake
installed. If gomobile build is run on an app that requires
golang.org/x/mobile/exp/audio/al and OpenAL wasn't built by init, the user
is instructed to do so.
Tested on Linux, macOS, Windows.
Fixes golang/go#16211
Fixes golang/go#18522
Change-Id: Ia38f6e43e671a207dad562678c65225b426e7e3e
Reviewed-on: https://go-review.googlesource.com/35173
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-13 00:59:09 +01:00
src := filepath . Join ( gomobilepath , dst )
if _ , err := os . Stat ( src ) ; err != nil {
return nil , errors . New ( "the Android requires the golang.org/x/mobile/exp/audio/al, but the OpenAL libraries was not found. Please run gomobile init with the -openal flag pointing to an OpenAL source directory." )
2015-12-11 17:19:23 -05:00
}
cmd/gomobile: replace stripped NDK with external NDK
Gomobile has up until now used stripped NDKs hosted by Google. This
arrangement adds maintenance overhead and blocks the use of custom
NDKs or custom API levels. Also, as noted in issue 16211, the stripped
NDK is no longer tiny because Gomobile supports more platforms.
This CL removed the code for generating and packaging stripped NDKs and
adds support for using external NDKs to the gomobile tool.
gomobile init will now use the NDK installed by the Android SDK manager,
if present, or a user specified NDK if the -ndk flag is given. If no
NDK was found or specified, Android initialization is skipped. gomobile
will instruct the user to run init with a valid NDK if bind or build is
invoked without Android initialization.
gomobile init will also attempt to build OpenAL for Android if the -openal
flag specifies a source directory. It needs cmake and, on Windows, nmake
installed. If gomobile build is run on an app that requires
golang.org/x/mobile/exp/audio/al and OpenAL wasn't built by init, the user
is instructed to do so.
Tested on Linux, macOS, Windows.
Fixes golang/go#16211
Fixes golang/go#18522
Change-Id: Ia38f6e43e671a207dad562678c65225b426e7e3e
Reviewed-on: https://go-review.googlesource.com/35173
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-13 00:59:09 +01:00
if err := apkwWriteFile ( dst , src ) ; err != nil {
2015-12-11 17:19:23 -05:00
return nil , err
}
2015-09-10 14:00:14 -04:00
}
2015-07-10 16:47:46 -06:00
}
// Add any assets.
2016-08-02 04:11:57 -05:00
var arsc struct {
iconPath string
}
2015-07-10 16:47:46 -06:00
assetsDir := filepath . Join ( pkg . Dir , "assets" )
assetsDirExists := true
fi , err := os . Stat ( assetsDir )
if err != nil {
if os . IsNotExist ( err ) {
assetsDirExists = false
} else {
2015-07-24 16:47:16 -04:00
return nil , err
2015-07-10 16:47:46 -06:00
}
} else {
assetsDirExists = fi . IsDir ( )
}
if assetsDirExists {
2015-09-22 11:26:15 -07:00
// if assets is a symlink, follow the symlink.
assetsDir , err = filepath . EvalSymlinks ( assetsDir )
if err != nil {
return nil , err
}
2015-07-10 16:47:46 -06:00
err = filepath . Walk ( assetsDir , func ( path string , info os . FileInfo , err error ) error {
if err != nil {
return err
}
2015-09-22 14:00:14 -07:00
if name := filepath . Base ( path ) ; strings . HasPrefix ( name , "." ) {
// Do not include the hidden files.
return nil
}
2015-07-10 16:47:46 -06:00
if info . IsDir ( ) {
return nil
}
2016-08-02 04:11:57 -05:00
if rel , err := filepath . Rel ( assetsDir , path ) ; rel == "icon.png" && err == nil {
arsc . iconPath = path
// TODO returning here does not write the assets/icon.png to the final assets output,
// making it unavailable via the assets API. Should the file be duplicated into assets
// or should assets API be able to retrieve files from the generated resource table?
return nil
}
2015-07-10 16:47:46 -06:00
name := "assets/" + path [ len ( assetsDir ) + 1 : ]
2015-09-10 14:00:14 -04:00
return apkwWriteFile ( name , path )
2015-07-10 16:47:46 -06:00
} )
if err != nil {
2015-07-24 16:47:16 -04:00
return nil , fmt . Errorf ( "asset %v" , err )
2015-07-10 16:47:46 -06:00
}
}
2016-08-02 04:11:57 -05:00
bxml , err := binres . UnmarshalXML ( bytes . NewReader ( manifestData ) , arsc . iconPath != "" )
if err != nil {
return nil , err
}
// generate resources.arsc identifying single xxxhdpi icon resource.
if arsc . iconPath != "" {
pkgname , err := bxml . RawValueByName ( "manifest" , xml . Name { Local : "package" } )
if err != nil {
return nil , err
}
tbl , name := binres . NewMipmapTable ( pkgname )
if err := apkwWriteFile ( name , arsc . iconPath ) ; err != nil {
return nil , err
}
w , err := apkwCreate ( "resources.arsc" )
if err != nil {
return nil , err
}
bin , err := tbl . MarshalBinary ( )
if err != nil {
return nil , err
}
if _ , err := w . Write ( bin ) ; err != nil {
return nil , err
}
}
w , err = apkwCreate ( "AndroidManifest.xml" )
if err != nil {
return nil , err
}
bin , err := bxml . MarshalBinary ( )
if err != nil {
return nil , err
}
if _ , err := w . Write ( bin ) ; err != nil {
return nil , err
}
2015-07-10 16:47:46 -06:00
// TODO: add gdbserver to apk?
if ! buildN {
if err := apkw . Close ( ) ; err != nil {
2015-07-24 16:47:16 -04:00
return nil , err
2015-07-10 16:47:46 -06:00
}
}
2015-12-11 17:19:23 -05:00
// TODO: return nmpkgs
2015-12-11 18:36:28 -05:00
return nmpkgs [ androidArchs [ 0 ] ] , nil
2015-07-10 16:47:46 -06:00
}
2015-11-12 17:08:01 -05:00
// androidPkgName sanitizes the go package name to be acceptable as a android
// package name part. The android package name convention is similar to the
// java package name convention described in
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5.3.1
// but not exactly same.
func androidPkgName ( name string ) string {
var res [ ] rune
for i , r := range name {
switch {
case 'a' <= r && r <= 'z' , 'A' <= r && r <= 'Z' :
res = append ( res , r )
case '0' <= r && r <= '9' :
if i == 0 {
panic ( fmt . Sprintf ( "package name %q is not a valid go package name" , name ) )
}
res = append ( res , r )
default :
res = append ( res , '_' )
}
}
if len ( res ) == 0 || res [ 0 ] == '_' {
// Android does not seem to allow the package part starting with _.
res = append ( [ ] rune { 'g' , 'o' } , res ... )
}
s := string ( res )
// Look for Java keywords that are not Go keywords, and avoid using
// them as a package name.
//
// This is not a problem for normal Go identifiers as we only expose
// exported symbols. The upper case first letter saves everything
// from accidentally matching except for the package name.
//
// Note that basic type names (like int) are not keywords in Go.
switch s {
case "abstract" , "assert" , "boolean" , "byte" , "catch" , "char" , "class" ,
"do" , "double" , "enum" , "extends" , "final" , "finally" , "float" ,
"implements" , "instanceof" , "int" , "long" , "native" , "private" ,
"protected" , "public" , "short" , "static" , "strictfp" , "super" ,
"synchronized" , "this" , "throw" , "throws" , "transient" , "try" ,
"void" , "volatile" , "while" :
s += "_"
}
return s
}
2015-07-10 16:47:46 -06:00
// A random uninteresting private key.
// Must be consistent across builds so newer app versions can be installed.
const debugCert = `
-- -- - BEGIN RSA PRIVATE KEY -- -- -
MIIEowIBAAKCAQEAy6ItnWZJ8DpX9R5FdWbS9Kr1U8Z7mKgqNByGU7No99JUnmyu
NQ6Uy6Nj0Gz3o3c0BXESECblOC13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