After a recent Android app update, it crashes after being opened for the first time. This is due to a multiDex issue according to log file outputs. In the Play developer console I'm getting
java.lang.NoClassDefFoundError errors like this:
java.lang.NoClassDefFoundError:
at com.google.android.gms.internal.zzcel.zzxK (Unknown Source)
at com.google.android.gms.internal.zzcfv.<init> (Unknown Source)
at com.google.android.gms.internal.zzcgk.<init> (Unknown Source)
at com.google.android.gms.internal.zzcgk.zzbj (Unknown Source)
at com.google.android.gms.internal.zzcgb.onReceive (Unknown Source)
at com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver.onReceive (Unknown Source)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:2725)
at android.app.ActivityThread.-wrap14 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1421)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5417)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
I attempted to follow the advice on the Android developer Multidex page. My minSdkVersion is 16 so I included both the multiDexEnabled true configuration and compile 'com.android.support:multidex:1.0.1' dependency. My Application class extends MultiDexApplication.
In the app-level build.gradle, I have both multiDexKeepFile file('multidex-config.txt') and multiDexKeepProguard 'multidex-config.pro' within android > buildTypes > release section.
The multidex-config.pro file within the same directory as app build.gradle consists of one line:
-keep com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver.class
And multidex-config.txt in that directory has:
com/google/android/gms/measurement/AppMeasurementInstallReferrerReceiver.class
However, when I try to create a release build apk by running npm run build-android-prod (my npm script for that is export ENVFILE=.env.prod && cd android && ./gradlew assembleRelease && cd ..), it ends up failing with the last few lines being:
Copying resources from program directory [/Users/patneedham/Desktop/dev/react-native/MyApp/android/app/build/intermediates/transforms/mergeJavaRes/release/folders/2/1f/main] (filtered)
Copying resources from program directory [/Users/patneedham/Desktop/dev/react-native/MyApp/android/app/build/intermediates/classes/release] (filtered)
Printing classes to [/Users/patneedham/Desktop/dev/react-native/MyApp/android/app/build/outputs/mapping/release/dump.txt]...
:app:transformClassesWithMultidexlistForRelease FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformClassesWithMultidexlistForRelease'.
> com.android.build.api.transform.TransformException: proguard.ParseException: Unexpected keyword 'com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver.class' in line 1 of file '/Users/patneedham/Desktop/dev/react-native/MyApp/android/app/multidex-config.pro'
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 1 mins 29.609 secs
npm ERR! Darwin 16.7.0
npm ERR! argv "/usr/local/Cellar/node/7.8.0/bin/node" "/usr/local/bin/npm" "run" "build-android-prod" "--stacktrace" "--debug"
npm ERR! node v7.8.0
npm ERR! npm v4.2.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! MyApp@0.0.1 build-android-prod: `export ENVFILE=.env.prod && cd android && ./gradlew assembleRelease && cd ..`
npm ERR! Exit status 1
...
The line that bugs me is: com.android.build.api.transform.TransformException: proguard.ParseException: Unexpected keyword 'com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver.class' in line 1 of file '/Users/patneedham/Desktop/dev/react-native/MyApp/android/app/multidex-config.pro'
Okay I realized I forgot the class keyword, so I added that in between -keep and com.google..., build was successful, and uploaded to Google Play alpha channel yet the same first-open app crash occurs. To be more clear about this, when I click the "Open" button from the Google play store screen, after a second or two I see a white screen for another second, then it closes with the default app crash alert saying 'Unfortunately, MyApp has stopped.'
How can I prevent this NoClassDefFoundError when the app first opens?