您的位置:首页 > 移动开发 > Android开发

android 混淆文件proguard.cfg详解

2015-05-06 13:18 337 查看
-injars androidtest.jar【jar包所在地址】

-outjars out【输出地址】

-libraryjars 'D:\android-sdk-windows\platforms\android-9\android.jar' 【引用的库的jar,用于解析injars所指定的jar类】

-optimizationpasses 5

-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

-keep public abstract interface com.asqw.android.Listener{

public protected <methods>; 【所有方法不进行混淆】

}

-keep public class com.asqw.android{

public void Start(java.lang.String); 【对该方法不进行混淆】

}

-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 *;

}

参考:/article/2305691.html

加入第三方jar包之后常出现的几个异常:

proguard returned with error code 1.See console

情况1:

Proguard returned with error code 1. See console

Error: C:/Documents (系统找不到指定文件)

后来发现是因为将整个工程放到了桌面上,而桌面的目录是C:/Documents and Settings/Administrator/桌面,在这里面有空格,而proguard进行发编译的时候是不允许有空格的

如果换了正确路径还不好用的话,直接删除proguard就好了

注意:SDK和程序路径最好不要有空格符

情况2:

Proguard returned with error code 1. See console

异常:

java.lang.ArrayIndexOutOfBoundsException

解决办法:将proguard.cfg中的"-dontpreverify"改成“-dontoptimize

参考文章:http://groups.google.com/group/android-developers/browse_thread/thread/eca3b0f5ce6ad00f

我把项目中生成的proguard文件夹(此时文件夹是空的)删掉,然后再重新运行项目,就OK 了。

情况3:

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] Proguard returned with error code 1. See console

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] java.io.IOException: Can't read [proguard.ClassPathEntry@106082] (No such file or directory)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] at proguard.InputReader.readInput(InputReader.java:230)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] at proguard.InputReader.readInput(InputReader.java:200)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] at proguard.InputReader.readInput(InputReader.java:178)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] at proguard.InputReader.execute(InputReader.java:100)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] at proguard.ProGuard.readInput(ProGuard.java:195)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] at proguard.ProGuard.execute(ProGuard.java:78)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] at proguard.ProGuard.main(ProGuard.java:499)

抛出这样的异常的原因是第三方jar的引用路径不对,没有找到这个需要忽略混淆的jar包。

参考:http://stackoverflow.com/questions/4884456/android-proguard-java-io-ioexception-cant-read-proguard-classpathentry

1.引入默认的打包配置

拷贝${sdk.dir}/tools/proguard/proguard-android.txt文件中的配置到你的工程的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
-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>;

}

# 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.**

2.第三方jar包的配置

1)fastjson配置

#fast json相关

-libraryjars libs/json.jar #fastjson的jar包不要混淆

-keep class com.alibaba.fastjson.** { *; } #fastjson包下的所有类不要混淆,包括类里面的方法

-keepattributes Signature #这行一定要加上,否则你的object中含有其他对象的字段的时候会抛出ClassCastException

-dontwarn com.alibaba.fastjson.** #告诉编译器fastjson打包过程中不要提示警告

2)xutils配置

#xutils相关

-libraryjars libs/xUtils-2.4.7.jar #xutils的jar包不要混淆

-keep class com.lidroid.** { *; } #xutils包下所有类不要混淆,包括类里面的方法

-keep class * extends java.lang.annotation.Annotation { *; }#注解包下的所有内容不要混淆,ViewUitls有用到

3)新浪微博配置

-libraryjars libs/weibosdkcore.jar #微博jar包不要混淆

-keep class com.sina.weibo.sdk.** { *; } #微博报下所有类及类里面的内容都不要混淆

4)腾讯开放平台配置

-libraryjars libs/mta-sdk-1.6.2.jar #腾讯平台jar包不要混淆

-keep class com.tencent.** { *; } #腾讯平台jar包中所有类及类里面的内容不要混淆

5)图片异步加载组件universal-image-loader配置

#图片加载

-libraryjars libs/universal-image-loader-1.8.4-with-sources.jar #imageLoader的jar包不要混淆

-keep class com.nostra13.universalimageloader.** { *; } #imageLoader包下所有类及类里面的内容不要混淆

6)友盟统计组件配置

#友盟相关

-libraryjars libs/umeng-analytics-v5.2.3.jar #友盟统计的jar包不要混淆

-keep class com.umeng.** { *; } #友盟统计jar包下的所有类及类里面的所有内容不要混淆

7)自定义控件及组件不要打包混淆

如果我们自定了ListView,ScrollView,Gallery这些组件的时候,我们就不能混淆这些自定义的类了,因为在layout里面我们已经引用这个了类,而且已经写死了。同样四大组件也是不能打包混淆的

#四大组件不能混淆

-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 * extends android.view.View {*;}

8)数据适配器adapter不要混淆

#adapter也不能混淆

-keep public class * extends android.widget.BaseAdapter {*;}

如果你使用了CusorAdapter,添加下面这行

-keep public class * extends android.widget.CusorAdapter{*;}

同样如果你觉得麻烦,就直接将BaseAdpater换成Adapter

9)数据模型不要混淆

-keepnames class * implements java.io.Serializable #比如我们要向activity传递对象使用了Serializable接口的时候,这时候这个类及类里面#的所有内容都不能混淆

-keepclassmembers class * implements java.io.Serializable {

*;

}

-keep class com.xx.xxx.domain.* {*;}

-keep class com.xx.xxx.vo.* {*;}

-keep class com.xx.xxx.model.* {*;}

这里的包名取决你自己定义的model所在包的名称

10)百度地图组件配置

#百度地图相关

-libraryjars libs/baidumapapi_v2_4_0.jar #地图相关的jar包不要混淆

-keep class com.baidu.** { *; } #地图组件包括图层、定位等接口所有的类及类里面的内容都不要混淆

-keep class vi.com.gdi.bgl.android.**{*;} #交通实况相关的类及类里面的所有内容不要混淆

-libraryjars libs/locSDK_3.1.jar #定位jar包不要混淆

-libraryjars libs/armeabi/libBaiduMapSDK_v2_4_0.so #地图相关的C++编译的可执行文件(引擎)不要混淆

-libraryjars libs/armeabi/liblocSDK3.so #定位相关的C++编译的可执行文件(引擎)不要混淆

这里要特别说明的是百度地图这个apiKey,一定要注意测试的时候与打包发布时候的SHA1是不一样的,所以需要准备两个apiKey。

所以打包的时候一定要把apiKey设置成打包所需的apiKey,否则打包后的apk文件运行后图层无法加载,但是定位功能不受影响

具体的操作是在打包最后一步把你的SHA1码拷贝一份到应用控制台重新生成一个key,如果你使用的是baidumapapi3.0以上的SDK就没有这么多麻烦事了

打包所需key生成如下图所示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: