go.mobile/example/libhello: make example self-contained
This example now uses the pre-L build system. A gradle-based example will follow as soon as I am testing on Android L devices. LGTM=adonovan R=adonovan CC=golang-codereviews https://golang.org/cl/143900043
This commit is contained in:
parent
3b555d82f6
commit
7dcf08fb02
|
@ -0,0 +1,23 @@
|
|||
<?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">
|
||||
<activity android:name="com.example.hello.MainActivity"
|
||||
android:label="Hello"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
|
@ -0,0 +1,42 @@
|
|||
The libhello app demonstrates calling Go code from a primarily Java app.
|
||||
|
||||
Starting in Java lets you program against Android's extensive UI
|
||||
libraries in their native language and call into Go for library code
|
||||
(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
|
||||
|
||||
Hi.Hello("world");
|
||||
|
||||
is a call into Go code.
|
||||
|
||||
The Go code is in a package called hi, the file is hi/hi.go, and it
|
||||
contains the function Hello:
|
||||
|
||||
func Hello(name string) {
|
||||
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
|
||||
|
||||
http://godoc.org/code.google.com/p/go.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.
|
||||
|
||||
Along with the gobind generated source, the app includes a main.go file
|
||||
to define the app entry point.
|
||||
|
||||
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.
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env bash
|
||||
# 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.
|
||||
|
||||
# Script to build and launch the app on an android device.
|
||||
|
||||
set -e
|
||||
|
||||
./make.bash
|
||||
|
||||
adb install -r bin/hello-debug.apk
|
||||
|
||||
adb shell am start -a android.intent.action.MAIN \
|
||||
-n com.example.hello/com.example.hello.MainActivity
|
|
@ -0,0 +1,15 @@
|
|||
<?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>
|
|
@ -2,14 +2,19 @@
|
|||
// 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 (
|
||||
"code.google.com/p/go.mobile/app"
|
||||
|
||||
_ "code.google.com/p/go.mobile/bind/java"
|
||||
_ "code.google.com/p/go.mobile/example/libhello/hi/go_hi"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app.Run()
|
||||
app.Run(app.Callbacks{})
|
||||
}
|
||||
|
|
|
@ -9,17 +9,13 @@ if [ ! -f make.bash ]; then
|
|||
echo 'make.bash must be run from $GOPATH/src/code.google.com/p/go.mobile/example/libhello'
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "$ANDROID_APP" ]; then
|
||||
echo 'ERROR: Environment variable ANDROID_APP is unset.'
|
||||
exit 1
|
||||
fi
|
||||
ANDROID_APP=$(cd $ANDROID_APP; pwd)
|
||||
|
||||
mkdir -p $ANDROID_APP/src/main/jniLibs/armeabi \
|
||||
$ANDROID_APP/src/main/java/go/hi
|
||||
(cd ../.. && ln -sf $PWD/app/*.java $ANDROID_APP/src/main/java/go)
|
||||
(cd ../.. && ln -sf $PWD/bind/java/*.java $ANDROID_APP/src/main/java/go)
|
||||
ln -sf $PWD/hi/*.java $ANDROID_APP/src/main/java/go/hi
|
||||
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)
|
||||
ln -sf $PWD/hi/*.java src/go/hi
|
||||
CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \
|
||||
go build -ldflags="-shared" .
|
||||
mv -f libhello $ANDROID_APP/src/main/jniLibs/armeabi/libgojni.so
|
||||
mv -f libhello libs/armeabi-v7a/libgojni.so
|
||||
ant debug
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.example.hello;
|
||||
|
||||
import go.Go;
|
||||
import go.hi.Hi;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
|
||||
/*
|
||||
* MainActivity is the entry point for the libhello app.
|
||||
*
|
||||
* From here, the Go runtime is initialized and a Go function is
|
||||
* invoked via gobind language bindings.
|
||||
*
|
||||
* See example/libhello/README for details.
|
||||
*/
|
||||
public class MainActivity extends Activity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Go.init(getApplicationContext());
|
||||
Hi.Hello("world");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue