启用R8构建Release APK时缺失com.google.errorprone.annotations.Immutable类的解决方案咨询
遇到的问题
我在启用R8构建Release APK时,收到了以下错误提示:
Task :app:minifyReleaseWithR8
AGPBI: {"kind":"warning","text":"Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in C:\Users\y509118\AndroidStudioProjects\SafeBox\app\build\outputs\mapping\release\missing_rules.txt.\n","sources":[{}]}
AGPBI: {"kind":"warning","text":"Missing class com.google.errorprone.annotations.Immutable (referenced from: com.google.crypto.tink.KeyTemplate and 4 other contexts)","sources":[{}],"tool":"R8"}
Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in C:\Users\y509118\AndroidStudioProjects\SafeBox\app\build\outputs\mapping\release\missing_rules.txt.
Missing class com.google.errorprone.annotations.Immutable (referenced from: com.google.crypto.tink.KeyTemplate and 4 other contexts)
同时生成的missing_rules.txt文件内容如下:
# Please add these rules to your existing keep rules in order to suppress warnings. # This is generated automatically by the Android Gradle plugin. -dontwarn com.google.errorprone.annotations.Immutable
我的疑问是:应该在ProGuard规则中添加该-dontwarn指令,还是需要添加对应的-keep规则?
解决方案
别纠结,直接添加生成的-dontwarn com.google.errorprone.annotations.Immutable规则就完全可以解决问题,原因如下:
- 这个缺失的类是编译期注解:
com.google.errorprone.annotations.Immutable是ErrorProne库提供的编译检查注解,它只在代码编译阶段生效,完全不影响APK的运行逻辑。Tink库只是在代码里标记了这个注解,但你的项目并没有引入ErrorProne相关依赖,所以R8才会报找不到类的警告。 -keep规则在这里没用:-keep的作用是保留指定类/方法不被R8混淆或移除,但这个类本身就不存在于你的项目依赖和最终APK中,添加-keep不仅解决不了问题,反而可能引发额外的错误。-dontwarn正好匹配场景:这个指令的作用就是告诉R8“我知道这个类找不到,不用给我发警告了”,完全契合当前情况——因为缺失的类不影响运行,只是个编译期注解的引用而已。
你只需要把missing_rules.txt里的那行-dontwarn指令复制到你的ProGuard配置文件(比如proguard-rules.pro)中,重新构建Release包就能消除这个警告了。
内容的提问来源于stack exchange,提问作者Nitin Verma

