Android项目混淆打包
以下为我此期项目中的关于混淆打包的总结:
(本人第一次混淆打包,呵呵,错误很多!列了一些比较头疼的)
一、项目混淆过程中注意事项:
由于我的sdk版本较高,因此新建android项目下只有proguard-project.txt和project.properties这两个文件夹,而网上一些所谓混淆的方法我均试验了下,都有或多或少的问题,以下是一些混淆总结:
1、如果你的项目没有其他第三方包的话,那么进行混淆很简单,只需要将project.properties文件夹下面的注释解开就行,一点区别在于:如果您是2.3之前的sdk版本,那么就用这个proguard.config=proguard.cfg
如果是之后的则为:proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt(当然视您生成项目时候该文件具体生成情况所定)。
2、如果有第三方lib包的话,则混淆时需要注意了,以下是常用的一些lib包的混淆配置:
1)project.properties文件里面的内容:
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 yourproject 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.txtProject target.proguard.config=proguard.cfg
target=android-11
2)proguard.cfg 文件的内容:
-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
-keepclasseswithmembernames class * {
native ;
}
-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public (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 *;
}
-libraryjars libs/umeng_sdk.jar
-keepclassmembers class * {
public (org.json.JSONObject);
}
-keep public class com.smile.android.R$*{
public static final int *;
} -keep class android.support.v4.** { *; }
-libraryjars /libs/nineoldandroids-2.4.0.jar
-dontwarn com.nineoldandroids.**
-keep class com.nineoldandroids.** { *;}
注:(对呀lib包的混淆配置,一般放到此文件的最后面!)
二、混淆打包后的错误收集:
1、如果项目中含有数据库并且数据库的创建方式为通过实现BaseColumns类来依次创建数据库表的话,那么在混淆之后会出现no such table这类的错误,这个错误困扰我一晚上了,实在想不通,后面觉得应该是混淆过程中将包名混淆了导致编译器找不到该表。
解决办法:换数据库的创建方式:直接在继承自SQLiteOpenHelper中执行表格创建即:db.execSQL(DB_CREAT);这样一来就不会出错了!很奇葩的解决方法,还是回归原始比较好!
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生成如下图所示
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
#系统配置
# 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>;
}
#四大组件不能混淆
-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 {*;}
#adapter也不能混淆
-keep public class * extends android.widget.BaseAdapter {*;}
-keep public class * extends android.widget.CusorAdapter{*;}
-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.* {*;}
-dontwarn android.support.**
-dontwarn android.support.v4.**
# 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.
#百度分享相关
-libraryjars libs/armeabi/libbdpush_V2_1.so
-libraryjars libs/mips/libbdpush_V2_1.so
-libraryjars libs/x86
-libraryjars libs/Baidu-Frontia-Android-2.0.7.jar
-keep class com.baidu.** { *; }
-dontwarn com.baidu.**
#微博相关
-libraryjars libs/weibosdk.jar
-libraryjars libs/weibosdkcore.jar
-keep class com.sina.weibo.** { *; }
-keep class com.sina.weibo.** { *; }
-dontwarn com.sina.weibo.**
#jackson相关
-libraryjars libs/jackson-annotations-2.4.0-rc3.jar #jackson的jar包不要混淆
-libraryjars libs/jackson-core-2.4.0-rc3.jar
-libraryjars libs/jackson-databind-2.4.0-rc3.jar
-dontwarn com.fasterxml.jackson.** #告诉编译器fastjson打包过程中不要提示警告
-keep class com.fasterxml.jackson.** { *; } #jackson包下的所有类不要混淆,包括类里面的方法
-keepattributes Signature #这行一定要加上,否则你的object中含有其他对象的字段的时候会抛出ClassCastException
#http相关
-keep class org.apache.http.entity.mime.** {*;} #http 中所有类及类里面的内容不要混淆
-libraryjars libs/mta-sdk-1.6.2.jar
-libraryjars libs/open_sdk.jar #腾讯平台jar包不要混淆
-keep class com.tencent.** { *; } #腾讯平台jar包中所有类及类里面的内容不要混淆
#图片加载
-libraryjars libs/universal-image-loader-1.9.3-with-sources.jar #imageLoader的jar包不要混淆
-keep class com.nostra13.universalimageloader.** { *; } #imageLoader包下所有类及类里面的内容不要混淆
最后,如果打包总是出现问题,没有耐心解决,那么推荐一下http://dev.360.cn/protect/welcome/
还有一款反编译apk软件,叫安卓改之理
Android项目混淆打包的更多相关文章
- Android项目实战(二十五):Android studio 混淆+打包+验证是否成功
前言: 单挑Android项目,最近即时通讯用到环信,集成sdk的时候 官方有一句 在 ProGuard 文件中加入以下 keep. -keep class com.hyphenate.** {*;} ...
- 第07讲- Android项目的打包apk
第07讲Android项目的打包apk 方法一:在工作目录bin文件夹下有一个与项目同名的apk文件 (最懒惰的方式,不推荐,不安全,不利于版本更新,只有在开发模式时使用) 方法二:使用key方式 签 ...
- android studio...混淆打包全揭秘
前言,当前android studio使用的版本较新,低版本的如果有差异,或者问题,欢迎拍砖! 1.修改配置文件 找到配置文件,build.gradle,修改如下. signingConfigs ...
- Android Studio混淆打包
1.apk混淆打包 如果要对apk进行混淆,你要先告知gradle这个app需要混淆,并告知其混淆规则. 告知gradle需要混淆的代码 在Project/app/build.gradle中把mini ...
- Android studio 混淆打包问题
参考 : Android Studio代码混淆设置以及上传mapping文件 AndroidStudio 混淆打包 在app 目录下 proguard-rules.pro中加入 通用 混淆 #指定代 ...
- Android studio 混淆打包安装后报错NullPointerException int java.util.List.size()
菜鸟的我,尝试混淆打包app...打包之前没有什么问题,混淆打包之后遇到各种问题.首先,感谢原博主的分享.解决了我的问题.谢谢. 原文地址:http://blog.csdn.net/tou_star/ ...
- Android studio 混淆打包
AndroidStudio中的项目可以用compile的形式引入github上的开源项目,可以引用module,而不一定都要用libs文件夹中添加jar包的形式. 在最终realease打包时,混淆的 ...
- Android studio导入eclipse项目混淆打包出错
将proguard-android.txt复制一份重命名成proguard-rules.pro,且在build.gradle添加 release { minifyEnabled ...
- android模块混淆打包时,泛型丢失
现象:lib模块中写了一个泛型接口,在混淆之后泛型消失,提示"Error:(67, 79) 错误: 类型 ******* 不带有参数" 解决:混淆时把泛型给混淆掉了,在progua ...
随机推荐
- C#中各种集合类比较
数组(Array)的不足(即:集合与数组的区别) 1. 数组是固定大小的,不能伸缩.虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是 ...
- maven optional可选依赖
应用场景:projectA 依赖projectB, projectB 依赖projectC时 <dependency> <groupId>com.itear.projectC ...
- centos5.5 快速安装mysql
安装MySQL. [root@sample ~]# yum -y install mysql-server ← 安装MySQL[root@sample ~]# yum -y install php-m ...
- Atitit.注解and属性解析(2)---------语法分析 生成AST attilax总结 java .net
Atitit.注解and属性解析(2)---------语法分析 生成AST attilax总结 java .net 1. 应用场景:::因为要使用ui化的注解 1 2. 使用解释器方式来实现生成 ...
- iPhone应用程序的启动过程
Phone的入口函数main,这之后它有是怎样启动应用程序,初始化的呢,这些都是通过 UIApplicationMain 来实现的. 其启动的流程图大致如下图所示: 1 int retVal = UI ...
- arrow:让Python的日期与时间变的更好
在处理数据的时候经常会碰见各种时间数据,但因为时间数据的格式不统一,所以导致数据处理的时候有一些麻烦.Python的标准库提供了相应模块,但可用性却不高,也不够人性化.本专栏之前已经有文章介绍过在R中 ...
- [C++]在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include StdAfx.h”
问题现象:在写好的.cpp文件后,编译报错.提示"你建立的工程使用了预编译功能, cpp最前边要留一行这样的内容:#include "StdAfx.h"问题原因:网上说是 ...
- JS学习笔记(2)--正则表达式获取指定字符串
js 正则提取字串 这里就有:SA 怎么用正则提取sa出来 var str=“这里就有:SA ”怎么用正则提取sa出来 YDhcui | 浏览 2087 次 推荐于2016-05-30 18:25:4 ...
- UNION types numeric and text cannot be matched
NULL ::NUMERIC 有时候会遇到这个问题,那是因为几个SQL组合在一起的时候,同一个字段的值,出来了不同类型的时候,这种时候就需要进行转型的处理了.
- PHP,JavaScript,CSS三种HTML内嵌语言的语法,变量,循环,函数记录
PHP PHP简介: PHP 是服务器端脚本语言. PHP(全称:PHP:Hypertext Preprocessor,即"PHP:超文本预处理器")是一种通用开源脚本语言. PH ...