android 混淆jar及apk的心得

时间:2021-02-15 16:44:11

混淆android的jar包配置文件proguard.cfg如下

-injars libs\your_library.jar
-outjars libs\your_library_out.jar

-libraryjars 'E:\program_tools\android-sdk\platforms\android-9\android.jar'

-optimizationpasses 5
-dontusemixedcaseclassnames
-ignorewarning
-dontskipnonpubliclibraryclasses
-dontoptimize
-verbose

-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep 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.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}


如上的红色文字非常重要

-injars 表示你要混淆的jar文件

-outjars 是混淆后的jar文件名

-libraryjars 是混淆时需要引用的android库

-ignorewarning 表示混淆时忽略所以的警告

-dontoptimize 不进行优化,这句代码非常重要。并且配置中不能有-dontpreverify字段;-dontpreverify在混淆jar时没有问题但是在apk应用中使用该jar后对apk进行混淆时就会失败。


如果项目中使用了jar库,则proguard.cfg文件的配置如下
-optimizationpasses 2
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep 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.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

上面的配置中 -optimizationpasses 只能为1,2;3以上混淆就会出错!