使用DexGuard的assumenosideeffects无法移除Android Log问题
问题:使用DexGuard移除Android Log方法失败
我尝试用DexGuard移除Android的Log方法,但生成APK安装到手机后仍能看到日志输出,以下是我的操作配置:
build.gradle(:app)
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'dexguard' android { compileSdkVersion rootProject.compileSdkVersion buildToolsVersion rootProject.buildToolsVersion compileOptions { sourceCompatibility = rootProject.javaSourceCompatibility targetCompatibility = rootProject.javaTargetCompatibility } kotlinOptions { jvmTarget = "1.8" } buildFeatures { viewBinding = true dataBinding = true buildConfig = true } defaultConfig { minSdkVersion xx targetSdkVersion rootProject.targetSdkVersion multiDexEnabled true testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" versionCode VERSION_CODE versionName COMMON_VERSION_NAME } flavorDimensions = ["deploymentType"] productFlavors { local { isDefault = true dimension "deploymentType" } mavenRelease { dimension "deploymentType" } mavenDevelopment { dimension "deploymentType" } } buildTypes { stagingStable { debuggable true minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField BOOLEAN, IS_DEBUG, TRUE buildConfigField BOOLEAN, IS_STAGING_STABLE, TRUE matchingFallbacks = ['development', 'release'] } } packagingOptions { exclude 'META-INF/*.kotlin_module' } } dependencies { implementation "com.squareup.retrofit2:retrofit:$retrofitVersion" implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion" implementation "com.squareup.okhttp3:logging-interceptor:$okHttpVersion" } repositories { mavenCentral() mavenLocal() } dexguard { path = '../../dexguard/DexGuard-9.5.5/' license = 'dexguard/dexguard-license.txt' configurations { localStagingStable{ defaultConfiguration 'dexguard-release.pro' configuration 'dexguard/dexguard-release-sdk.txt' } } }
proguard-rules.pro
# Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in /home/mateobosco/Android/Sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Remove Android logging calls (in this case, including errors). -optimizations code/removal/* -assumenosideeffects public class android.util.Log -assumenosideeffects class android.util.Log { *; }
dexguard-release-sdk.txt
# We'll display some more statistics about the processed code. -verbose # Base rules -include ../../../dexguard/dexguard-rules.pro # Print a full report -printconfiguration ../build/fullconfig.pro # Encrypt the "geopagos" classes #-encryptclasses com.geopagos.** #-encryptclasses com.model.geopagos.** #-encryptclasses com.services.geopagos.** #-encryptclasses com.utils.geopagos.** -optimizations code/removal/* -assumenosideeffects public class android.util.Log -assumenosideeffects class android.util.Log { *; }
问题根源及修复方案
核心问题:minifyEnabled未开启
你的stagingStablebuildType中设置了minifyEnabled false,这会直接禁用DexGuard的代码优化功能——包括-assumenosideeffects规则的执行,只有当minifyEnabled设为true时,DexGuard才会处理这类优化规则。修正步骤
开启minifyEnabled
修改stagingStable的配置:stagingStable { debuggable true minifyEnabled true // 必须设为true,否则DexGuard优化不生效 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField BOOLEAN, IS_DEBUG, TRUE buildConfigField BOOLEAN, IS_STAGING_STABLE, TRUE matchingFallbacks = ['development', 'release'] }优化assumenosideeffects规则
替换现有模糊的规则,使用精确的方法匹配,确保DexGuard能准确识别并移除Log调用:-optimizations code/removal/advanced -assumenosideeffects class android.util.Log { public static int v(...); public static int d(...); public static int i(...); public static int w(...); public static int e(...); public static int wtf(...); }验证配置生效
构建APK时查看控制台输出,确认DexGuard被触发(会有专属日志),同时检查生成的fullconfig.pro文件,确保上述规则已被包含。
内容的提问来源于stack exchange,提问作者Nicote Ool
相关产品推荐
相关产品推荐

