AAssetManager_fromJava crash when enabling proguard obfuscation

258 views
Skip to first unread message

ol...@illusionlabs.se

unread,
May 28, 2014, 10:18:09 AM5/28/14
to andro...@googlegroups.com
I'm using AAssetManager_fromJava in my JNI code.
This works perfectly.

Until I enable proguard, then the call to AAssetManager_fromJava crashes.

I'm assuming I need to add something to my proguard config, but what?

I tried add -keep class android.content.res.* but that is not enough.

Anyone know what to add? Have tried searching the documentation and google but couldn't find any information about it.

Cheers,
Olof

Here is the crash:
05-28 16:02:04.752 23051 23051 F libc    : Fatal signal 11 (SIGSEGV) at 0x0000002c (code=1), thread 23051 (llusionlabs.bmx)
05-28 16:02:04.853   123   123 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
05-28 16:02:04.853   123   123 I DEBUG   : Build fingerprint: 'Amazon/soho/soho:4.2.2/JDQ39/11.3.2.2_user_322095420:user/release-keys'
05-28 16:02:04.853   123   123 I DEBUG   : Revision: '1'
05-28 16:02:04.853   123   123 I DEBUG   : pid: 23051, tid: 23051, name: llusionlabs.bmx  >>> se.illusionlabs.bmx <<<
05-28 16:02:04.853   123   123 I DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0000002c
05-28 16:02:04.931   123   123 I DEBUG   :     r0 00000000  r1 00000000  r2 40e8d950  r3 40929c88
05-28 16:02:04.931   123   123 I DEBUG   :     r4 00000000  r5 5c4ffa3d  r6 40e8d950  r7 5c4ffa45
05-28 16:02:04.931   123   123 I DEBUG   :     r8 be9654d8  r9 40defc4c  sl 40e8fe20  fp be9654a8
05-28 16:02:04.931   123   123 I DEBUG   :     ip 00000000  sp be965360  lr 408cb6f1  pc 408cb6f0  cpsr 60000030
05-28 16:02:04.931   123   123 I DEBUG   :     d0  6d41797469766961  d1  6341584d422f787a
05-28 16:02:04.931   123   123 I DEBUG   :     d2  65746e6f635f646f  d3  415f7365725f746e
05-28 16:02:04.931   123   123 I DEBUG   :     d4  0000000000000000  d5  3ff0000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d6  42c8000000000963  d7  0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d8  0000000000000000  d9  0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d10 0000000000000000  d11 0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d12 0000000000000000  d13 0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d14 0000000000000000  d15 0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d16 0000000000000017  d17 0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d18 0000000000000000  d19 0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d20 3fc554e7eb0eb47c  d21 3e66376972bea4d0
05-28 16:02:04.931   123   123 I DEBUG   :     d22 3f4de16b9c24a98f  d23 3fb0f4a31edab38b
05-28 16:02:04.931   123   123 I DEBUG   :     d24 3fede16b9c24a98f  d25 3fe55559ee5e69f9
05-28 16:02:04.931   123   123 I DEBUG   :     d26 0000000000000000  d27 0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d28 0000000000000005  d29 0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     d30 0000000000000000  d31 0000000000000000
05-28 16:02:04.931   123   123 I DEBUG   :     scr 80000090
05-28 16:02:04.931   123   123 I DEBUG   : 
05-28 16:02:04.931   123   123 I DEBUG   : backtrace:
05-28 16:02:04.931   123   123 I DEBUG   :     #00  pc 0004b6f0  /system/lib/libdvm.so
05-28 16:02:04.931   123   123 I DEBUG   :     #01  pc 00008add  /system/lib/libandroid.so (AAssetManager_fromJava+52)
05-28 16:02:04.931   123   123 I DEBUG   :     #02  pc 0011f82c  /data/app-lib/se.illusionlabs.bmx-1/libcommon.so (ILFile_InitAssetManager(_JNIEnv*, _jobject*, char const*)+72)
05-28 16:02:04.931   123   123 I DEBUG   :     #03  pc 002003b8  /data/app-lib/se.illusionlabs.bmx-1/libbmx.so (Java_se_illusionlabs_bmx_BMXJNILib_setup+140)
05-28 16:02:04.931   123   123 I DEBUG   :     #04  pc 0001e390  /system/lib/libdvm.so (dvmPlatformInvoke+112)
05-28 16:02:04.931   123   123 I DEBUG   :     #05  pc 0004d319  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+396)
05-28 16:02:04.931   123   123 I DEBUG   :     #06  pc 0004f477  /system/lib/libdvm.so (dvmResolveNativeMethod(unsigned int const*, JValue*, Method const*, Thread*)+174)
05-28 16:02:04.931   123   123 I DEBUG   :     #07  pc 000277a0  /system/lib/libdvm.so
05-28 16:02:04.931   123   123 I DEBUG   :     #08  pc 0002b488  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)
05-28 16:02:04.931   123   123 I DEBUG   :     #09  pc 0005ff23  /system/lib/libdvm.so (dvmInvokeMethod(Object*, Method const*, ArrayObject*, ArrayObject*, ClassObject*, bool)+374)
05-28 16:02:04.931   123   123 I DEBUG   :     #10  pc 0006783d  /system/lib/libdvm.so
05-28 16:02:04.931   123   123 I DEBUG   :     #11  pc 000277a0  /system/lib/libdvm.so
05-28 16:02:04.931   123   123 I DEBUG   :     #12  pc 0002b488  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)
05-28 16:02:04.939   123   123 I DEBUG   :     #13  pc 0005fc4d  /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+272)
05-28 16:02:04.939   123   123 I DEBUG   :     #14  pc 00049903  /system/lib/libdvm.so
05-28 16:02:04.939   123   123 I DEBUG   :     #15  pc 0004bc59  /system/lib/libandroid_runtime.so
05-28 16:02:04.939   123   123 I DEBUG   :     #16  pc 0004c903  /system/lib/libandroid_runtime.so (android::AndroidRuntime::start(char const*, char const*)+390)
05-28 16:02:04.939   123   123 I DEBUG   :     #17  pc 00000db7  /system/bin/app_process
05-28 16:02:04.939   123   123 I DEBUG   :     #18  pc 0001279f  /system/lib/libc.so (__libc_init+38)
05-28 16:02:04.939   123   123 I DEBUG   :     #19  pc 00000ae8  /system/bin/app_process




