Home > Net >  Relase aar doesn't contain any code android?
Relase aar doesn't contain any code android?

Time:04-02

I am using android studio bumble bee I created a module library which has a single class

class Lib2 {

fun print(){
    println("From lib2")
}
}

The proguard as

-keepclasseswithmembers class com.example.lib2.Lib2{
    public <methods>;
    public <fields>;
}

I create the release aar with minifyEnabled true

However when I integrate the aar with my app module I am not getting Lib2. What is wrong here?

CodePudding user response:

From documentation:

Specifies classes and class members to be preserved, on the condition that all of the specified class members are present. For example, you may want to keep all applications that have a main method, without having to list them explicitly.

In English it means "Don't obfuscate classes that have the following methods and fields structure".

For example, keeping all classes which have constructors with single Context parameter:

-keepclasseswithmembers class * { 
    public <init>(android.content.Context); 
} 

Since, from my understanding, you simply want to keep the whole class, you can use:

-keep public class com.example.lib2.Lib2

In my opinion, it's better to use @Keep annnotation directly in the class, because:

  1. The proguard rules is decoupled from source and as the time flies we forget that the rule is there. by using @Keep you know immediately what classes are being kept from obfuscation and it's much more readable.
  2. This annotation is packed with your AAR so when you publish it, consumer apps won't obfuscate it as well. With proguard rules you must add another rules file to pack with your AAR that "tells" the consumer app to not obfuscate this class.

Much more simple:

@Keep
class Lib2 {

    fun print(){
        println("From lib2")
    }

}
  • Related