Obfuscating .aar files

I created an .aar file (containing resources and drawings) of an Android library project using

./gradlew assemble 

I turned on obfuscation by setting minify == true

 buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } 

However, when I run the specified gradle command with minify enabled = true, I get java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options? java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options?

What does this error indicate and how can I confuse the .aar library file?

Best wishes

+8
android android-studio obfuscation proguard
source share
4 answers

Proguard cuts unused classes. Libraries are standalone products and have some specific entry points that should not be tricked. Therefore, you need to add rules to save these entry points. The rules look like this:

 -keep class packagename {public *;} 
+3
source share

Using Proguard worked for me!

Proguard is used for compression, obfuscation, code optimization. Proguard is necessary for your library to remove unused code and make reverse engineering a little complicated. The Proguard rules for the library are different from regular applications. As you know, Proguard renames classes, variables, and methods that use meaningless names. You would like to keep the names of these methods and classes as this is what the developers will call. You will need to verify and verify the confusing code from the generated AAR file.

Library module build.gradle of your library

 buildTypes { release { // Enables code shrinking, obfuscation, and optimization for only // your project release build type. minifyEnabled true // Includes the default ProGuard rules files that are packaged with // the Android Gradle plugin. To learn more, go to the section about // R8 configuration files. proguardFiles getDefaultProguardFile( 'proguard-android-optimize.txt'), 'proguard-rules.pro' } } 

Inside your proguard-rules.pro your library

 # Save the obfuscation mapping to a file, so we can de-obfuscate any stack # traces later on. Keep a fixed source file attribute and all line number # tables to get line numbers in the stack traces. # You can comment this out if you're not interested in stack traces. -printmapping out.map -keepparameternames -renamesourcefileattribute SourceFile -keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,EnclosingMethod # Preserve all annotations. -keepattributes *Annotation* # Preserve all public classes, and their public and protected fields and # methods. -keep public class * { public protected *; } # Preserve all .class method names. -keepclassmembernames class * { java.lang.Class class$(java.lang.String); java.lang.Class class$(java.lang.String, boolean); } # Preserve all native method names and the names of their classes. -keepclasseswithmembernames class * { native <methods>; } # Preserve the special static methods that are required in all enumeration # classes. -keepclassmembers class * extends java.lang.Enum { public static **[] values(); public static ** valueOf(java.lang.String); } # Explicitly preserve all serialization members. The Serializable interface # is only a marker interface, so it wouldn't save them. # You can comment this out if your library does not use serialization. # If your code contains serializable classes that have to be backward # compatible, please refer to the manual. -keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); } # The library may contain more items that need to be preserved; # typically classes that are dynamically created using Class.forName: # -keep public class mypackage.MyClass # -keep public interface mypackage.MyInterface # -keep public class * implements mypackage.MyInterface 

Thanks .... link

https://dev.to/mohitrajput987/develop--publish-your-own-sdk-in-android---part-2getting-started-with-sdk-development-3159

+1
source share

Two suggestions:

  • Please make sure that this file exists in the library module: proguard-rules.pro.
  • Try running "./gradlew: mylib: assembleRelease" using "mylib" will be replaced with your library module name.
0
source share
  • Copy the library.pro file into the library project from this location:
     ...\android-sdk\tools\proguard\examples 
  • Comment on these lines when creating from Android Studio, probably it should be stored / updated when creating from the command line:
     -injars in.jar -outjars out.jar -libraryjars /lib/rt.jar 
  • Update the build.gradle library library build.gradle to use library.pro :

     release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'library.pro' } 
  • Synchronize and create the project, and now it should generate a tangled AAR file.

0
source share

All Articles