Search code examples
androidbuildgradleproguardandroid-build

Android Gradle build resulting apk contains both obfuscated and non-obfuscated classes


When building my android project, I have added the following to the build.gradle file to enable proguard:

   buildTypes {
     release {
        runProguard true
        proguardFile 'proguard-project.txt'
        proguardFile '../common/proguard-shared.txt'
        proguardFile getDefaultProguardFile('proguard-android.txt')
     }
   }

Everything builds okay BUT when I disassemble the resulting dex file, it turns out that both the obfuscated and non-obfuscated files are there.

For example, both common.Base64 and common.a exist, the first is non-obfuscated, while the second is.

Not sure its related, but the project itself has a non-typical structure. This is a result of us having a large android code base with more than 40 android apps. We are trying to create a gradle based build flow side-by-side of existing eclipse based build.

If all goes well, we intend to change the file structure to be more native gradle, and start using flavours and build-types to have-away with many of the libraries we created to accommodate for the lack of flavours and such.

Project E above relies on a chain of libraries like that:

E -> D -> C -> B -> A

e.g. The E project depends on the library D which depends on library C ... all the way up to A.


Solution

  • After looking into this, I found out that this is a problem if you first build without proguard enabled and then build it with it enabled. This is due to the incremental mode of dex.

    You can do a clean build after enabling proguard and it'll fix this.

    Edit: I previously indicated that you can disable incremental mode in dex, but it turns out that actually doesn't help!