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 <crawshaw@golang.org>
This commit is contained in:
Hyang-Ah (Hana) Kim 2015-07-30 13:41:20 -04:00 committed by Hyang-Ah Hana Kim
parent 92213082f9
commit dc0c746bbb
3 changed files with 75 additions and 28 deletions

View File

@ -5,7 +5,7 @@ gobindPlugin invokes gomobile bind command on the specified package.
build.gradle:
<pre>
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"
}
</pre>

View File

@ -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 {

View File

@ -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<String> 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 = ""
}