Proguard 在 proguard-rules.pro 添加包名称后运行 R8 时检测到缺少类

Jim*_*nts 61 android proguard kotlin

AGPBI将此作为输出:

> Task :app:minifyReleaseWithR8
AGPBI: {"kind":"warning","text":"Unexpected reference to missing service class: META-INF/services/reactor.blockhound.integration.BlockHoundIntegration.","sources":[{"file":"/app/build/intermediates/merged_java_res/release/base.jar"}],"tool":"R8"}

AGPBI: {"kind":"warning","text":"Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in /app/build/outputs/mapping/release/missing_rules.txt.\n","sources":[{}]}

AGPBI: {"kind":"warning","text":"Missing class com.aayushatharva.brotli4j.Brotli4jLoader (referenced from: void io.netty.handler.codec.compression.Brotli.<clinit>() and 2 other contexts)\n

Missing class com.aayushatharva.brotli4j.decoder.DecoderJNI$Status (referenced from: void io.netty.handler.codec.compression.BrotliDecoder$1.<clinit>() and 1 other context)\n
Missing class com.aayushatharva.brotli4j.decoder.DecoderJNI$Wrapper (referenced from: com.aayushatharva.brotli4j.decoder.DecoderJNI$Wrapper io.netty.handler.codec.compression.BrotliDecoder.decoder and 4 other contexts)\n
Missing class com.aayushatharva.brotli4j.encoder.Encoder$Mode (referenced from: void io.netty.handler.codec.compression.BrotliOptions.<clinit>())\n
Missing class com.aayushatharva.brotli4j.encoder.Encoder$Parameters (referenced from: com.aayushatharva.brotli4j.encoder.Encoder$Parameters io.netty.handler.codec.compression.BrotliEncoder.parameters and 7 other contexts)\n
Missing class com.aayushatharva.brotli4j.encoder.Encoder (referenced from: io.netty.buffer.ByteBuf io.netty.handler.codec.compression.BrotliEncoder.allocateBuffer(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, boolean))\n
Missing class com.android.org.conscrypt.SSLParametersImpl (referenced from: void org.conscrypt.KitKatPlatformOpenSSLSocketImplAdapter.<init>(org.conscrypt.AbstractConscryptSocket))\n
Missing class com.github.luben.zstd.Zstd (referenced from: io.netty.buffer.ByteBuf io.netty.handler.codec.compression.ZstdEncoder.allocateBuffer(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, boolean) and 1 other context)\n
Missing class com.google.protobuf.ExtensionRegistry (referenced from: void io.netty.handler.codec.protobuf.ProtobufDecoder.<init>(com.google.protobuf.MessageLite, com.google.protobuf.ExtensionRegistry))\n
Missing class com.google.protobuf.ExtensionRegistryLite (referenced from: com.google.protobuf.ExtensionRegistryLite io.netty.handler.codec.protobuf.ProtobufDecoder.extensionRegistry and 2 other contexts)\n
Run Code Online (Sandbox Code Playgroud)

丢失的文件似乎是bouncycastle库的一部分。这是我的proguard文件:

-keep class org.spongycastle.** { *; }
-dontwarn org.spongycastle.**

# When I remove this, java.security.cert.CertificateException: X.509 not found is thrown
-keep class org.bouncycastle.** { *; }
-keep interface org.bouncycastle.**

# I got the missing classes from missing_rules.txt and added the package names that created the problem here:
-keep class org.conscrypt.** { *; }
-keep class io.netty.** { *; }

-keep class com.aayushatharva.** { *; }

-keep class ** { *; }
Run Code Online (Sandbox Code Playgroud)

构建.gradle(应用程序):

implementation "org.conscrypt:conscrypt-android:$conscrypt"
implementation 'org.bouncycastle:bcprov-jdk15on:1.69'
implementation 'org.bouncycastle:bcpkix-jdk15on:1.69'
Run Code Online (Sandbox Code Playgroud)

为什么当我添加正确的缺失包名称时它仍然给出错误?

Ven*_*Ren 122

我将 gradle 升级到8.0打包应用程序时遇到同样的问题。

这是错误消息

运行 R8 时检测到缺少类。请添加缺少的类或应用在您的 Missing_rules.txt 路径中生成的其他保留规则

我按照这样的路径到missing_rules.txt

app -> build -> outputs -> mapping -> your_app_name -> missing_rules.txt
Run Code Online (Sandbox Code Playgroud)

我收到那些消息

# Please add these rules to your existing keep rules in order to 
  suppress warnings.
# This is generated automatically by the Android Gradle plugin.
-dontwarn android.os.ServiceManager*
-dontwarn com.bun.miitmdid.core.MdidSdkHelper*
-dontwarn com.bun.miitmdid.interfaces.IIdentifierListener*
-dontwarn com.bun.miitmdid.interfaces.IdSupplier*
-dontwarn com.google.firebase.iid.FirebaseInstanceId*
-dontwarn com.google.firebase.iid.InstanceIdResult*
-dontwarn com.huawei.hms.ads.identifier.AdvertisingIdClient$Info*
-dontwarn com.huawei.hms.ads.identifier.AdvertisingIdClient*
-dontwarn com.tencent.android.tpush.otherpush.OtherPushClient*
Run Code Online (Sandbox Code Playgroud)

我将这些消息复制到proguard-rules.pro然后解决这个问题。

  • 安全吗?在我看来,Android Studio软件存在很多隐藏的问题。它们时不时地出现,每个响应者都有一个与其他解决方案无关的不同解决方案,它们通常涉及无效缓存或我们提出的任何任意问题,例如臭名昭​​著的红色 R 问题,我每年都会遇到一次,..无论如何,如果问题消失了,我们就可以继续我们的一天——直到下一个问题出现。 (3认同)
  • 我按照上面的指示添加了这些,它们用红色下划线加上“未解析的类名”,无论问题消失:) -dontwarn javax.annotation.Nullable -dontwarn javax.annotation.ParametersAreNonnullByDefault -dontwarn javax.annotation.WillClose (2认同)
  • 这安全吗?我们只是抑制了这里的警告,并没有解决真正的问题 (2认同)
  • @Mick我知道如果你在末尾添加*,这会收到一些警告。警告将会消失。-dontwarn android.os.ServiceManager* -dontwarn com.bun.miitmdid.core.MdidSdkHelper* -dontwarn com.bun.miitmdid.interfaces.IIdentifierListener* -dontwarn com.bun.miitmdid.interfaces.IdSupplier* (2认同)

sgj*_*sse 24

缺少类警告表明所提到的类不存在于输入(您的源和依赖项)或引用的库(android.jar在本例中由 Platfrom SDK 提供的 Android 运行时库)中。当 R8 跟踪程序时,它将尝试处理在它认为处于活动状态的程序部分中找到的所有类、方法和字段。将所有类保留在一个包中不会改变这一点。在这种情况下有两种选择:

  1. 找到缺少的依赖项并将其添加到项目中
  2. 忽略使用 a 的警告,-dontwarn因为该类预计会丢失。

对于班级的警告com.android.org.conscrypt.SSLParametersImpl添加

-dontwarn com.android.org.conscrypt.SSLParametersImpl`
Run Code Online (Sandbox Code Playgroud)

是正确的做法,因为查看源代码表明该类扩展了一个将在 KitKat 运行时中的类,但不在应用程序中(也不在android.jar)中。

对于其余的警告,必须做出类似的决定。


bom*_*mpf 14

不直接是 OP 的问题,但如果您发现 R8 由于缺少规则而导致构建失败,并且您使用的是旧版本的 OkHttp,则可以将 OkHttp 升级到 4.11.0 以获得必要的 Proguard 规则,而不是将它们添加到您的项目中。