And here is my proguard config:

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

-dontwarn com.amazon.**
-keep class com.amazon.** {*;}
-keepattributes *Annotation*

-keep class se.illusionlabs.bmx.BMXJNILib
-keepclassmembers public class se.illusionlabs.bmx.BMXActivity {
public void startMusic();
public void stopMusic();
public void lockRotation();
public void unlockRotation();
}

-keepclassmembers public class se.illusionlabs.bmx.BMXActivityAmazon {
  public void setupIAP();
  public void buyItem(String);
  public void addItem(String);
}

ol...@illusionlabs.se

unread,
May 28, 2014, 10:59:20 AM5/28/14
to andro...@googlegroups.com
Ok, I've found the implementation of the AAssetManager_fromJava function;
It looks like this:

/**
0070  * Asset Manager functionality
0071  */
0072 AAssetManager* AAssetManager_fromJava(JNIEnv* env, jobject assetManager)
0073 {
0074     {
0075         Mutex::Autolock _l(gMutex);
0076 
0077         if (gJNIConfigured == false) {
0078             jclass amClass = env->FindClass("android/content/res/AssetManager");
0079             gAssetManagerOffsets.mObject = env->GetFieldID(amClass, "mObject", "I");
0080             gJNIConfigured = true;
0081         }
0082     }
0083 
0084     return (AAssetManager*) env->GetIntField(assetManager, gAssetManagerOffsets.mObject);
0085 }

So now I doubt my problem is rules in proguard config....
Only thing referenced is android.content.res.AssetManager so my rule should work right?

Anyone have any other ideas why enabling proguard would make a JNI method crash?
If I disable proguard and no other change, everything works fine.

Cheers,
Olof

Phill Djonov

unread,
May 28, 2014, 8:12:54 PM5/28/14
to andro...@googlegroups.com
In your config, you need to make sure you not only have proguard keep the AssetManager class, but also its mObject field (which is what it might be renaming on you).

ol...@illusionlabs.se

unread,
May 29, 2014, 4:49:43 AM5/29/14
to andro...@googlegroups.com
Thanks for helping out!

I still can't get it to work though... Could you possibly advise on exactly how the rules should look?
I'm a bit noob on using proguard.

Shouldn't "-keep class android.content.res.AssetManager" keep both the class and it's members?

I've tried a lot of different options, but I can't get anything to work. At the moment my rules look like this:

-keep class android.content.res.AssetManager
-keepclassmembers public class android.content.res.AssetManager {
  <fields>;
}

Still the same crash.

Cheers,
Olof

ol...@illusionlabs.se

unread,
May 29, 2014, 5:52:55 AM5/29/14
to andro...@googlegroups.com
Ok, I've solved it now.

Turns out it had nothing to do with AAssetManager_fromJava....

My code looks like this:

  char* package_name = JNI_readString(env, c, bmxactivity, "PACKAGE_NAME");
  LOGI("Package name: %s",package_name);
  ILFile_InitAssetManager(env,assManager,package_name);

and I had forgot to keep the "PACKAGE_NAME" field.

I have no idea why the callstack would show the crash within AAssetManager_fromJava which is called from InitAssetManager....
package_name is null, but it's not used until after calling AAssetManager_fromJava.

Seems you do not need any rules for AAssetManager to work with proguard, I've removed them now and everything works fine.

Cheers,
Olof
Reply all
Reply to author
Forward
0 new messages