NativeActivity JNI: Retain VM reference to AssetManager

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
diff --git a/core/jni/android_app_NativeActivity.cpp b/core/jni/android_app_NativeActivity.cpp
index fd9e714..ee74ef0 100644
--- a/core/jni/android_app_NativeActivity.cpp
+++ b/core/jni/android_app_NativeActivity.cpp
@@ -128,8 +128,13 @@
         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 @@
     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 @@
 
     code->sdkVersion = sdkVersion;
 
+    code->javaAssetManager = env->NewGlobalRef(jAssetMgr);
     code->assetManager = assetManagerForJavaObject(env, jAssetMgr);
 
     if (obbDir != NULL) {