From 493d0b451b5817dd2a204fce7075cd6cde16a1d3 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 7 Mar 2016 18:46:38 +0100 Subject: [PATCH] modbile/misc/androidstudio: add GOARCH to the gradle plugin Add GOARCH to the gobind gradle plugin to limit the architectures to include in the fat .aar file. If GOARCH is empty or not specified, every supported architecture is included. GobindPlugin.groovy was indented with both tabs and (a varying number of) spaces, so it is re-indented here with tabs. Sorry. For golang/go#12819 Change-Id: I8b2cb72068df7750d20f474395944ca2968a2f1b Reviewed-on: https://go-review.googlesource.com/20305 Reviewed-by: Hyang-Ah Hana Kim --- misc/androidstudio/README.md | 3 + .../org/golang/mobile/GobindPlugin.groovy | 155 +++++++++--------- 2 files changed, 85 insertions(+), 73 deletions(-) diff --git a/misc/androidstudio/README.md b/misc/androidstudio/README.md index 5a01e45..1488b87 100644 --- a/misc/androidstudio/README.md +++ b/misc/androidstudio/README.md @@ -15,6 +15,9 @@ gobind { // GOPATH GOPATH "/home/gopher" + // Optional list of architectures. Defaults to all supported architectures. + GOARCH="arm amd64" + // Absolute path to the gomobile binary GOMOBILE "/mypath/bin/gomobile" diff --git a/misc/androidstudio/src/main/groovy/org/golang/mobile/GobindPlugin.groovy b/misc/androidstudio/src/main/groovy/org/golang/mobile/GobindPlugin.groovy index 91b6b15..d4df51f 100644 --- a/misc/androidstudio/src/main/groovy/org/golang/mobile/GobindPlugin.groovy +++ b/misc/androidstudio/src/main/groovy/org/golang/mobile/GobindPlugin.groovy @@ -23,21 +23,21 @@ import org.golang.mobile.AARPublishArtifact * For gomobile bind command, see https://golang.org/x/mobile/cmd/gomobile */ class GobindPlugin implements Plugin { - void apply(Project project) { - project.configurations.create("default") - project.extensions.create('gobind', GobindExtension) + void apply(Project project) { + project.configurations.create("default") + project.extensions.create('gobind', GobindExtension) - Task gobindTask = project.tasks.create("gobind", GobindTask) - gobindTask.outputFile = project.file(project.name+".aar") - project.artifacts.add("default", new AARPublishArtifact( - 'mylib', - null, - gobindTask)) + Task gobindTask = project.tasks.create("gobind", GobindTask) + gobindTask.outputFile = project.file(project.name+".aar") + project.artifacts.add("default", new AARPublishArtifact( + 'mylib', + null, + gobindTask)) - Task cleanTask = project.tasks.create("clean", { - project.delete(project.name+".aar") - }) - } + Task cleanTask = project.tasks.create("clean", { + project.delete(project.name+".aar") + }) + } } class GobindTask extends DefaultTask implements OutputFileTask { @@ -46,62 +46,68 @@ class GobindTask extends DefaultTask implements OutputFileTask { @TaskAction def gobind() { - def pkg = project.gobind.pkg - def gopath = (project.gobind.GOPATH ?: System.getenv("GOPATH"))?.trim() - if (!pkg || !gopath) { + def pkg = project.gobind.pkg + def gopath = (project.gobind.GOPATH ?: System.getenv("GOPATH"))?.trim() + if (!pkg || !gopath) { throw new GradleException('gobind.pkg and gobind.GOPATH must be set') - } + } - def paths = (gopath.split(File.pathSeparator).collect{ "$it/bin" } + + def paths = (gopath.split(File.pathSeparator).collect{ "$it/bin" } + System.getenv("PATH").split(File.pathSeparator)).flatten() - // Default installation path of go distribution. - if (isWindows()) { - paths = paths + "c:\\Go\\bin" - } else { - paths = paths + "/usr/local/go/bin" - } - - def gomobile = (project.gobind.GOMOBILE ?: findExecutable("gomobile", paths))?.trim() - def gobin = (project.gobind.GO ?: findExecutable("go", paths))?.trim() - def gomobileFlags = project.gobind.GOMOBILEFLAGS?.trim() - - if (!gomobile || !gobin) { - throw new GradleException('failed to find gomobile/go tools. Set gobind.GOMOBILE and gobind.GO') - } - - paths = [findDir(gomobile), findDir(gobin), paths].flatten() - - def androidHome = "" - try { - Properties properties = new Properties() - properties.load(project.rootProject.file('local.properties').newDataInputStream()) - androidHome = properties.getProperty('sdk.dir') - } catch (all) { - logger.info("failed to load local.properties.") - } - if (!androidHome?.trim()) { - // fallback to ANDROID_HOME - androidHome = System.getenv("ANDROID_HOME") - } - - project.exec { - executable(gomobile) - - def cmd = ["bind", "-target=android", "-i", "-o", project.name+".aar"] - if (gomobileFlags) { - cmd = (cmd+gomobileFlags.split(" ")).flatten() + // Default installation path of go distribution. + if (isWindows()) { + paths = paths + "c:\\Go\\bin" + } else { + paths = paths + "/usr/local/go/bin" } - cmd << pkg - args(cmd) + def gomobile = (project.gobind.GOMOBILE ?: findExecutable("gomobile", paths))?.trim() + def gobin = (project.gobind.GO ?: findExecutable("go", paths))?.trim() + def gomobileFlags = project.gobind.GOMOBILEFLAGS?.trim() + def goarch = project.gobind.GOARCH?.trim() + + if (!gomobile || !gobin) { + throw new GradleException('failed to find gomobile/go tools. Set gobind.GOMOBILE and gobind.GO') + } + + paths = [findDir(gomobile), findDir(gobin), paths].flatten() + + def androidHome = "" + try { + Properties properties = new Properties() + properties.load(project.rootProject.file('local.properties').newDataInputStream()) + androidHome = properties.getProperty('sdk.dir') + } catch (all) { + logger.info("failed to load local.properties.") + } if (!androidHome?.trim()) { - throw new GradleException('Neither sdk.dir or ANDROID_HOME is set') + // fallback to ANDROID_HOME + androidHome = System.getenv("ANDROID_HOME") } - environment("GOPATH", gopath) - environment("PATH", paths.join(File.pathSeparator)) - environment("ANDROID_HOME", androidHome) - } - } + + project.exec { + executable(gomobile) + + def cmd = ["bind", "-i", "-o", project.name+".aar", "-target"] + if (goarch) { + cmd = cmd+goarch.split(" ").collect{ 'android/'+it }.join(",") + } else { + cmd << "android" + } + if (gomobileFlags) { + cmd = cmd+gomobileFlags.split(" ") + } + cmd << pkg + + args(cmd) + if (!androidHome?.trim()) { + throw new GradleException('Neither sdk.dir or ANDROID_HOME is set') + } + environment("GOPATH", gopath) + environment("PATH", paths.join(File.pathSeparator)) + environment("ANDROID_HOME", androidHome) + } + } def isWindows() { return System.getProperty("os.name").startsWith("Windows") @@ -114,7 +120,7 @@ class GobindTask extends DefaultTask implements OutputFileTask { for (p in paths) { def f = new File(p + File.separator + name) if (f.exists()) { - return p + File.separator + name + return p + File.separator + name } } throw new GradleException('binary ' + name + ' is not found in $PATH (' + paths + ')') @@ -131,18 +137,21 @@ class GobindTask extends DefaultTask implements OutputFileTask { } class GobindExtension { - // Package to bind. (required) - def String pkg = "" + // Package to bind. (required) + def String pkg = "" - // GOPATH: necessary for gomobile tool. (required) - def String GOPATH = System.getenv("GOPATH") + // GOPATH: necessary for gomobile tool. (required) + def String GOPATH = System.getenv("GOPATH") - // GO: path to go tool. (can omit if 'go' is in the paths visible by Android Studio) - def String GO = "" + // GOARCH: (List of) GOARCH to include. + def String GOARCH = "" - // GOMOBILE: path to gomobile binary. (can omit if 'gomobile' is under GOPATH) - def String GOMOBILE = "" + // GO: path to go tool. (can omit if 'go' is in the paths visible by Android Studio) + def String GO = "" - // GOMOBILEFLAGS: extra flags to be passed to gomobile command. (optional) - def String GOMOBILEFLAGS = "" + // GOMOBILE: path to gomobile binary. (can omit if 'gomobile' is under GOPATH) + def String GOMOBILE = "" + + // GOMOBILEFLAGS: extra flags to be passed to gomobile command. (optional) + def String GOMOBILEFLAGS = "" }