发布 aar 不包含任何代码android?

Relase aar doesn't contain any code android?

我正在使用 android studio bumble bee 我创建了一个模块库,它有一个 class

class Lib2 {

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

proguard 作为

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

我用 minifyEnabled true

创建发布 aar

然而,当我将 aar 与我的应用程序模块集成时,我没有得到 Lib2。这里有什么问题?

来自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.

英文的意思是“不要混淆具有以下方法和字段结构的 classes”。

例如,保留所有具有单个 Context 参数的构造函数的 classes:

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

因为,根据我的理解,你只是想保留整个 class,你可以使用:

-keep public class com.example.lib2.Lib2

在我看来,直接在class中使用@Keep注解比较好,因为:

  1. proguard 规则与源代码分离,随着时间的流逝,我们忘记了规则的存在。通过使用 @Keep,您可以立即知道哪些 classes 没有被混淆,并且它更具可读性。
  2. 此注释包含在您的 AAR 中,因此当您发布它时,消费者应用程序也不会混淆它。使用 proguard 规则,您必须添加另一个规则文件以与您的 AAR 一起打包,该文件“告诉”消费者应用程序不要混淆此 class.

更简单:

@Keep
class Lib2 {

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

}