OSDN Git Service

NativeActivity JNI: Retain VM reference to AssetManager
authorAdam Lesinski <adamlesinski@google.com>
Fri, 17 Mar 2017 22:17:12 +0000 (15:17 -0700)
committerAdam Lesinski <adamlesinski@google.com>
Mon, 20 Mar 2017 18:08:40 +0000 (11:08 -0700)
AssetManagers get destroyed when in MultiWindow or when loading
WebView. Retain an reference to the AssetManager given to
Native Code.

This AssetManager does not need to be updated since the changes
happening to the AssetManager involve non-application assets and
configuration changes, both which don't affect the native AAssetManager
which can only load files in assets/ which are unaffected by
configuration changes.

Test: none
Change-Id: Iedf30212fb85a8e6afec0645d689288f58fd77f1

core/jni/android_app_NativeActivity.cpp

index fd9e714..ee74ef0 100644 (file)
@@ -128,8 +128,13 @@ struct NativeCode : public ANativeActivity {
         if (callbacks.onDestroy != NULL) {
             callbacks.onDestroy(this);
         }
-        if (env != NULL && clazz != NULL) {
+        if (env != NULL) {
+          if (clazz != NULL) {
             env->DeleteGlobalRef(clazz);
+          }
+          if (javaAssetManager != NULL) {
+            env->DeleteGlobalRef(javaAssetManager);
+          }
         }
         if (messageQueue != NULL && mainWorkRead >= 0) {
             messageQueue->getLooper()->removeFd(mainWorkRead);
@@ -170,6 +175,10 @@ struct NativeCode : public ANativeActivity {
     int mainWorkRead;
     int mainWorkWrite;
     sp<MessageQueue> messageQueue;
+
+    // Need to hold on to a reference here in case the upper layers destroy our
+    // AssetManager.
+    jobject javaAssetManager;
 };
 
 void android_NativeActivity_finish(ANativeActivity* activity) {
@@ -345,6 +354,7 @@ loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jstring funcName
 
     code->sdkVersion = sdkVersion;
 
+    code->javaAssetManager = env->NewGlobalRef(jAssetMgr);
     code->assetManager = assetManagerForJavaObject(env, jAssetMgr);
 
     if (obbDir != NULL) {