Android : proguard-android.txt 解析

本文详细解析了androidsdk中提供的默认混淆配置文件proguard-android.txt,包括各规则的作用及应用场景,如保留注解、特定类及方法等,并指出实际使用时常需自定义大量规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android sdk 在目录 \sdk\tools\proguard\proguard-android.txt 下提供了默认的混淆配置,下面将介绍每一条规则的作用。

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

# 包名不混合大小写
-dontusemixedcaseclassnames
# 不忽略非公共的库类
-dontskipnonpubliclibraryclasses
# 输出混淆日志
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
# 不进行优化
-dontoptimize
# 不进行预检验
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

# 不混淆注解(注解不能被混淆)
-keepattributes *Annotation*
# 不混淆指定类
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
# 不混淆native的方法
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
# 不混淆继承View的所有类的set和get方法
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
# 不混淆继承Activity的所有类的中的参数类型为View的方法
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
# 不混淆枚举类型的values和valueOf方法
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# 不混淆继承Parcelable的所有类的CREATOR
-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

# 不混淆R类中所有static字段
-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
# 忽略兼容库的所有警告
-dontwarn android.support.**

# Understand the @Keep support annotation.
# 不混淆指定的类及其类成员
-keep class android.support.annotation.Keep

# 不混淆使用注解的类及其类成员
-keep @android.support.annotation.Keep class * {*;}

# 不混淆所有类及其类成员中的使用注解的方法
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

# 不混淆所有类及其类成员中的使用注解的字段
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

# 不混淆所有类及其类成员中的使用注解的初始化方法
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

从上面的解析可以看出,proguard-android.txt 中只提供了基本的内容,在实际使用 ProGuard 时通常需要配置大量的规则。例如,引用的第三方库的混淆配置,不混淆自定义控件,不混淆反射的类等。


参考:

