ythy / blog

Give everything a shot
6 stars 0 forks source link

Android ProGuard #540

Open ythy opened 2 weeks ago

ythy commented 2 weeks ago

Android 升级到 30+以后, proguard 混淆以后打包运行报错。 报错 1. 空对象 报错 2. 未实现对应接口

根据官网说明,升级使用方法如下: AGP version 4.x - AGP 7.x 需要使用 ProGuard Gradle Plugin

  1. You can add the ProGuard plugin to your project by including the following in your root level build.gradle(.kts) file:
  dependencies {
      classpath 'com.android.tools.build:gradle:x.y.z'    // The Android Gradle plugin.
      classpath 'com.guardsquare:proguard-gradle:7.1.0'  // The ProGuard Gradle plugin.
  }
  1. To actually apply the plugin to your project, just add the line to your module level build.gradle(.kts) file after applying the Android Gradle plugin as shown below.
 apply plugin: 'com.android.application'
 apply plugin: 'com.guardsquare.proguard'
  1. ProGuard expects unobfuscated class files as input. Therefore, other obfuscators such as R8 have to be disabled.
buildTypes {
       release {
          // Deactivate R8.
          minifyEnabled false
       }
    }

ProGuard can be executed automatically whenever you build any of the configured variants. You can configure a variant using the proguard block in your module level build.gradle(.kts) files. This is a top-level block and should be placed outside the android block.

For example, in the snippet below, ProGuard is configured to only process the release variant of the application, using a configuration provided by the user (proguard-project.txt) and a default configuration (proguard-android-optimize.txt).

android {
    ...
}

proguard {
   configurations {
      release {
         defaultConfiguration 'proguard-android-optimize.txt'
         configuration 'proguard-project.txt'
      }
   }
}

There are three default configurations available:

DEFAULT CONFIGURATION DESCRIPTION
proguard-android.txt ProGuard will obfuscate and shrink your application
proguard-android-optimize.txt ProGuard will obfuscate, shrink and optimize your application
proguard-android-debug.txt ProGuard will process the application without any obfuscation, optimization or shrinking
ythy commented 2 weeks ago

reference : https://www.guardsquare.com/manual/setup/gradleplugin