2
votes

This question is the aftermath of a four day long and painful journey of consecutive attempts to overcome a appDebugAssemble failed build. I've read a super considerable amount of topics/articles that google threw at me by looking up anything with the term "multidex" but to no avail.

Recently I've started using Android Studio 3.0 Beta 4 and updated the SDK versions accordingly to Android O build release. Alongside this, I'm using AS3b4 with Gradle 4.1-all, and up to now, I was enjoying the new updates immensely.

Four days ago I decided to add Firebase Analytics and Messaging to my project and that's where it went south. For brevity, I've tried more than 30+ approaches from the suggestions (and accepted answers on SO) on how MultiDex should be configured, and since all did not make the build pass, I'm reaching a state where I'm starting to believe that there's a spell cast upon me, particularly around the time I'm about to release my app to Play Store. I've read and reread this too https://developer.android.com/studio/build/multidex.html

It goes without saying, but your shared wisdom here will be much appreciated and if a potential solution comes up, I'm very positive it'll prevent a lot of developers from going bold, or heads being banged against the wall. With that off my chest now, here's my configuration details

app.gradle

apply plugin: 'com.android.application'
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
    minSdkVersion 19
    targetSdkVersion 26
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        minifyEnabled false
        debuggable true
        useProguard false
    }
}
dexOptions {
    javaMaxHeapSize "4g"
    preDexLibraries = false
}

dependencies {
    ...
    implementation 'com.android.support:multidex:1.0.2'
}
apply plugin: 'com.google.gms.google-services'

The custom MyApp class

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

Looking forward to what this plentiful community of talents will have to enrich me and other fellow folks suffering from this same epidemic disease. Thank you!

EDIT: Totally forgot to paste the error log. This is what I'm faced with:

What went wrong:

Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

> java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

1
Whats the error you are getting.Post the logs - Farmaan Elahi
My bad, I've updated the question with the error log I'm getting. Thanks. - Xhezairi
@Xhezairi The log error you provided is too generic. It just says what happened, but not why. Look for a root reason. For example, one of the root reasons for this is Multiple dex files define {class-name}. - Alex Lipov
You're right @AlexLipov, I did stumble upon this comment github.com/fullstackreact/react-native-firestack/issues/… and gave me a clue. Posting an answer below for future reference if that's the appropriate approach to conclude this quest. - Xhezairi

1 Answers

1
votes

Ok, I guess I could endure a little longer with my research, but I almost felt hopeless. I finally managed to resolve the issue, so I'm posting it for future reference nevertheless, since I realize there's a lot of us who actually struggle with enabling MultiDexing in our apps.

My actual problem was duplicate versions of Google Play Service libraries. I'm using Facebook Audience Network SDK and it relies on com.google.android.gms:play-services-ads:10.0.4, whilst my Firebase implementation depends on 11.2.2.

The thing is that unless you remind yourself to look in detail for what dependencies your libraries depend on, you really have no way to figure this out because just like @AlexLipov said above "the error log is too generic". My bulb lightened up from this comment on GitHub.

To conclude, basically follow the official MultiDex guide, then if similar problem pop up, give a peak to identify potential dependency conflicts and then do the following to force them use the recent version accordingly.

implementation('com.facebook.android:audience-network-sdk:4.25.0') {
    exclude group: 'com.google.android.gms'
}

A final note worthy of mention, running gradle --stacktrace helps narrow down problems quite a bit. This might come in handy too.

P.S. Thanks and Alex for chiming in.