app: initialize asset manager for gobind android app.

Tested manually by modifying example/libhello to read an asset file,
and checking crawshaw's balloon app continues to work.

Fixes golang/go#9422.

Change-Id: I17b25d9456a4023c68be1de82ee071ea01299e68
Reviewed-on: https://go-review.googlesource.com/5680
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Hyang-Ah Hana Kim
2015-02-24 16:06:04 +00:00
parent 605b2be3c2
commit 595ca5ba2c
2 changed files with 20 additions and 4 deletions
+13
View File
@@ -4,6 +4,7 @@
// +build android
#include <android/asset_manager_jni.h>
#include <android/log.h>
#include <dlfcn.h>
#include <errno.h>
@@ -210,6 +211,18 @@ void ANativeActivity_onCreate(ANativeActivity *activity, void* savedState, size_
JNIEXPORT void JNICALL
Java_go_Go_run(JNIEnv* env, jclass clazz, jobject ctx) {
current_ctx = (*env)->NewGlobalRef(env, ctx);
if (current_ctx != NULL) {
// Init asset_manager.
jclass context_clazz = find_class(env, "android/content/Context");
jmethodID getassets = find_method(
env, context_clazz, "getAssets", "()Landroid/content/res/AssetManager;");
// Prevent the java AssetManager from being GC'd
jobject asset_manager_ref = (*env)->NewGlobalRef(
env, (*env)->CallObjectMethod(env, current_ctx, getassets));
asset_manager = AAssetManager_fromJava(env, asset_manager_ref);
}
init_go_runtime(NULL);
}
+7 -4
View File
@@ -40,6 +40,11 @@ jobject current_ctx;
// current_native_activity is the Android ANativeActivity. May be NULL.
ANativeActivity* current_native_activity;
// asset_manager is the asset manager of the app.
// For all-Go app, this is initialized in onCreate.
// For go library app, this is set from the context passed to Go.run.
AAssetManager* asset_manager;
// build_auxv builds an ELF auxiliary vector for initializing the Go
// runtime. While there does not appear to be any spec for this
// format, there are some notes in
@@ -67,11 +72,9 @@ import (
"golang.org/x/mobile/geom"
)
var assetManager *C.AAssetManager
//export onCreate
func onCreate(activity *C.ANativeActivity) {
assetManager = activity.assetManager
C.asset_manager = activity.assetManager
config := C.AConfiguration_new()
C.AConfiguration_fromAssetManager(config, activity.assetManager)
@@ -192,7 +195,7 @@ func openAsset(name string) (ReadSeekCloser, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
a := &asset{
ptr: C.AAssetManager_open(assetManager, cname, C.AASSET_MODE_UNKNOWN),
ptr: C.AAssetManager_open(C.asset_manager, cname, C.AASSET_MODE_UNKNOWN),
name: name,
}
if a.ptr == nil {