1
votes

I recently upgraded my build environment to Android studio 2.2, JDK 1.8 (with jack) and I am using gradle wrapper with gradle-2.14.1. My build/release variants compile and run properly on any android release > K. However when I try to run the code on an android K device, the code compiles fine and apk is generated properly but the code immediately crashes with stack trace below as the code can't seem to find BuildConfig class. Would appreciate any pointers that I can possibly look into. Thanks!

FATAL EXCEPTION: main Process: com.trail.android.debug, PID: 21045 java.lang.NoClassDefFoundError: com.trail.android.BuildConfig at com.trail.android.TrailApplication.attachBaseContext(TrailApplication.java:41) at android.app.Application.attach(Application.java:181) at android.app.Instrumentation.newApplication(Instrumentation.java:991) at android.app.Instrumentation.newApplication(Instrumentation.java:975) at android.app.LoadedApk.makeApplication(LoadedApk.java:511) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317) at android.app.ActivityThread.access$1500(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

1
The BuildConfig class should be automatically generated by Android.. or did you create your own (which is bad)? Have you tried doing Build -> Clean Project -> Rebuild Project? - Gino Mempin
Thanks for your reply. I tried a clean project->rebuild but I continue to get the same error. I have not written any custom BuildConfig and using the default generated one. The puzzling thing is, the setup works perfectly fine for android L and above.. - Abhijit Vaidya
Last few guesses, did you set the correct minSdkVersion? have you tried also updating the build tools or the Android gradle plugin versions? - Gino Mempin
yes I am setting minSdkVersion 19 in my build.gradle. Build tools version is 24.0.2 (compileSDK 24) and I am using plugin: 'com.android.application' - Abhijit Vaidya
After doing lot of investigation, looks like I am hitting: code.google.com/p/android/issues/detail?id=224026. The class happens to be in secondary dex file and on android K we have to use legacy multidex. That coupled with Jack, we hit the above bug. - Abhijit Vaidya

1 Answers

0
votes

This was related to https://code.google.com/p/android/issues/detail?id=224026

Some of the classes get into secondary dex file (in my case BuildConfig.class was in secondary dex) and KitKat was not able to find in secondary dex (this works automatically on Android L and above and on pre L the multidex support library should have taken care of this but I was hitting the bug I guess). To workaround the issue, please add following to your jackOptions.

 jackOptions {
            enabled true
            additionalParameters("jack.dex.output.policy" : "multidex")
            additionalParameters("jack.preprocessor" : "true")
            additionalParameters("jack.preprocessor.file" : "legacyMultidexInstallation.jpp")
            additionalParameters("jack.dex.output.multidex.legacy": "true")
        }

With this fix, it worked for me.