go to index

Android打包提示Missing classes detected while running R8 错误解决方案

read time 2 min read
Android 混淆 R8 安卓

概述

在使用 R8 进行代码混淆和压缩时,有时会遇到以下错误提示:

plaintext
Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in your path to missing_rules.txt.

该错误表示 R8 在处理代码时检测到某些类缺失,导致混淆和压缩失败。本文将介绍如何解决这一问题。

解决办法

步骤一:查找生成的 missing_rules.txt 文件

  1. 定位文件路径
    • 在项目目录下找到 app/build/outputs/mapping/release/missing_rules.txt 文件。
  2. 查看文件内容
    • 该文件中包含了类似以下的规则建议:

      plaintext
      -dontwarn android.os.ServiceManager
      -dontwarn com.bun.miitmdid.core.MdidSdkHelper
      -dontwarn com.bun.miitmdid.interfaces.IdSupplier
      -dontwarn com.google.firebase.iid.FirebaseInstanceId
      -dontwarn com.google.firebase.iid.InstanceIdResult
      -dontwarn com.tencent.android.tpush.otherpush.OtherPushClient
      

步骤二:将规则添加到 proguard-rules.pro

  1. 打开 proguard-rules.pro 文件

    • 通常位于项目的 app 目录下。
  2. 复制并粘贴规则

    • missing_rules.txt 中的规则复制到 proguard-rules.pro 文件中。
  3. 保存文件

    • 确保所有修改已保存。

示例

假设 missing_rules.txt 文件内容如下:

plaintext
-dontwarn android.os.ServiceManager
-dontwarn com.bun.miitmdid.core.MdidSdkHelper
-dontwarn com.bun.miitmdid.interfaces.IdSupplier
-dontwarn com.google.firebase.iid.FirebaseInstanceId
-dontwarn com.google.firebase.iid.InstanceIdResult
-dontwarn com.tencent.android.tpush.otherpush.OtherPushClient

你需要将这些规则添加到 proguard-rules.pro 文件中:

plaintext
# proguard-rules.pro

# 忽略特定类的警告
-dontwarn android.os.ServiceManager
-dontwarn com.bun.miitmdid.core.MdidSdkHelper
-dontwarn com.bun.miitmdid.interfaces.IdSupplier
-dontwarn com.google.firebase.iid.FirebaseInstanceId
-dontwarn com.google.firebase.iid.InstanceIdResult
-dontwarn com.tencent.android.tpush.otherpush.OtherPushClient

注意事项

  • 保持一致性:确保 proguard-rules.pro 文件中的规则与 missing_rules.txt 中的一致。
  • 测试构建:添加规则后,重新构建项目以验证问题是否解决。
  • 依赖库更新:如果问题依然存在,检查是否有依赖库需要更新或配置调整。