Home > Mobile >  Overridden library java class fails during release build with r8
Overridden library java class fails during release build with r8

Time:03-04

Currently I'm using library. Assume it's called com.example. And it has a file com.example.File.java which I have overridden by creating my own File.java under same path of com.example in my app project. It works as expected with debug builds, where I successfully inject my own version of File.java. But when I try to build release using r8 I get error:

Type com.example.File$1 is defined multiple times: <...>

So I tried using android packaging options:

packagingOptions {
    exclude 'com/example/**'
}

In hopes of at least completely removing mentioned file (and all others in that path), but I still get same error. How to properly exclude file from packagingOptions or is this not the right place for that?

CodePudding user response:

R8 does not allow multiple definitions of the same class in the input to its compilation. AGP will pass all app classes together with all direct dependencies and their transitive dependencies to R8 producing this error. If you want to replace a class in a library your best option is to download that library as a JAR, remove the class files you want to replace and provide that specific JAR as a file dependency. Remember to then also add any direct dependencies of that library as explicit dependencies.

There might be a way to filter out parts of a dependency in Gradle, but I have looked for it before, and did not find any other way to accomplish this. Basically what you would like to be able do is to specify a dependency (JAR/AAR) and then be able to filter out specific .class files from it.

  • Related