Android 之混淆打包 1.什么是混淆打包。 混淆打包就是在不影响程序的执行的结果的情况下。用其他字符替代了代码中的类名,变量名,方法名等等。目的就是让反编译的人难以看懂代码。 没有混淆打包的apk被反编译后的代码。这代码真是一目了然啊,和源程序差不多了。 ![](/uploads/202410/10/20074b3421525411.webp) 而混淆打包的apk被反编译后的代码,基本的代码都被a,b,c,d,代替了。想看懂它很难啊。 ![](/uploads/202410/10/20074b3421525411.webp) 2.android混淆打包的简介。 Android已经内置了proguard的混淆打包插件。所以混淆打包就不用另外装插件了。 插件的位置在 android SDK\ools\proguard 下。 3.android混淆打包的工程结构。 ![](/uploads/202410/10/20074b3421525411.webp) 4. project.properties的配置(这个文件创建android项目的时候就自带的) # This file is automatically generated by Android Tools. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # This file must be checked in Version Control Systems. # # To customize properties used by the Ant build system edit # "ant.properties", and override values to adapt the script to your # project structure. # # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. target=android-16 proguard.config=proguard.cfg android.library.reference.1=..\\Apk android.library.reference.2=..\\ChoiceListView06 主要就是proguard.config=proguard.cfg 这句指向向了proguard.cfg这个文件。然后android编译器就会找proguard.cfg这个文件。 5.prguard.cfg的配置 ##--------------------------------------------------------------------------------- ##---------------Begin: proguard configuration common for all Android apps ---------- ##--------------------------------------------------------------------------------- # 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 -keepclasseswithmembernames class * {
native <methods>; } # keep setters in Views so that animations can still work. # see http://proguard.sourceforge.net/manual/examples.html#beans -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 -keepclassmembers class * extends android.app.Activity {
public void *(android.view.View); } # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations -keepclassmembers enum * {
public static **[] values(); public static ** valueOf(java.lang.String); } -keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *; } -keepclassmembers class **.R$* {
public static <fields>; } ##--------------------------------------------------------------------------------- ##---------------End: proguard configuration common for all Android apps ---------- ##--------------------------------------------------------------------------------- ##--------------------------------------------------------------------------------- ##---------------Begin: proguard configuration common for libs---------- ##--------------------------------------------------------------------------------- -libraryjars /libs/GoogleAdMobAdsSdk-6.4.1.jar -libraryjars /libs/libGoogleAnalyticsV2.jar -libraryjars /libs/commons-io-1.4.jar -libraryjars /libs/lzma-4.63-jio-0.93.jar -keep class com.google.ads.** {*; } -keep class com.google.analytics.** {*; } -keep class com.google.android.gms.** {*; } -keep class android.support.** {*; } -keep class org.apache.commons.io.** {*; } -dontwarn com.google.ads.** -dontwarn android.support.** -dontwarn org.apache.commons.io.** ##--------------------------------------------------------------------------------- ##---------------End: proguard configuration common for ad---------- ##--------------------------------------------------------------------------------- 這個配置文件 for android app的那部份都是一樣的,所以只要改變common for libs 的那部份就好了。 (1)-libraryjars /libs/GoogleAdMobAdsSdk-6.4.1.jar 這句是指明了引用了libs/下的GoogleAdMobAdsSdk-6.4.1.jar包 ![](/uploads/202410/10/20074b3421525411.webp) 所以我在里面要设置配置4个lib路径。 (2)-keep class com.google.ads.** {*; } 这句意思是保持com.google.ads 下的所有类的代码都不被混淆。也就是保持原样。 -keep 还可以保持指定的类,方法。不改变。 现在来谈谈为什么有的要保持不变。 <1>java的jni都是通过方法名字去调用本地方法的。如果名字都变了,那java编译器就无法通过方法名去找本地方法了。 要怎么写呢?早在for android app的那部份就有配置这个了,所以不用担心这种情况。 -keepclasseswithmembernames class * {
native <methods>; } <2>使用java的反射找成员变量的时候。 比如gson-2.2.2.jar 这个类库就是通过反射的方式。解析json数据的。如果实体类的里面的字段被混淆了,那它通过反射也找不到这个成员的名字了。 要怎么写呢?只要加上: -keep class org.apache.commons.io.** {*; } 类似这句代码的配置即可。 (3)-dontwarn com.google.ads.** 这句意思是不要提示警告。 (4)具体的用法请参考 prguard官网的http://proguard.sourceforge.net/index.html#manual/usage.html 6.apk混淆打包 和平常的打包方式一样。这里就不累赘了。 ![](/uploads/202410/10/20074b3421525411.webp) |
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/109234.html