Giuseppe De Sio Giuseppe De Sio - 4 months ago 59
Android Question

Gradle "com.android.dex.DexException" building an app involving cordova-plugin-googlemaps and ibm-mfp-push plugins.

I’m trying to build an Ionic app on Android platform, including ibm-mfp-push cordova pplugin and googlemaps cordova plugin.

Starting from a fresh Ionic app, I’ve imported the cordova ibm-mfp-push plugin first, and the android build completes successfully.
After importing the

cordova-plugin-googlemaps
, the gradle build breaks with the following (frequent) exception:

:transformClassesWithDexForDebug

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzqm;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:579)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:517)

at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)


Following the common approach, I looked for all modules causing the conflict in my dependency tree:

[...]
compile - Classpath for compiling the main sources.
+--- com.ibm.mobilefirstplatform.clientsdk.android:core:1.+ -> 1.2.2
| \--- com.squareup.okhttp:okhttp:2.4.0
| \--- com.squareup.okio:okio:1.4.0
+--- com.ibm.mobilefirstplatform.clientsdk.android:push:1.+ -> 1.0.2
| +--- com.android.support:appcompat-v7:23.0.0
| | \--- com.android.support:support-v4:23.0.0
| | \--- com.android.support:support-annotations:23.0.0
| +--- com.ibm.mobilefirstplatform.clientsdk.android:core:1.1.0 -> 1.2.2 (*)
| \--- com.google.android.gms:play-services:7.8.0
| +--- com.google.android.gms:play-services-ads:7.8.0
| | +--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0
| | | +--- com.google.android.gms:play-services-basement:9.2.0
| | | | \--- com.android.support:support-v4:23.0.0 (*)
| | | \--- com.google.android.gms:play-services-tasks:9.2.0
| | | \--- com.google.android.gms:play-services-basement:9.2.0 (*)
| | +--- com.google.android.gms:play-services-analytics:7.8.0
| | | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| | \--- com.google.android.gms:play-services-appindexing:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-analytics:7.8.0 (*)
| +--- com.google.android.gms:play-services-appindexing:7.8.0 (*)
| +--- com.google.android.gms:play-services-appinvite:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-appstate:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-cast:7.8.0
| | +--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| | \--- com.android.support:mediarouter-v7:22.2.0
| | \--- com.android.support:appcompat-v7:22.2.0 -> 23.0.0 (*)
| +--- com.google.android.gms:play-services-drive:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-fitness:7.8.0
| | +--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| | \--- com.google.android.gms:play-services-location:7.8.0 -> 9.2.0
| | +--- com.google.android.gms:play-services-base:9.2.0 (*)
| | \--- com.google.android.gms:play-services-basement:9.2.0 (*)
| +--- com.google.android.gms:play-services-games:7.8.0
| | +--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| | \--- com.google.android.gms:play-services-drive:7.8.0 (*)
| +--- com.google.android.gms:play-services-gcm:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-identity:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-location:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-maps:7.8.0 -> 9.2.0
| | +--- com.google.android.gms:play-services-base:9.2.0 (*)
| | \--- com.google.android.gms:play-services-basement:9.2.0 (*)
| +--- com.google.android.gms:play-services-nearby:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-panorama:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-plus:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-safetynet:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-vision:7.8.0
| | \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| +--- com.google.android.gms:play-services-wallet:7.8.0
| | +--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
| | +--- com.google.android.gms:play-services-identity:7.8.0 (*)
| | \--- com.google.android.gms:play-services-maps:7.8.0 -> 9.2.0 (*)
| \--- com.google.android.gms:play-services-wearable:7.8.0
| \--- com.google.android.gms:play-services-base:7.8.0 -> 9.2.0 (*)
+--- com.google.android.gms:play-services-maps:+ -> 9.2.0 (*)
\--- com.google.android.gms:play-services-location:+ -> 9.2.0 (*)

[...]


Finally I excluded following modules:

configurations {
all*.exclude group: 'com.google.android.gms', module: 'play-services-analytics'
all*.exclude group: 'com.google.android.gms', module: 'play-services-appindexing'
all*.exclude group: 'com.google.android.gms', module: 'play-services-ads'
all*.exclude group: 'com.google.android.gms', module: 'play-services-gcm'
all*.exclude group: 'com.google.android.gms', module: 'play-services-plus'
all*.exclude group: 'com.google.android.gms', module: 'play-services-safetynet'
all*.exclude group: 'com.google.android.gms', module: 'play-services-wallet'
all*.exclude group: 'com.google.android.gms', module: 'play-services-wearable'
}


Now, the build terminates successfully, but I've a ClassNotFoundException at runtime:

E/AndroidRuntime(17337): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(17337): at android.os.AsyncTask$3.done(AsyncTask.java:305)
E/AndroidRuntime(17337): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime(17337): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime(17337): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(17337): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:232)
E/AndroidRuntime(17337): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(17337): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(17337): at java.lang.Thread.run(Thread.java:818)
E/AndroidRuntime(17337): Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/gcm/GoogleCloudMessaging;
E/AndroidRuntime(17337): at com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPush$6.doInBackground(MFPPush.java:547)
E/AndroidRuntime(17337): at com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPush$6.doInBackground(MFPPush.java:541)
E/AndroidRuntime(17337): at android.os.AsyncTask$2.call(AsyncTask.java:293)
E/AndroidRuntime(17337): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(17337): ... 4 more
E/AndroidRuntime(17337): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.gcm.GoogleCloudMessaging" on path: DexPathList[[zip file "/data/app/com.dsg.Smokers-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E/AndroidRuntime(17337): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(17337): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(17337): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/AndroidRuntime(17337): ... 8 more
E/AndroidRuntime(17337): Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.gcm.GoogleCloudMessaging
E/AndroidRuntime(17337): at java.lang.Class.classForName(Native Method)
E/AndroidRuntime(17337): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/AndroidRuntime(17337): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/AndroidRuntime(17337): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/AndroidRuntime(17337): ... 9 more
E/AndroidRuntime(17337): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available


How can I fix my issue?
Thanks a lot for your help.

Answer

Finally solved modifying the project.properties file in Android project, changing the following lines

cordova.system.library.1=com.google.android.gms:play-services-maps:9.2.0
cordova.system.library.2=com.google.android.gms:play-services-location:9.2.0

with the following:

cordova.system.library.1=com.google.android.gms:play-services-maps:7.8.0
cordova.system.library.2=com.google.android.gms:play-services-location:7.8.0