app: support importing the package for bind programs
Delay loading of the getRune method ID to the first GoNativeActivity
is created. That way, importing golang.org/x/mobile/app will not
crash, even for gomobile bind programs that don't include (or use)
GoNativeActivity.
Fixes golang/go#24490
Change-Id: I4bf90e067700451f7c026e53165b6614366d7a94
Reviewed-on: https://go-review.googlesource.com/104395
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/app/android.c b/app/android.c
index c0f57d2..b677869 100644
--- a/app/android.c
+++ b/app/android.c
@@ -35,7 +35,7 @@
return m;
}
-jmethodID key_rune_method;
+static jmethodID key_rune_method;
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
@@ -43,11 +43,6 @@
return -1;
}
- // Load classes here, which uses the correct ClassLoader.
- current_ctx_clazz = find_class(env, "org/golang/app/GoNativeActivity");
- current_ctx_clazz = (jclass)(*env)->NewGlobalRef(env, current_ctx_clazz);
- key_rune_method = find_method(env, current_ctx_clazz, "getRune", "(III)I");
-
return JNI_VERSION_1_6;
}
@@ -69,10 +64,13 @@
current_vm = activity->vm;
current_ctx = activity->clazz;
+ jclass clazz = (*env)->GetObjectClass(env, current_ctx);
+ key_rune_method = find_method(env, clazz, "getRune", "(III)I");
+
setCurrentContext(current_vm, (*env)->NewGlobalRef(env, current_ctx));
// Set TMPDIR.
- jmethodID gettmpdir = find_method(env, current_ctx_clazz, "getTmpdir", "()Ljava/lang/String;");
+ jmethodID gettmpdir = find_method(env, clazz, "getTmpdir", "()Ljava/lang/String;");
jstring jpath = (jstring)(*env)->CallObjectMethod(env, current_ctx, gettmpdir, NULL);
const char* tmpdir = (*env)->GetStringUTFChars(env, jpath, NULL);
if (setenv("TMPDIR", tmpdir, 1) != 0) {
diff --git a/app/android.go b/app/android.go
index e2a4023..dff66c9 100644
--- a/app/android.go
+++ b/app/android.go
@@ -37,7 +37,6 @@
JavaVM* current_vm;
jobject current_ctx;
-jclass current_ctx_clazz;
jclass app_find_class(JNIEnv* env, const char* name);