问题
I'm writing 2 Android libraries. When I obfuscate both, the obfuscated code in both of them contains a class named a.a.a.a.a which causes the following error when trying to use both libraries in the same application:
Duplicate class a.a.a.a.a found in modules classes.jar (lib1) and classes.jar (lib2)
How can I prevent Proguard from obfuscating the first 3 packages to end up with:
my.domain.lib1.a.a and my.domain.lib2.a.a?
Edit: The obfuscation is happening as part of building the libraries, not while building the application.
回答1:
This means at runtime your dependencies of both libraries results as the same name given by proguard.
To avoid this issue, try using like below:
android {
...
configurations {
all*.exclude module: 'support-v4'
// This removes all other versions of `support-v4`
// if it gets duplicated from all the artifacts.
}
...
}
I have faced this with v4... It can be something from others with your code.. but majorly this occurs with v4.
For Further knowledge: https://discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991
回答2:
This can be resolved by putting -repackageclasses my.domain.lib#.ofs in the proguard-rules file of each library while replaceing # with 1 and 2 respectivly. This will move all the obfuscated classes into the my.domain.lib#.ofs package while all the non-obfuscated classes will remain in their original packages and you're guaranteed to have no collisions.
As the Proguard documentation states:
-repackageclasses [package_name]
Specifies to repackage all class files that are renamed, by moving them into the single given package.
Another solution is to use -keeppackagenames. Unfortunately, I couldn't find a way to make it keep only the first 3 packages.
See the Proguard documentation:
-keeppackagenames [package_filter]
Specifies not to obfuscate the given package names.
来源:https://stackoverflow.com/questions/59712878/android-library-proguard-package-obfuscation-produces-a-a-a-a-a-collision