From dc0c746bbbeac06209a644f19e1252434e11bb64 Mon Sep 17 00:00:00 2001 From: "Hyang-Ah (Hana) Kim" Date: Thu, 30 Jul 2015 13:41:20 -0400 Subject: [PATCH] misc/androidstudio: change GobindPlugin parameters Plugin version 0.2.2: While debugging golang/go#11895 I noticed the PATH param is less intuitive that I hoped for. This change makes the plugin requires instead: pkg: the package to compile GOMOBILE: absolute path to gomobile GO: absolute path to go GOPATH: gopath. GOMOBILE/GO params are optional if Android Studio is configured to include right paths in its environment. Additional changes included in this cl: - GOMOBILEFLAGS param for users to optionally specify gomobile options e.g. -v -x which will be useful for debugging. - Set src/target java version to 1.7. Previous versions of plugins were compiled with JDK8 by accident, which prevented use of the android studio recommended JDK7. Change-Id: Ie7818eb20c5220b16ed0ebccfb91520fe03e6ccd Reviewed-on: https://go-review.googlesource.com/12923 Reviewed-by: David Crawshaw --- misc/androidstudio/README.md | 9 +- misc/androidstudio/build.gradle | 10 ++- .../org/golang/mobile/GobindPlugin.groovy | 84 ++++++++++++++----- 3 files changed, 75 insertions(+), 28 deletions(-) diff --git a/misc/androidstudio/README.md b/misc/androidstudio/README.md index 3a7c62a..5a01e45 100644 --- a/misc/androidstudio/README.md +++ b/misc/androidstudio/README.md @@ -5,7 +5,7 @@ gobindPlugin invokes gomobile bind command on the specified package. build.gradle:
 plugins {
-  id "org.golang.mobile.bind" version "0.2.1"
+  id "org.golang.mobile.bind" version "0.2.2"
 }
 
 gobind {
@@ -15,8 +15,11 @@ gobind {
   // GOPATH
   GOPATH "/home/gopher"
 
-  // PATH to directories with "go" and "gomobile" tools.
-  PATH "path1:path2:"
+  // Absolute path to the gomobile binary
+  GOMOBILE "/mypath/bin/gomobile"
+
+  // Absolute path to the go binary
+  GO "/usr/local/go/bin/go"
 }
 
diff --git a/misc/androidstudio/build.gradle b/misc/androidstudio/build.gradle index 5b92a85..68760dc 100644 --- a/misc/androidstudio/build.gradle +++ b/misc/androidstudio/build.gradle @@ -1,5 +1,9 @@ apply plugin: 'groovy' +// Many android users still use JDK7. +sourceCompatibility = '1.7' +targetCompatibility = '1.7' + buildscript { repositories { maven { @@ -24,10 +28,10 @@ dependencies { } pluginBundle { - website = 'https://golang.org' - vcsUrl = 'https://github.com/golang/mobile' + website = 'https://golang.org/x/mobile' + vcsUrl = 'https://golang.org/x/mobile' description = 'Plugin for gomobile projects (beta)' - version = '0.2.1' + version = '0.2.2' plugins { gobindPlugin { 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 227cb0e..0721bf6 100644 --- a/misc/androidstudio/src/main/groovy/org/golang/mobile/GobindPlugin.groovy +++ b/misc/androidstudio/src/main/groovy/org/golang/mobile/GobindPlugin.groovy @@ -46,13 +46,30 @@ class GobindTask extends DefaultTask implements OutputFileTask { @TaskAction def gobind() { - def pkg = project.gobind.pkg.trim() - def gopath = project.gobind.GOPATH.trim() - def paths = project.gobind.PATH.trim() + File.pathSeparator + System.getenv("PATH") + 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 gomobile = findGomobile() + + 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() Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) @@ -65,39 +82,62 @@ class GobindTask extends DefaultTask implements OutputFileTask { project.exec { executable(gomobile) - args("bind", "-target=android", "-i", "-o", project.name+".aar", pkg) + def cmd = ["bind", "-target=android", "-i", "-o", project.name+".aar"] + if (gomobileFlags) { + cmd = (cmd+gomobileFlags.split(" ")).flatten() + } + cmd << pkg + + args(cmd) if (!androidHome?.trim()) { throw new GradleException('Neither sdk.dir or ANDROID_HOME is set') } environment("GOPATH", gopath) - environment("PATH", paths) + environment("PATH", paths.join(File.pathSeparator)) environment("ANDROID_HOME", androidHome) } } - def findGomobile() { - def gomobile = "gomobile" - if (System.getProperty("os.name").startsWith("Windows")) { - gomobile = "gomobile.exe" - } - def paths = project.gobind.PATH + File.pathSeparator + System.getenv("PATH") - for (p in paths.split(File.pathSeparator)) { - def f = new File(p + File.separator + gomobile) - if (f.exists()) { - return p + File.separator + gomobile + def isWindows() { + return System.getProperty("os.name").startsWith("Windows") + } + + def findExecutable(String name, ArrayList paths) { + if (isWindows() && !name.endsWith(".exe")) { + name = name + ".exe" } - } - throw new GradleException('failed to find gomobile command from ' + paths) + for (p in paths) { + def f = new File(p + File.separator + name) + if (f.exists()) { + return p + File.separator + name + } + } + throw new GradleException('binary ' + name + ' is not found in $PATH (' + paths + ')') + } + + def findDir(String binpath) { + if (!binpath) { + return "" + } + + def f = new File(binpath) + return f.getParentFile().getAbsolutePath(); } } class GobindExtension { - // Package to bind. + // Package to bind. (required) def String pkg = "" - // GOPATH: necessary for gomobile tool. + // GOPATH: necessary for gomobile tool. (required) def String GOPATH = System.getenv("GOPATH") - // PATH: must include path to 'gomobile' and 'go' binary. - def String PATH = "" + // GO: path to go tool. (can omit if 'go' is in the paths visible by Android Studio) + def String GO = "" + + // 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 = "" }