[mobile] example/libhello: convert to use gomobile and gradle for build.

344 views
Skip to first unread message

Hyang-Ah Hana Kim (Gerrit)

unread,
Apr 28, 2015, 10:40:36 PM4/28/15
to Ian Lance Taylor, Hyang-Ah Hana Kim, golang-co...@googlegroups.com
Hyang-Ah Hana Kim uploaded a change:
https://go-review.googlesource.com/9455

example/libhello: convert to use gomobile and gradle for build.

Change-Id: Iaae6d5fec95be3cf47f8cfa62cad1bb77557ece0
---
A example/libhello/.gitignore
M example/libhello/README
M example/libhello/all.bash
M example/libhello/all.bat
A example/libhello/app/.gitignore
A example/libhello/app/build.gradle
R example/libhello/app/src/main/AndroidManifest.xml
R example/libhello/app/src/main/java/com/example/hello/MainActivity.java
A example/libhello/build.gradle
D example/libhello/build.xml
A example/libhello/gradle/wrapper/gradle-wrapper.jar
A example/libhello/gradle/wrapper/gradle-wrapper.properties
A example/libhello/gradlew
A example/libhello/gradlew.bat
A example/libhello/hi/.gitignore
A example/libhello/hi/build.gradle
D example/libhello/hi/go_hi/go_hi.go
D example/libhello/main.go
M example/libhello/make.bash
M example/libhello/make.bat
A example/libhello/settings.gradle
D example/libhello/src/go/hi/Hi.java
22 files changed, 398 insertions(+), 146 deletions(-)



diff --git a/example/libhello/.gitignore b/example/libhello/.gitignore
new file mode 100644
index 0000000..9c4de58
--- /dev/null
+++ b/example/libhello/.gitignore
@@ -0,0 +1,7 @@
+.gradle
+/local.properties
+/.idea/workspace.xml
+/.idea/libraries
+.DS_Store
+/build
+/captures
diff --git a/example/libhello/README b/example/libhello/README
index ce7f17b..720b0d9 100644
--- a/example/libhello/README
+++ b/example/libhello/README
@@ -5,11 +5,12 @@
(business logic, code shared with a Go server, portable code).

The Java entry point to the program is the file
-src/com/example/hello/MainActivity.java, where the statement
+app/src/main/java/com/example/hello/MainActivity.java, where the statement

Hi.Hello("world");

-is a call into Go code.
+is a call into Go code. Note that Go.init must be called at the beginning
+of the app to initiate the Go runtime.

The Go code is in a package called hi, the file is hi/hi.go, and it
contains the function Hello:
@@ -18,25 +19,20 @@
fmt.Printf("Hello, %s!\n", name)
}

-Java language bindings are generated for this package using the gobind
-tool. There is a user guide for gobind at
+The 'gomobile bind' command generates Java language bindings for this
+package, compiles the generated source, and builds an AAR (Android Archive)
+file for the binary distribution. There is a user guide for gomobile at
+
+ http://golang.org/x/mobile/cmd/gomobile
+
+The generated source is not included in the distribution but you can
+check the source code using the gobind command.

http://golang.org/x/mobile/cmd/gobind

