1

I have a multi-module Android application and when I build the release APK, I get this dex error:

java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/XX/package-info;

I do of course have a package-info file in all my modules and they all have the same name.
Why does dex even care about package-info files, and how can I configure it to ignore them?

Note: I do NOT want to enable multi-dex

TmTron
  • 17,012
  • 10
  • 94
  • 142
  • What are the reasons for having the same package in multiple modules? Would it make more sense to create unique (sub)packages across modules? – Code-Apprentice Mar 25 '17 at 12:59
  • in a layered architecture you have the same package in different modules and thus you can use package-visibility for access-control. – TmTron Mar 25 '17 at 13:01

2 Answers2

0

Try with using packagingOptions inside android tag in build.gradle for app module.

packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/license.txt'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/notice.txt'
}

More: Packaging

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Ready Android
  • 3,529
  • 2
  • 26
  • 40
  • `exclude` seems not to work for `package-info` files - maybe because they are java-source files? – TmTron Mar 25 '17 at 14:31
0

The solution is to exclude the package-info files from the jar files.

Example code to use in build.gradle files

for java-modules:

jar {
    exclude('com/**/package-info.*')
}

for android-modules:

android {
    sourceSets.main.java.filter.exclude 'com/**/package-info.*'
}

Works with:

  • Android Studio 3.0 Canary 5
  • Gradle 4.1-milestone-1
  • Android gradle plugin: 3.0.0-alpha5

Note: you can of course only then exclude the package-info files if they don't contribute to your build!
e.g. when you use immutables style-configuration in your package-info file then you cannot exclude the files from the build, because then the names of the generated files may change!

TmTron
  • 17,012
  • 10
  • 94
  • 142