解决方案:在Proguard.cfg方件中添加以下设定:
- 过滤R文件的混淆:
- 过滤第三方包的混淆:
Android导入第三方jar包,proguard混淆脚本(屏蔽警告,不混淆第三方包)
最近1个项目中 需要导入移动MM的第三方计费包,混淆时用到了如下脚本,可屏蔽警告,不混淆第三方包指定内容。
非常有效
proguard.cfg 文件
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-ignorewarnings //这1句是屏蔽警告,脚本中把这行注释去掉
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
/ /这1句是导入第三方的类库,防止混淆时候读取包内容出错,脚本中把这行注释去掉
-libraryjars libs/mmbilling.jar
-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>;
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
//这4句是不混淆第三方包中的指定内容,脚本中把这行注释去掉 -keep class com.ccit.** {*; }
-keep class ccit.** { *; }
-keep class com.aspire.**
-keep class mm.vending.**
今天在打包混淆的时候,因为使用了android-support-v4而发生混淆错误,无法打包,网上google了一下,没发现可用的,就东拼西凑的把网上各个版本拼凑了下,拼凑出下面自己可用通过的代码。
在proguard.cfg中加入以下代码:
-libraryjars libs/android-support-v4.jar //这里根据你的jar包的路径做改动
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment
不混淆webview中对javascript接口
-keep public class com.igrs.dlna.activity.webviewActivity.JavaScriptInterface
-keepclassmembers class com.igrs.dlna.activity.webviewActivity.JavaScriptInterface{
void showSource(java.lang.String,java.lang.String);
}
-keepclassmembers class com.igrs.dlna.activity.webviewActivity$InJavaScriptLocalObj {
public void showSource(java.lang.String,java.lang.String);
public void showTitle(java.lang.String);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
混淆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以上混淆就会出错!