plugins { id 'com.android.application' } android { compileSdkVersion 30 buildToolsVersion "30.0.2" defaultConfig { applicationId "com.app.test" minSdkVersion 18 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } viewBinding { enabled = true } } dependencies { implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'com.google.android.material:material:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.annotation:annotation-jvm:+' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'com.contrarywind:Android-PickerView:4.1.9' implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.google.android.material:material:1.0.0' implementation 'io.github.youth5201314:banner:2.2.2' //数据解析 implementation 'com.google.code.gson:gson:2.8.9' //图片加载 implementation 'com.github.bumptech.glide:glide:4.16.0' //网络请求 implementation 'com.squareup.okhttp3:okhttp:4.11.0' implementation'androidx.recyclerview:recyclerview-selection:1.1.0' }
03-13
build.gradle(app):apply plugin: 'com.android.application' android { compileSdkVersion 34 defaultConfig { applicationId "com.hik.netsdk.SimpleDemo" minSdkVersion 21 targetSdkVersion 34 versionCode 11 versionName "3.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true multiDexEnabled true ndk { abiFilters "armeabi-v7a","arm64-v8a" } configurations.all { resolutionStrategy { // 强制使用统一版本的 OkHttp force 'com.squareup.okhttp3:okhttp:4.9.1' // 解决可能的冲突 eachDependency { details -> if (details.requested.group == 'com.squareup.okhttp3') { details.useVersion '4.9.1' } } } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main { res.srcDirs = [ 'src/main/res', 'src/main/res/layout/DevMgtUI', 'src/main/res/layout/BusinessUI' ] jniLibs.srcDirs = ['libs'] } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } // 添加的 aaptOptions 配置 aaptOptions { cruncherEnabled = false // 禁用 PNG 压缩检查 additionalParameters '--warn-manifest-validation' // 启用清单验证警告 } // 添加的 packagingOptions 配置 packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/license.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/notice.txt' exclude 'META-INF/ASL2.0' } buildToolsVersion = '28.0.3' } dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'io.github.ezviz-open:ezviz-sdk:5.20' implementation 'com.ezviz.sdk:ezuikit:2.2.1' implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.squareup.okhttp3:okhttp:4.9.1' implementation "androidx.media3:media3-exoplayer:1.3.1" implementation "androidx.media3:media3-exoplayer-hls:1.3.1" implementation "androidx.media3:media3-ui:1.3.1" implementation "androidx.media3:media3-datasource-okhttp:1.3.1" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4' implementation "androidx.media3:media3-common:1.3.1" } 依据上述依赖解决rebuild project后的报错:Duplicate class org.MediaPlayer.PlayM4.Player$PRIVATE_RENDER found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.Player$PTZ_PARAM found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.Player$SESSION_INFO found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.Player$SRTRANS_ELEMENT found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.Player$SRTRANS_PARAM found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.Player$TEM_FLAG found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerAdditionalCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerAudioDataCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerDecodeCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerDecodeCBEx found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerDisplayCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerDisplayCBEx found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerEncTypeChgCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerEncryptTypeCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerFECDisplayCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerFileRefCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerHSDetectCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerIVSDrawFunCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerPlayEndCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerPreRecordCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerPreRecordCBEx found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.PlayerCallBack$PlayerRunTimeInfoCB found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.SurfaceCallBack found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Duplicate class org.MediaPlayer.PlayM4.TimeStruct found in modules jetified-ezviz-sdk-4.9.1.1-runtime (com.ezviz.sdk:ezviz-sdk:4.9.1.1) and jetified-ezviz-sdk-5.20-runtime (io.github.ezviz-open:ezviz-sdk:5.20) Go to the documentation to learn how to Fix dependency resolution errors.
06-23
<?xml version="1.0" encoding="utf-8" standalone="no"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.dw.Dating.lua" platformBuildVersionCode="25" platformBuildVersionName="7.1.1"> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true"/> <uses-feature android:glEsVersion="0x20000"/> A <uses-permission android:name="android.permission.INTERNET"/> <uses-feature android:name="android.hardware.touchscreen" android:required="false"/> <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false"/> <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_LOGS"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.webkit.permission.PLUGIN"/> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> <uses-permission android:name="android.permission.VIBRATE"/> <meta-data android:name="android.support.VERSION" android:value="25.3.1"/> <application android:hardwareAccelerated="false" android:icon="@drawable/app_icon" android:isGame="true" android:label="@string/app_name" android:name="com.dw.Dating.wxapi.App"> <meta-data android:name="URL_VALUE" android:value="http://192.168.111.88"/> <meta-data android:name="CHANNEL" android:value="AgentID-0"/> <meta-data android:name="FW_VALUE" android:value="FW-30855784"/> <meta-data android:name="AppID" android:value="wx401c37330dd3e92e"/> <activity android:configChanges="locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode" android:label="@string/app_name" android:launchMode="singleTask" android:name="com.dw.Dating.wxapi.MainActivity" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LEANBACK_LAUNCHER"/> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true"/> <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false"/> </activity> <activity android:configChanges="keyboardHidden|orientation|screenSize" android:launchMode="singleTask" android:name="com.fanwei.jubaosdk.cashier.CashierActivity" android:theme="@style/FanweiDialogActivityTheme"/> <activity android:configChanges="keyboardHidden|orientation|screenSize" android:launchMode="singleTop" android:name="com.fanwei.jubaosdk.wap.WapActivity" android:screenOrientation="portrait" android:theme="@style/FanweiActivityTheme"/> <provider android:authorities="com.dw.Dating.lua.fileProvider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths_fanwei"/> </provider> <activity android:name="com.pay.sdk.usage.PayActivity" android:screenOrientation="portrait"/> <activity android:name="sdk.pay.PayWebViewActivity" android:screenOrientation="portrait"/> <activity android:name="com.switfpass.pay.activity.QQWapPayWebView" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar"/> <activity android:exported="true" android:launchMode="singleTop" android:name="com.dw.Dating.lua.wxapi.WXEntryActivity" android:theme="@android:style/Theme.NoDisplay"/> <meta-data android:name="UMENG_APPKEY" android:value="111"/> <meta-data android:name="UMENG_CHANNEL" android:value="Test"/> </application> </manifest> 根据这个数据生成完整的AndroidManifest.xml
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值