-The generated source has been included in the distribution. If you
-modify the exported interface of package hi, you have to run gobind
-manually before calling all.bash.
+To build the app, we need Go 1.5 or newer version from source,
+the gomobile tool, and Android SDK (http://developer.android.com/sdk).

-Along with the gobind generated source, the app includes a main.go file
-to define the app entry point.
+Set the ANDROID_HOME path to the path to the installed Android SDK
directory.

-make.bash builds the app, all.bash deploys it.
-
-The first step in building the app is to build the native shared
-library out of the Go code, and place it in
-libs/armeabi-v7a/libgojni.so.
-
-The second step is building the app with the standard Android build
-system by calling ant debug (also done in make.bash). Two extra Java
-files are included in the build by make.bash to support the language
-bindings. This produces an apk ready for running on a device.
+make.bash builds the app, and all.bash deploys it.
diff --git a/example/libhello/all.bash b/example/libhello/all.bash
index be7052f..ad947a0 100755
--- a/example/libhello/all.bash
+++ b/example/libhello/all.bash
@@ -3,13 +3,13 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

-# Script to build and launch the app on an android device.
-
set -e

./make.bash

-adb install -r bin/Hello-debug.apk
+adb wait-for-device
+
+adb install -r app/build/outputs/apk/app-debug.apk

adb shell am start -a android.intent.action.MAIN \
-n com.example.hello/com.example.hello.MainActivity
diff --git a/example/libhello/all.bat b/example/libhello/all.bat
index 5ecebe4..ec3b09e 100644
--- a/example/libhello/all.bat
+++ b/example/libhello/all.bat
@@ -9,8 +9,11 @@
echo # building libhello
call make.bat

+echo # waiting for device
+adb wait-for-device
+
echo # installing bin/Hello-debug.apk
-adb install -r bin/Hello-debug.apk >nul
+adb install -r app/build/outputs/apk/app-debug.apk >nul

echo # starting com.example.hello.MainActivity
-adb shell am start -a android.intent.action.MAIN -n
com.example.hello/com.example.hello.MainActivity >nul
\ No newline at end of file
+adb shell am start -a android.intent.action.MAIN -n
com.example.hello/com.example.hello.MainActivity >nul
diff --git a/example/libhello/app/.gitignore
b/example/libhello/app/.gitignore
new file mode 100644
index 0000000..796b96d
--- /dev/null
+++ b/example/libhello/app/.gitignore
@@ -0,0 +1 @@
+/build
diff --git a/example/libhello/app/build.gradle
b/example/libhello/app/build.gradle
new file mode 100644
index 0000000..a7e0abb
--- /dev/null
+++ b/example/libhello/app/build.gradle
@@ -0,0 +1,26 @@
+apply plugin: 'com.android.application'
+
+android {
+ compileSdkVersion 22
+ buildToolsVersion "21.1.2"
+
+ defaultConfig {
+ applicationId "com.example.hello"
+ minSdkVersion 15
+ targetSdkVersion 22
+ versionCode 1
+ versionName "1.0"
+ }
+ buildTypes {
+ release {
+ minifyEnabled false
+ //proguardFiles
getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+ }
+ }
+}
+
+dependencies {
+ compile fileTree(dir: 'libs', include: ['*.jar'])
+ compile 'com.android.support:appcompat-v7:22.0.0'
+ compile project(':hi')
+}
diff --git a/example/libhello/AndroidManifest.xml
b/example/libhello/app/src/main/AndroidManifest.xml
similarity index 63%
rename from example/libhello/AndroidManifest.xml
rename to example/libhello/app/src/main/AndroidManifest.xml
index dddb380..d0e6561 100644
--- a/example/libhello/AndroidManifest.xml
+++ b/example/libhello/app/src/main/AndroidManifest.xml
@@ -1,16 +1,12 @@
-<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2014 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.
-->
-<manifest
- xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.hello"
- android:versionCode="1"
- android:versionName="1.0">
-
- <application android:label="Hello">
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello">
+ <application
+ android:allowBackup="true"
+ android:label="Hello">
<activity android:name="com.example.hello.MainActivity"
android:label="Hello"
android:exported="true">
@@ -19,5 +15,5 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- </application>
-</manifest>
+ </application>
+</manifest>
diff --git a/example/libhello/src/com/example/hello/MainActivity.java
b/example/libhello/app/src/main/java/com/example/hello/MainActivity.java
similarity index 65%
rename from example/libhello/src/com/example/hello/MainActivity.java
rename to
example/libhello/app/src/main/java/com/example/hello/MainActivity.java
index 61fc8b6..e65b569 100644
--- a/example/libhello/src/com/example/hello/MainActivity.java
+++ b/example/libhello/app/src/main/java/com/example/hello/MainActivity.java
@@ -6,8 +6,11 @@

package com.example.hello;

+// go.Go and go.hi.Hi are included in the output of gomobile bind command.
+// gomobile bind golang.org/x/mobile/example/libhello/hi
import go.Go;
import go.hi.Hi;
+
import android.app.Activity;
import android.os.Bundle;

@@ -23,7 +26,13 @@
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ // Go.init initializes the Go runtime; must be called once before
+ // calling into Go code.
Go.init(getApplicationContext());
+
+ // Hi.Hello is the Java language binding for the Hello function
+ // of the package hi (hi/hi.go).
+ // gobind -lang=java golang.org/x/mobile/example/libhello/hi
Hi.Hello("world");
}
}
diff --git a/example/libhello/build.gradle b/example/libhello/build.gradle
new file mode 100644
index 0000000..d3ff69d
--- /dev/null
+++ b/example/libhello/build.gradle
@@ -0,0 +1,19 @@
+// Top-level build file where you can add configuration options common to
all sub-projects/modules.
+
+buildscript {
+ repositories {
+ jcenter()
+ }
+ dependencies {
+ classpath 'com.android.tools.build:gradle:1.1.0'
+
+ // NOTE: Do not place your application dependencies here; they
belong
+ // in the individual module build.gradle files
+ }
+}
+
+allprojects {
+ repositories {
+ jcenter()
+ }
+}
diff --git a/example/libhello/build.xml b/example/libhello/build.xml
deleted file mode 100644
index 066f914..0000000
--- a/example/libhello/build.xml
+++ /dev/null
@@ -1,15 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-Copyright 2014 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.
--->
-<project name="Hello" default="help">
- <property name="target" value="android-19" />
- <property environment="env" />
- <condition property="sdk.dir" value="${env.ANDROID_HOME}">
- <isset property="env.ANDROID_HOME" />
- </condition>
- <fail message="missing ANDROID_HOME env variable" unless="sdk.dir" />
- <import file="${sdk.dir}/tools/ant/build.xml" />
-</project>
diff --git a/example/libhello/gradle/wrapper/gradle-wrapper.jar
b/example/libhello/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..8c0fb64
--- /dev/null
+++ b/example/libhello/gradle/wrapper/gradle-wrapper.jar
Binary files differ
diff --git a/example/libhello/gradle/wrapper/gradle-wrapper.properties
b/example/libhello/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..0c71e76
--- /dev/null
+++ b/example/libhello/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,6 @@
+#Wed Apr 10 15:27:10 PDT 2013
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
diff --git a/example/libhello/gradlew b/example/libhello/gradlew
new file mode 100755
index 0000000..91a7e26
--- /dev/null
+++ b/example/libhello/gradlew
@@ -0,0 +1,164 @@
+#!/usr/bin/env bash
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS
to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn ( ) {
+ echo "$*"
+}
+
+die ( ) {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+esac
+
+# For Cygwin, ensure paths are in UNIX format before anything is touched.
+if $cygwin ; then
+ [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
+fi
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >&-
+APP_HOME="`pwd -P`"
+cd "$SAVED" >&-
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and
no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the
dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\"
\"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"`
### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then
### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=$((i+1))
+ done
+ case $i in
+ (0) set -- ;;
+ (1) set -- "$args0" ;;
+ (2) set -- "$args0" "$args1" ;;
+ (3) set -- "$args0" "$args1" "$args2" ;;
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ (7) set
-- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ (8) set
-- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ (9) set
-- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following
the shell quoting and substitution rules
+function splitJvmOpts() {
+ JVM_OPTS=("$@")
+}
+eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
+JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
+
+exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH"
org.gradle.wrapper.GradleWrapperMain "$@"
diff --git a/example/libhello/gradlew.bat b/example/libhello/gradlew.bat
new file mode 100644
index 0000000..aec9973
--- /dev/null
+++ b/example/libhello/gradlew.bat
@@ -0,0 +1,90 @@
+@if "%DEBUG%" == "" @echo off
+@rem
##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem
##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and
GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in
your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windowz variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+if "%@eval[2+2]" == "4" goto 4NT_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+goto execute
+
+:4NT_args
+@rem Get arguments from the 4NT Shell from JP Software
+set CMD_LINE_ARGS=%$
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%"
-classpath "%CLASSPATH%"
org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code
instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/example/libhello/hi/.gitignore b/example/libhello/hi/.gitignore
new file mode 100644
index 0000000..dd5fb96
--- /dev/null
+++ b/example/libhello/hi/.gitignore
@@ -0,0 +1 @@
+hi.aar
diff --git a/example/libhello/hi/build.gradle
b/example/libhello/hi/build.gradle
new file mode 100644
index 0000000..858108e
--- /dev/null
+++ b/example/libhello/hi/build.gradle
@@ -0,0 +1,2 @@
+configurations.create("default")
+artifacts.add("default", file("hi.aar"))
diff --git a/example/libhello/hi/go_hi/go_hi.go
b/example/libhello/hi/go_hi/go_hi.go
deleted file mode 100644
index 89b9d2d..0000000
--- a/example/libhello/hi/go_hi/go_hi.go
+++ /dev/null
@@ -1,19 +0,0 @@
-// Package go_hi is an autogenerated binder stub for package hi.
-// gobind -lang=go golang.org/x/mobile/example/libhello/hi
-//
-// File is generated by gobind. Do not edit.
-package go_hi
-
-import (
- "golang.org/x/mobile/bind/seq"
- "golang.org/x/mobile/example/libhello/hi"
-)
-
-func proxy_Hello(out, in *seq.Buffer) {
- param_name := in.ReadUTF16()
- hi.Hello(param_name)
-}
-
-func init() {
- seq.Register("hi", 1, proxy_Hello)
-}
diff --git a/example/libhello/main.go b/example/libhello/main.go
deleted file mode 100644
index 320529a..0000000
--- a/example/libhello/main.go
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright 2014 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.
-
-// This is the Go entry point for the libhello app.
-// It is invoked from Java.
-//
-// See README for details.
-package main
-
-import (
- "golang.org/x/mobile/app"
-
- "golang.org/x/mobile/bind/java"
- _ "golang.org/x/mobile/example/libhello/hi/go_hi"
-)
-
-func main() {
- app.Run(app.Callbacks{Start: java.Init})
-}
diff --git a/example/libhello/make.bash b/example/libhello/make.bash
index 97bd45d..0e084b8 100755
--- a/example/libhello/make.bash
+++ b/example/libhello/make.bash
@@ -5,16 +5,22 @@

set -e

+function die() {
+ echo "FAIL: $1"
+ exit 1
+}
+
if [ ! -f make.bash ]; then
- echo 'make.bash must be run from
$GOPATH/src/golang.org/x/mobile/example/libhello'
- exit 1
+ die 'make.bash must be run from
$GOPATH/src/golang.org/x/mobile/example/libhello.'
fi

-mkdir -p libs/armeabi-v7a src/go/hi
-ANDROID_APP=$PWD
-(cd ../.. && ln -sf $PWD/app/*.java $ANDROID_APP/src/go)
-(cd ../.. && ln -sf $PWD/bind/java/Seq.java $ANDROID_APP/src/go)
-CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \
- go build -ldflags="-shared" .
-mv -f libhello libs/armeabi-v7a/libgojni.so
-ant debug
+if [ -z "${ANDROID_HOME}" ]; then
+ die 'ANDROID_HOME must be set.'
+fi
+
+echo "sdk.dir=${ANDROID_HOME}" > local.properties
+
+# TODO(hyangah): call gomobile bind as a gradle task.
+( cd hi ; gomobile bind -i )
+
+./gradlew build -x lint
diff --git a/example/libhello/make.bat b/example/libhello/make.bat
index 1955ade..509e8a5 100644
--- a/example/libhello/make.bat
+++ b/example/libhello/make.bat
@@ -8,39 +8,38 @@

if not exist make.bat goto error-invalid-path

-:go-build
-if not exist libs\armeabi-v7a mkdir libs\armeabi-v7a
-if not exist src\go\hi mkdir src\go\hi
-if not exist jni\armeabi mkdir jni\armeabi
+if not defined %ANDROID_HOME% goto error-missing-android-home

-set CGO_ENABLED=1
-set GOOS=android
-set GOARCH=arm
-set GOARM=7
-set ANDROID_APP=%CD%
+:gomobile-bind
+cd hi
+gomobile bind -i
+cd ..
+if errorlevel 1 goto error-gomobile-bind

-xcopy /y ..\..\app\*.java %ANDROID_APP%\src\go >nul
-copy /y ..\..\bind\java\Seq.java %ANDROID_APP%\src\go\Seq.java >nul
+:gradle
+echo sdk.dir=%ANDROID_HOME% > local.properties
+call gradlew.bat build -x lint
+if errorlevel 1 goto error-gradle-build

-go build -ldflags="-shared" .
-if errorlevel 1 goto error-go-build
-
-move /y libhello libs\armeabi-v7a\libgojni.so >nul
-
-if defined ANT_HOME goto ant-build
-echo ANT_HOME path not defined
-goto end
-
-:ant-build
-call %ANT_HOME%\bin\ant.bat debug >nul
goto end

:error-invalid-path
-echo make.bat must be run from example\libhello
-goto end
+echo make.bat must be run from example\libhello.
+goto fail

-:error-go-build
-echo Error building go lib
-goto end
+:error-missing-android-home
+echo ANDROID_HOME is not set.
+goto fail

-:end
\ No newline at end of file
+:error-gomobile-bind
+echo Error running 'gomobile bind' command.
+goto fail
+
+:error-gradle-build
+echo Error running 'gradle build' command.
+goto fail
+
+:fail
+exit /b 1
+
+:end
diff --git a/example/libhello/settings.gradle
b/example/libhello/settings.gradle
new file mode 100644
index 0000000..d2757a7
--- /dev/null
+++ b/example/libhello/settings.gradle
@@ -0,0 +1 @@
+include ':app', ':hi'
diff --git a/example/libhello/src/go/hi/Hi.java
b/example/libhello/src/go/hi/Hi.java
deleted file mode 100644
index 6514f5a..0000000
--- a/example/libhello/src/go/hi/Hi.java
+++ /dev/null
@@ -1,20 +0,0 @@
-// Java Package hi is a proxy for talking to a Go program.
-//
-// File is generated by gobind. Do not edit.
-package go.hi;
-
-import go.Seq;
-
-public abstract class Hi {
- private Hi() {} // uninstantiable
-
- public static void Hello(String name) {
- go.Seq _in = new go.Seq();
- go.Seq _out = new go.Seq();
- _in.writeUTF16(name);
- Seq.send(DESCRIPTOR, CALL_Hello, _in, _out);
- }
-
- private static final int CALL_Hello = 1;
- private static final String DESCRIPTOR = "hi";
-}

--
https://go-review.googlesource.com/9455

Hyang-Ah Hana Kim (Gerrit)

unread,
Apr 28, 2015, 10:51:46 PM4/28/15
to Hyang-Ah Hana Kim, David Crawshaw, Burcu Dogan, golang-co...@googlegroups.com
Hyang-Ah Hana Kim has posted comments on this change.

example/libhello: convert to use gomobile and gradle for build.

Patch Set 1:

(1 comment)

https://go-review.googlesource.com/#/c/9455/1/example/libhello/make.bash
File example/libhello/make.bash:

Line 23: # TODO(hyangah): call gomobile bind as a gradle task.
I could convert this gomobile bind call step as a gradle task of the ':hi'
project.

+) we don't need all this make.*, all.*, but ask users to run
./gradlew build
or
./gradlew installDebug
and
./gradlew clean

-) many users may be new to gradle and that gradle task may distract and
confuse users. make.bash makes it clear that gomobile bind command runs
underneath. Users of a different build system other than gradle can pick it
up and adopt quickly.

I don't know what is better.


--
https://go-review.googlesource.com/9455
Gerrit-Reviewer: Burcu Dogan <j...@google.com>
Gerrit-Reviewer: David Crawshaw <craw...@golang.org>
Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
Gerrit-HasComments: Yes

David Crawshaw (Gerrit)

unread,
Apr 29, 2015, 6:37:45 AM4/29/15
to Hyang-Ah Hana Kim, Burcu Dogan, golang-co...@googlegroups.com
David Crawshaw has posted comments on this change.

example/libhello: convert to use gomobile and gradle for build.

Patch Set 1:

(2 comments)

https://go-review.googlesource.com/#/c/9455/1/example/libhello/app/src/main/AndroidManifest.xml
File example/libhello/app/src/main/AndroidManifest.xml:

Line 1: <?xml version="1.0" encoding="utf-8"?>
Isn't this line required for valid XML?


https://go-review.googlesource.com/#/c/9455/1/example/libhello/gradlew
File example/libhello/gradlew:

Line 1: #!/usr/bin/env bash
Where does this script come from? It doesn't have a copyright header. Is
there no standard source for it?

It looks like a lot of corner cases for calling:

java -classpath gradle-wrapper.jar org.gradle.wrapper.GradleWrapperMain

Are those corner cases necessary? It's not obvious to me the FD ulimit junk
is required, or that we have to go digging around inside JAVA_HOME to find
the java binary (we can just assume users have it on the path, because
doesn't gomobile bind require javac?). The cygwin cases also feel
unnecessary, especially if we are going to have make.bat.

I suspect you can do without this script.

Hyang-Ah Hana Kim (Gerrit)

unread,
Apr 29, 2015, 11:25:06 AM4/29/15
to Hyang-Ah Hana Kim, David Crawshaw, Burcu Dogan, golang-co...@googlegroups.com
Hyang-Ah Hana Kim uploaded a new patch set:
22 files changed, 399 insertions(+), 139 deletions(-)

Hyang-Ah Hana Kim (Gerrit)

unread,
Apr 29, 2015, 11:28:36 AM4/29/15
to Hyang-Ah Hana Kim, David Crawshaw, Burcu Dogan, golang-co...@googlegroups.com
Hyang-Ah Hana Kim has posted comments on this change.

example/libhello: convert to use gomobile and gradle for build.

Patch Set 1:

(2 comments)

https://go-review.googlesource.com/#/c/9455/1/example/libhello/app/src/main/AndroidManifest.xml
File example/libhello/app/src/main/AndroidManifest.xml:

Line 1: <?xml version="1.0" encoding="utf-8"?>
> Isn't this line required for valid XML?
strange. it was dropped while copying files from a project created through
android studio and gradle build never complained about it. recovered.


https://go-review.googlesource.com/#/c/9455/1/example/libhello/gradlew
File example/libhello/gradlew:

Line 1: #!/usr/bin/env bash
> Where does this script come from? It doesn't have a copyright header. Is
> th
I hate this but
http://gradle.org/docs/current/userguide/gradle_wrapper.html

This allows for users to build the project without setting various env vars
and installing gradle by themselves.

This is auto-generated by android/gradle. I don't know what to do with the
copyright. The page doesn't say anything but these wrappers should be
submitted to vcs as well.

Or, should we just remove all this gradle dependency?


--
https://go-review.googlesource.com/9455
Gerrit-Reviewer: Burcu Dogan <j...@google.com>
Gerrit-Reviewer: David Crawshaw <craw...@golang.org>
Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
Gerrit-HasComments: Yes

David Crawshaw (Gerrit)

unread,
Apr 29, 2015, 11:31:49 AM4/29/15
to Hyang-Ah Hana Kim, Burcu Dogan, golang-co...@googlegroups.com
David Crawshaw has posted comments on this change.

example/libhello: convert to use gomobile and gradle for build.

Patch Set 2:

Can we call the command that autogenerates all of this?

--
https://go-review.googlesource.com/9455
Gerrit-Reviewer: Burcu Dogan <j...@google.com>
Gerrit-Reviewer: David Crawshaw <craw...@golang.org>
Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
Gerrit-HasComments: No

Burcu Dogan (Gerrit)

unread,
Apr 29, 2015, 12:52:59 PM4/29/15
to Hyang-Ah Hana Kim, David Crawshaw, golang-co...@googlegroups.com
Burcu Dogan has posted comments on this change.

example/libhello: convert to use gomobile and gradle for build.

Patch Set 2:

(6 comments)

https://go-review.googlesource.com/#/c/9455/2/example/libhello/all.bat
File example/libhello/all.bat:

Line 13
Why do we redirect the output to nul?


https://go-review.googlesource.com/#/c/9455/2/example/libhello/app/src/main/AndroidManifest.xml
File example/libhello/app/src/main/AndroidManifest.xml:

Line 13: <application android:label="Hello" android:allowBackup="true">
The default should be true, not needed to set allowBackup.


https://go-review.googlesource.com/#/c/9455/2/example/libhello/app/src/main/java/com/example/hello/MainActivity.java
File example/libhello/app/src/main/java/com/example/hello/MainActivity.java:

Line 9: //
Keep docs between /* */, this is a Java file.


Line 10: // gomobile bind golang.org/x/mobile/example/libhello/hi
We should add some white space around the command line to make it
distinguishable from the text above. What about?

/*
* go.Go and go.hi.Hi are included in the output of gomobile bind command.
*
* $ gomobile bind golang.org/x/mobile/example/libhello/hi
*
*/


https://go-review.googlesource.com/#/c/9455/2/example/libhello/gradle/wrapper/gradle-wrapper.properties
File example/libhello/gradle/wrapper/gradle-wrapper.properties:

Line 1: #Wed Apr 10 15:27:10 PDT 2013
I am clueless about gradle. Why do we need to check in the gradle wrapper?


https://go-review.googlesource.com/#/c/9455/2/example/libhello/hi/.gitignore
File example/libhello/hi/.gitignore:

Line 1: hi.aar
Cant we just put it to the top-level .gitignore file? There are so many
gitignores under libhello.


--
https://go-review.googlesource.com/9455
Gerrit-Reviewer: Burcu Dogan <j...@google.com>
Gerrit-Reviewer: David Crawshaw <craw...@golang.org>
Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
Gerrit-HasComments: Yes

Hyang-Ah Hana Kim (Gerrit)

unread,
May 1, 2015, 1:51:35 PM5/1/15
to Hyang-Ah Hana Kim, David Crawshaw, Burcu Dogan, golang-co...@googlegroups.com
Hyang-Ah Hana Kim uploaded a new patch set:
https://go-review.googlesource.com/9455

example/libhello: convert to use gomobile and gradle for build.

Change-Id: Iaae6d5fec95be3cf47f8cfa62cad1bb77557ece0
---
A example/libhello/.gitignore
D example/libhello/AndroidManifest.xml
A example/libhello/MainActivity.java
M example/libhello/README
M example/libhello/all.bash
M example/libhello/all.bat
D example/libhello/build.xml
D example/libhello/hi/go_hi/go_hi.go
M example/libhello/hi/hi.go
D example/libhello/main.go
M example/libhello/make.bash
M example/libhello/make.bat
A example/libhello/setup.go
D example/libhello/src/com/example/hello/MainActivity.java
D example/libhello/src/go/hi/Hi.java
15 files changed, 480 insertions(+), 186 deletions(-)

Hyang-Ah Hana Kim (Gerrit)

unread,
May 1, 2015, 1:57:13 PM5/1/15
to Hyang-Ah Hana Kim, David Crawshaw, Burcu Dogan, golang-co...@googlegroups.com
Hyang-Ah Hana Kim uploaded a new patch set:
https://go-review.googlesource.com/9455

example/libhello: convert to use gomobile and gradle for build.

Change-Id: Iaae6d5fec95be3cf47f8cfa62cad1bb77557ece0
---
A example/libhello/.gitignore
D example/libhello/AndroidManifest.xml
A example/libhello/MainActivity.java
M example/libhello/README
M example/libhello/all.bash
M example/libhello/all.bat
D example/libhello/build.xml
D example/libhello/hi/go_hi/go_hi.go
M example/libhello/hi/hi.go
D example/libhello/main.go
M example/libhello/make.bash
M example/libhello/make.bat
A example/libhello/setup.go
D example/libhello/src/com/example/hello/MainActivity.java
D example/libhello/src/go/hi/Hi.java
15 files changed, 486 insertions(+), 185 deletions(-)

Hyang-Ah Hana Kim (Gerrit)

unread,
May 1, 2015, 2:14:34 PM5/1/15
to Hyang-Ah Hana Kim, David Crawshaw, Burcu Dogan, golang-co...@googlegroups.com
Hyang-Ah Hana Kim uploaded a new patch set:
https://go-review.googlesource.com/9455

example/libhello: convert to use gomobile and gradle for build.

Change-Id: Iaae6d5fec95be3cf47f8cfa62cad1bb77557ece0
---
A example/libhello/.gitignore
D example/libhello/AndroidManifest.xml
A example/libhello/MainActivity.java
M example/libhello/README
M example/libhello/all.bash
M example/libhello/all.bat
D example/libhello/build.xml
D example/libhello/hi/go_hi/go_hi.go
M example/libhello/hi/hi.go
D example/libhello/main.go
M example/libhello/make.bash
M example/libhello/make.bat

Hyang-Ah Hana Kim (Gerrit)

unread,
May 1, 2015, 2:22:06 PM5/1/15
to Hyang-Ah Hana Kim, David Crawshaw, Burcu Dogan, golang-co...@googlegroups.com
Hyang-Ah Hana Kim has posted comments on this change.

example/libhello: convert to use gomobile and gradle for build.

Patch Set 2:

(6 comments)

Thanks for the review.

Now all the gradle, project setup is done through setup.go command.
I wished the android SDK's android command tool did this for me, but
unfortunately, the android command tool doesn't seem to be actively
maintained any more. The android create project command produces
a project structure that's no more consistent with the project management
guide in the developer site, that still uses mavenCentral instead of
jcenter (what
Android Studio uses now), and more importantly, the tradle-wrapper
properties
is outdated so the project doesn't build. :-(

So, I decided to just build android-gradle compatible structure myself.

https://go-review.googlesource.com/#/c/9455/2/example/libhello/all.bat
File example/libhello/all.bat:

Line 13
> Why do we redirect the output to nul?
I don't know. I am removing the redirection for now and will find out when
I get to a windows machine.


https://go-review.googlesource.com/#/c/9455/2/example/libhello/app/src/main/AndroidManifest.xml
File example/libhello/app/src/main/AndroidManifest.xml:

Line 13: <application android:label="Hello" android:allowBackup="true">
> The default should be true, not needed to set allowBackup.
right. I was attempting to make the default android gradle lint task happy,
but there are a couple of more lints (non-critical) it complains about, so
I decided to turn off lint check.

btw, this is now in setup.go (androidManifest const)


https://go-review.googlesource.com/#/c/9455/2/example/libhello/app/src/main/java/com/example/hello/MainActivity.java
File example/libhello/app/src/main/java/com/example/hello/MainActivity.java:

Line 9: //
> Keep docs between /* */, this is a Java file.
Done


Line 10: // gomobile bind golang.org/x/mobile/example/libhello/hi
> We should add some white space around the command line to make it
> distingui
Done


https://go-review.googlesource.com/#/c/9455/2/example/libhello/gradle/wrapper/gradle-wrapper.properties
File example/libhello/gradle/wrapper/gradle-wrapper.properties:

Line 1: #Wed Apr 10 15:27:10 PDT 2013
> I am clueless about gradle. Why do we need to check in the gradle wrapper?
That's how Android Studio set up a project for vcs by default, and how
gradle recommends. We don't need such inefficiency, and now setup.go will
handle this.


https://go-review.googlesource.com/#/c/9455/2/example/libhello/hi/.gitignore
File example/libhello/hi/.gitignore:

Line 1: hi.aar
> Cant we just put it to the top-level .gitignore file? There are so many
> git
removed.


--
https://go-review.googlesource.com/9455
Gerrit-Reviewer: Burcu Dogan <j...@google.com>
Gerrit-Reviewer: David Crawshaw <craw...@golang.org>
Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
Gerrit-HasComments: Yes

David Crawshaw (Gerrit)

unread,
May 1, 2015, 3:53:44 PM5/1/15
to Hyang-Ah Hana Kim, Burcu Dogan, golang-co...@googlegroups.com
David Crawshaw has posted comments on this change.

example/libhello: convert to use gomobile and gradle for build.

Patch Set 5:

Your setup.go is a very nice script, but I would like to understand the
higher level goal here.

Are you proposing that users who write small Android apps use this script
or something similar to setup their environments? Is this an alternative to
Android Studio?

It seems to me that the main way gobind and gradle will be used together is
via Android Studio. I wouldn't expect anyone using Android Studio to build
a project outside of Studio or expect to be able to build it.

So why not make this example guide people in how to setup their environment
they way we would expect them to? That is, write a README that says 1.
Install Android Studio, 2. Create a project that looks like this, 3.
Install this gradle plugin, 4. Copy these particular files into place, 5.
Hit compile.

I realize that's more involved than a setup script, but this example is a
step towards outlining how to use Studio, before we have proper integration.

--
https://go-review.googlesource.com/9455
Gerrit-Reviewer: Burcu Dogan <j...@google.com>
Gerrit-Reviewer: David Crawshaw <craw...@golang.org>
Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
Gerrit-HasComments: No

Burcu Dogan (Gerrit)

unread,
May 2, 2015, 7:43:26 PM5/2/15
to Hyang-Ah Hana Kim, David Crawshaw, golang-co...@googlegroups.com
Burcu Dogan has posted comments on this change.

example/libhello: convert to use gomobile and gradle for build.

Patch Set 5:

setup.go is probably a cleaner document than the English equivalent, but
this guide should also target those who already has an Android Studio
project and want to add Go bits.

I agree that we should explain step by step how to interact with Android
Studio to achieve the desired result, an app that compiles.

(Thanks for your time to reverse engineer this process. If you maintain
setup.go somewhere else, I'd be a user.)

Hyang-Ah Hana Kim (Gerrit)

unread,
Aug 24, 2015, 4:39:13 PM8/24/15
to Hyang-Ah Hana Kim, David Crawshaw, Burcu Dogan, golang-co...@googlegroups.com
Hyang-Ah Hana Kim has abandoned this change.

Change subject: example/libhello: convert to use gomobile and gradle for
build.
......................................................................


Abandoned
Reply all
Reply to author
Forward
0 new messages