Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove external library from obfuscated classes.jar (in Android Studio / Gradle)

I created an android library (using Android Studio), which imports an external .jar file (containing only interfaces) in order to build correctly. But the final library file should not include this file, as the main application project will include a .jar file that already contains these interface classes contained in the external library. If I leave the file inside, the compiler will complain with "multiple dex files define...".

I can automatically remove the external .jar library from the .aar with gradle like this:

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->
        def packageLib = output.getPackageLibrary()
        packageLib.exclude('libs/externalLibrary.jar')
    }
}

But if the library is obfuscated using ProGuard (minifyEnabled true), then all class files from the external library are added to classes.jar before packaging it into the .aar file, and therefore can't be excluded this way.

I can edit the classes.jar file and remove files from it automatically using gradle like this:

task makeRelease(type: Jar) {
    baseName='changedClasses'
    destinationDir=file('../../release/')

    from zipTree('build/intermediates/bundles/release/classes.jar')
    exclude "com/externallibrary/**"
}

which works when running this task after finishing the build of the library. But I would need to run this task after the proguard task has finished, but before the files are packaged into the final .aar in order to automatically build and run the final app. And I haven't found any way to do this so far...

Therefore: How can I automatically remove files from the classes.jar that is included into the final .aar file? Or maybe I'm going at this the wrong way, is there another way to solve my problem, like telling proguard to not include the files in classes.jar?

like image 556
snarp Avatar asked Nov 10 '22 20:11

snarp


1 Answers

in your dependencies, instead of writing "compile" write "provided" which means the thing you are compiling is just for getting a reference and not for packaging.

like image 86
Yakir Yehuda Avatar answered Nov 14 '22 22:11

Yakir Yehuda