Search code examples
androidgradleandroid-multidex

Android dependency included multiple times, causes app to reach dex limit


I have an android app structured like this:

main-app/

dependencies {
    compile project(':lib-A')
    compile project(':lib-B')
}

lib-A/

dependencies {
    compile 'Large3PLib'
    compile 'Other-libs'
}

lib-B/

dependencies {
    compile 'Large3PLib'
}

When I compile I am reaching the multi-dex limit, and I see that main-app, lib-A, and lib-B all have very large dex counts due to them all including 'Large3PLib'. Is there a way I can tell gradle to only include 'Large3PLib' once in order to shrink my dex count?

Notes:

  • I have proguard enabled
  • I've tried combining the code in main-app, lib-A, and lib-B into one large module. This puts me back under the multidex limit, but I don't like this solution as now my code is no longer organized.

Solution

  • You can edit lib-A and lib-B to mark the dependency as 'provided':

    provided 'Large3PLib'
    

    This means that it will not be included in the resulting jar or aar. But then you will have to add it to the root project:

    dependencies {
        compile 'Large3PLib'
        compile project(':lib-A')
        compile project(':lib-B')
    }