To create a new project, the Android studio default turns off the code obfuscation switch in the Build.gradle file, as shown in the minifyenabled switch, so if you need to confuse the code, change false to True, Then add the specific obfuscation rules to the file Proguard-rules.pro.
The buildtypes part of your own Build.gradle file
buildtypes {debug {Buildconfigfield"Boolean", "Log_debug", "true"minifyenabledfalsezipalignenabledtrueshrinkresourcestrue} release {Buildconfigfield"Boolean", "Log_debug", "false"minifyenabledtruezipalignenabledtrueshrinkresourcestrueproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro ' } }
Common obfuscation rules are as follows:
-dontskipnonpubliclibraryclasses # do not ignore non-public library classes
-optimizationpasses 5 # Specify the compression level of the Code-dontusemixedcaseclassnames # whether to use case blending-dontpreverify # Whether to make a pre-check-verbose # Confusion when the log is logged
-keepattributes *annotation* # Keep Annotations-ignorewarning # Ignore warnings
-dontoptimize # Optimization of class files that do not optimize input
-optimizations!code/simplification/arithmetic,!field/*,!class/merging/* # algorithm used to confuse
#保持哪些类不被混淆-keep public class * extends Android.app.activity-keep public class * extends Android.app.application-k EEP public class * extends Android.app.service-keep public class * extends Android.content.broadcastreceiver-keep public class * Extends Android.content.contentprovider-keep public class * extends Android.app.backup.BackupAgentHelp Er-keep public class * extends Android.preference.preference-keep public class Com.android.vending.licensing.ILic Ensingservice
#生成日志数据, the Gradle build is output at the root of this project
-dump Class_files.txt #apk包内所有class的内部结构
-printseeds Seeds.txt #未混淆的类和成员
-printusage Unused.txt #打印未被使用的代码
-printmapping Mapping.txt #混淆前后的映射
-keep public class * extends android.support.** #如果有引用v4或者v7包 to be added
-libraryjars Libs/xxx.jar #混淆第三方jar包, where xxx is the jar package name
-keep class com.xxx.**{*;} #不混淆某个包内的所有文件
-dontwarn com.xxx** #忽略某个包的警告
-keepattributes Signature #不混淆泛型
-keepnames class * Implements Java.io.Serializable #不混淆Serializable
-keepclassmembers class * *. r$* {#不混淆资源类
public static <fields>;
}-keepclasseswithmembernames class * {# Keep Native method not to be confused native <methods>;} -keepclasseswithmembers class * {# Keep custom control classes from being confused with public <init> (Android.content.Context, Android.util.Attribu Teset);} -keepclasseswithmembers class * {# Keep custom control classes from being confused with public <init> (Android.content.Context, Android.util.Attribu Teset, int);} -keepclassmembers class * extends Android.app.Activity {# Keep the custom control class from being confused with public void * (Android.view.View);} -keepclassmembers enum * {# keep Enum enum class not confused public static **[] values (); public static * * VALUEOF (java.lang.String);} -keep class * Implements android.os.Parcelable {# keep Parcelable not be confused public static final Android.os.Parcelab Le$creator *;}
Android Studio Code Obfuscation