android studio 3.1 警告:规则`-keep public class *extends java.lang.annotation.Annotation {

android studio 3.1 Warning: The rule `-keep public class *extends java.lang.annotation.Annotation {

我最近升级了 android 工作室,但我找不到 android 工作室 3.1 中报告的以下问题的来源:

Warning: The rule `-keep public class *extends java.lang.annotation.Annotation {

警告似乎被切断,可能缺少信息。但它看起来像一个 proguard 问题,尽管我在构建调试变体时收到此警告。我检查了我的 proguard 文件,但没有与它完全匹配的行。我搜索了整个项目。关于根本原因的任何想法?

通配符 * 和关键字 extends 之间缺少 space。 警告本身可能不是来自 ProGuard,而是来自 google.

的内置收缩器

如果您在您的项目中找不到它,那么它很可能是来自依赖 aar 文件中包含的消费者 Proguard 文件的违规规则。

我从应用程序的构建 gradle defaultConfig 中删除了 "multiDexEnabled true" 并且警告消失了:

defaultConfig {
    ...

    //multiDexEnabled true
}

dexOptions {
    javaMaxHeapSize "Xg"
}

祝你好运)

正如@arcone1、@Vincent Mattana 在问题评论中提到的以及@random 确认的那样,该问题已在 Android Studio 3.2 中得到解决。

来自issue in Google Issue Tracker

To clarify, this is a warning, not an error, from R8, which we use to compute the list of classes for the main dex, in legacy multidex variant. It does not affect the output, and it should not cause build nor runtime failures.
I am working on a fix to change this keep rule to "-keep public class * implements java.lang.annotation.Annotation", which is semantically the same, and removes the warning.

所以,暂时忽略它或 go bleeding edge with Canary(后果自负)。

更新3.2 is out

Class android.support.annotation.Keep是我用的
(现在称为 androidx.annotation.Keep)。

-keep @interface android.support.annotation.Keep
-keep @android.support.annotation.Keep class *
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

还有更多的标志来控制要保留哪些注释:

-keepattributes RuntimeVisibleAnnotations
-keepattributes AnnotationDefault
-keepattributes *Annotation*

可以在终端选项卡中通过 运行 ./gradlew assembleRelease 获得原始输出。

当项目的 ProGuard 配置中没有任何内容涉及 Annotation 时,此警告可能源自某些引用库的“消费者”规则,在构建时进行混淆。

因此它似乎是一个无害的警告,可以将其静音:

-dontwarn java.lang.annotation.Annotation

由于 gradle defaultConfig 中的 "multiDexEnabled true" 设置,我遇到了同样的问题。

我通过添加 multidex 依赖解决了这个问题 "implementation 'com.android.support:multidex:1.0.3'"

android {
defaultConfig {
    ...
    multiDexEnabled true
}
...
}

dependencies {
  implementation 'com.android.support:multidex:1.0.3'
}

参考: https://developer.android.com/studio/build/multidex

  1. 打开 build.gradle 文件。
  2. 删除“multiDexEnabled true”。
  3. 重建应用程序。

Gradle版本:3.1.4

MultiDex:启用

就我而言,我忘记为某些字符串资源添加翻译。添加后没有error/warning

当您在 google Play 商店上传新的升级版本并且大多数用户在上传后点击保留然后提交时会出现此问题。不要点击保留只是上传和提交。您的 apk 文件上传成功,保留文件自动丢弃并设置为停用模式。

记住:确保您放置了与旧版本不同的新更新的所有详细信息。

我的 gradle 版本是我升级到 com.android.tools.build:gradle:3.3.2 的问题,错误消失了。

在应用程序依赖性中使用了实现'com.android.support:support-annotations:27.1.1'

版本根据你的app兼容版本选择

在我的例子中,警告是在更改 Gradle 版本后出现的。一旦我使缓存失效并重新启动 Android Studio,警告就消失了。

从菜单:文件 > 使缓存无效并重新启动

invalidate caches & restart