UPDATE: I submitted a patch that fixes the issue and it was accepted. The bug should be fixed starting with build tools 26.0.0. This means that the workaround described here is not necessary when using build tools 26.0.0 or later.


This blog post will be short, because this issue does not affect the majority of Android developers, since it is related to Maven.

The problem

The default build system for Android development is Gradle for a long time now. However, there may be legacy projects that are based on Maven. These projects typically use the Android Maven Plugin.

Using the plugin, one can run into this issue when building a multi-dex application.

The solution

The root cause of the issue is explained in the bug report:

  • if the –disable-annotation-resolution-workaround switch is not used,
  • the mainDexClasses.bat will pass wrong parameters to the MainDexListBuilder
  • it will build a corrupted class list
  • finally, the APK will be corrupted and will not install / run on older devices.

Google did not manage to fix this – maybe they did not have enough resources for this. However, the suggestion proposed by the bug report’s opener worked, so I decided to fix and commit it into the Android Open Source Project. The change has been accepted, and the commit is in the code base now. The fix is still not included in the latest release of the Build Tools, but it will probably arrive with the version that comes with Android O, because it is on the o-preview branch.

The explanation

If you check the commit, you can see that the fix is as simple as removing the quotes from a parameter. If the quotes are there and the workaround option is not requested for a build, the BAT script will pass and empty String to the MainDexListBuilder class instead of passing nothing. Because of this, the Builder will log the error and terminate without building the dex list. The empty dex list will manifest itself in empty, almost zero-byte-length DEX file and a faulty APK in the end.

Conclusion

Since the bug is in the BAT script, only Windows users are affected. Hopefully, the fix will arrive with Android O, which is almost here. Until then, the BAT script has to be edited manually, based on the commit linked above. The file can be found in the following location:

<ANDROID_SDK_PATH>/build-tools/<BUILD_TOOLS_VERSION>/mainDexClasses.bat

The fix can be tested on this project.