Android之如何混淆代码和相关配置

昨天,客户想看一下目前项目开发到什么程度了,于是需要将项目签名打包成apk,结果打包的时候出错了,吃惊,什么情况。等成功打包以后,安装起来发现部分功能又报错了,囧,所幸最后还是解决了。在这里记录一下遇到的错误和解决方法。

1.如何混淆

将代码混淆起来,这样可以防止在apk被人反编译后而被别人直接看到源码,混淆方法很简单,当我们创建好项目时,已经自动为我们生成了混淆文件,老版的ADT生成的是proguard.cfg文件,而新版的ADT则是以proguard-project.txt替代了它。两则配置方法一致,只要在project.properties文件中引入就好。前者是将project.properties的“#  proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt”的“#”去掉就可以了,后者加入一句proguard.config=proguard.cfg即可。

2.如何防止第三方jar包混淆

我使用的混淆文件是proguard.cfg。

开头提到的第一个打包失败的原因是因为引入了第三方jar包。

所报错误如下

  1  Proguard returned with error code 1. See console
  2  Note: there were 1465 duplicate class definitions.
  3  Warning: library class android.net.http.AndroidHttpClient extends or implements program class org.apache.http.client.HttpClient
  4  Warning: org.apache.http.client.protocol.RequestAddCookies: can‘t find referenced class org.apache.commons.logging.LogFactory
  5  Warning: org.apache.http.client.protocol.RequestAddCookies: can‘t find referenced class org.apache.commons.logging.LogFactory
  6  Warning: org.apache.http.client.protocol.RequestAuthCache: can‘t find referenced class org.apache.commons.logging.LogFactory
  7  Warning: org.apache.http.client.protocol.RequestAuthCache: can‘t find referenced class org.apache.commons.logging.LogFactory
  8  Warning: org.apache.http.client.protocol.RequestAuthenticationBase: can‘t find referenced class org.apache.commons.logging.LogFactory
  9  Warning: org.apache.http.client.protocol.RequestAuthenticationBase: can‘t find referenced class org.apache.commons.logging.LogFactory
 10  Warning: org.apache.http.client.protocol.RequestClientConnControl: can‘t find referenced class org.apache.commons.logging.LogFactory
 11  Warning: org.apache.http.client.protocol.RequestClientConnControl: can‘t find referenced class org.apache.commons.logging.LogFactory
 12  Warning: org.apache.http.client.protocol.ResponseAuthCache: can‘t find referenced class org.apache.commons.logging.LogFactory
 13  Warning: org.apache.http.client.protocol.ResponseAuthCache: can‘t find referenced class org.apache.commons.logging.LogFactory
 14  Warning: org.apache.http.client.protocol.ResponseProcessCookies: can‘t find referenced class org.apache.commons.logging.LogFactory
 15  Warning: org.apache.http.client.protocol.ResponseProcessCookies: can‘t find referenced class org.apache.commons.logging.LogFactory
 16  Warning: org.apache.http.impl.auth.BasicScheme: can‘t find referenced class org.apache.commons.codec.binary.Base64
 17  Warning: org.apache.http.impl.auth.BasicScheme: can‘t find referenced class org.apache.commons.codec.binary.Base64
 18  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.apache.commons.logging.LogFactory
 19  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.apache.commons.codec.binary.Base64
 20  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.apache.commons.codec.binary.Base64
 21  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSManager
 22  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSName
 23  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSManager
 24  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSName
 25  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSManager
 26  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSContext
 27  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSContext
 28  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSContext
 29  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSException
 30  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSException
 31  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSException
 32  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.apache.commons.codec.binary.Base64
 33  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.apache.commons.codec.binary.Base64
 34  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.apache.commons.logging.LogFactory
 35  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSManager
 36  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSName
 37  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSContext
 38  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.apache.commons.codec.binary.Base64
 39  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSManager
 40  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.Oid
 41  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.Oid
 42  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSManager
 43  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSName
 44  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSContext
 45  Warning: org.apache.http.impl.auth.GGSSchemeBase: can‘t find referenced class org.ietf.jgss.GSSException
 46  Warning: org.apache.http.impl.auth.KerberosScheme: can‘t find referenced class org.ietf.jgss.Oid
 47  Warning: org.apache.http.impl.auth.KerberosScheme: can‘t find referenced class org.ietf.jgss.Oid
 48  Warning: org.apache.http.impl.auth.KerberosScheme: can‘t find referenced class org.ietf.jgss.GSSException
 49  Warning: org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage: can‘t find referenced class org.apache.commons.codec.binary.Base64
 50  Warning: org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage: can‘t find referenced class org.apache.commons.codec.binary.Base64
 51  Warning: org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage: can‘t find referenced class org.apache.commons.codec.binary.Base64
 52  Warning: org.apache.http.impl.auth.NegotiateScheme: can‘t find referenced class org.apache.commons.logging.LogFactory
 53  Warning: org.apache.http.impl.auth.NegotiateScheme: can‘t find referenced class org.ietf.jgss.Oid
 54  Warning: org.apache.http.impl.auth.NegotiateScheme: can‘t find referenced class org.ietf.jgss.Oid
 55  Warning: org.apache.http.impl.auth.NegotiateScheme: can‘t find referenced class org.ietf.jgss.GSSException
 56  Warning: org.apache.http.impl.auth.NegotiateScheme: can‘t find referenced class org.ietf.jgss.GSSException
 57  Warning: org.apache.http.impl.auth.NegotiateScheme: can‘t find referenced class org.apache.commons.logging.LogFactory
 58  Warning: org.apache.http.impl.auth.NegotiateScheme: can‘t find referenced class org.ietf.jgss.GSSException
 59  Warning: org.apache.http.impl.auth.NegotiateScheme: can‘t find referenced class org.ietf.jgss.Oid
 60  Warning: org.apache.http.impl.auth.SPNegoScheme: can‘t find referenced class org.ietf.jgss.Oid
 61  Warning: org.apache.http.impl.auth.SPNegoScheme: can‘t find referenced class org.ietf.jgss.Oid
 62  Warning: org.apache.http.impl.auth.SPNegoScheme: can‘t find referenced class org.ietf.jgss.GSSException
 63  Warning: org.apache.http.impl.client.AbstractAuthenticationHandler: can‘t find referenced class org.apache.commons.logging.LogFactory
 64  Warning: org.apache.http.impl.client.AbstractAuthenticationHandler: can‘t find referenced class org.apache.commons.logging.LogFactory
 65  Warning: org.apache.http.impl.client.AbstractHttpClient: can‘t find referenced class org.apache.commons.logging.LogFactory
 66  Warning: org.apache.http.impl.client.AbstractHttpClient: can‘t find referenced class org.apache.commons.logging.LogFactory
 67  Warning: org.apache.http.impl.client.AuthenticationStrategyAdaptor: can‘t find referenced class org.apache.commons.logging.LogFactory
 68  Warning: org.apache.http.impl.client.AuthenticationStrategyAdaptor: can‘t find referenced class org.apache.commons.logging.LogFactory
 69  Warning: org.apache.http.impl.client.AuthenticationStrategyImpl: can‘t find referenced class org.apache.commons.logging.LogFactory
 70  Warning: org.apache.http.impl.client.AuthenticationStrategyImpl: can‘t find referenced class org.apache.commons.logging.LogFactory
 71  Warning: org.apache.http.impl.client.AutoRetryHttpClient: can‘t find referenced class org.apache.commons.logging.LogFactory
 72  Warning: org.apache.http.impl.client.AutoRetryHttpClient: can‘t find referenced class org.apache.commons.logging.LogFactory
 73  Warning: org.apache.http.impl.client.DefaultRedirectHandler: can‘t find referenced class org.apache.commons.logging.LogFactory
 74  Warning: org.apache.http.impl.client.DefaultRedirectHandler: can‘t find referenced class org.apache.commons.logging.LogFactory
 75  Warning: org.apache.http.impl.client.DefaultRedirectStrategy: can‘t find referenced class org.apache.commons.logging.LogFactory
 76  Warning: org.apache.http.impl.client.DefaultRedirectStrategy: can‘t find referenced class org.apache.commons.logging.LogFactory
 77  Warning: org.apache.http.impl.client.DefaultRequestDirector: can‘t find referenced class org.apache.commons.logging.LogFactory
 78  Warning: org.apache.http.impl.client.DefaultRequestDirector: can‘t find referenced class org.apache.commons.logging.LogFactory
 79  Warning: org.apache.http.impl.client.HttpAuthenticator: can‘t find referenced class org.apache.commons.logging.LogFactory
 80  Warning: org.apache.http.impl.client.HttpAuthenticator: can‘t find referenced class org.apache.commons.logging.LogFactory
 81  Warning: org.apache.http.impl.conn.BasicClientConnectionManager: can‘t find referenced class org.apache.commons.logging.LogFactory
 82  Warning: org.apache.http.impl.conn.BasicClientConnectionManager: can‘t find referenced class org.apache.commons.logging.LogFactory
 83  Warning: org.apache.http.impl.conn.DefaultClientConnection: can‘t find referenced class org.apache.commons.logging.LogFactory
 84  Warning: org.apache.http.impl.conn.DefaultClientConnection: can‘t find referenced class org.apache.commons.logging.LogFactory
 85  Warning: org.apache.http.impl.conn.DefaultClientConnection: can‘t find referenced class org.apache.commons.logging.LogFactory
 86  Warning: org.apache.http.impl.conn.DefaultClientConnectionOperator: can‘t find referenced class org.apache.commons.logging.LogFactory
 87  Warning: org.apache.http.impl.conn.DefaultClientConnectionOperator: can‘t find referenced class org.apache.commons.logging.LogFactory
 88  Warning: org.apache.http.impl.conn.DefaultHttpResponseParser: can‘t find referenced class org.apache.commons.logging.LogFactory
 89  Warning: org.apache.http.impl.conn.DefaultHttpResponseParser: can‘t find referenced class org.apache.commons.logging.LogFactory
 90  Warning: org.apache.http.impl.conn.DefaultResponseParser: can‘t find referenced class org.apache.commons.logging.LogFactory
 91  Warning: org.apache.http.impl.conn.DefaultResponseParser: can‘t find referenced class org.apache.commons.logging.LogFactory
 92  Warning: org.apache.http.impl.conn.IdleConnectionHandler: can‘t find referenced class org.apache.commons.logging.LogFactory
 93  Warning: org.apache.http.impl.conn.IdleConnectionHandler: can‘t find referenced class org.apache.commons.logging.LogFactory
 94  Warning: org.apache.http.impl.conn.InMemoryDnsResolver: can‘t find referenced class org.apache.commons.logging.LogFactory
 95  Warning: org.apache.http.impl.conn.InMemoryDnsResolver: can‘t find referenced class org.apache.commons.logging.LogFactory
 96  Warning: org.apache.http.impl.conn.PoolingClientConnectionManager: can‘t find referenced class org.apache.commons.logging.LogFactory
 97  Warning: org.apache.http.impl.conn.PoolingClientConnectionManager: can‘t find referenced class org.apache.commons.logging.LogFactory
 98  Warning: org.apache.http.impl.conn.SingleClientConnManager: can‘t find referenced class org.apache.commons.logging.LogFactory
 99  Warning: org.apache.http.impl.conn.SingleClientConnManager: can‘t find referenced class org.apache.commons.logging.LogFactory
100  Warning: org.apache.http.impl.conn.tsccm.AbstractConnPool: can‘t find referenced class org.apache.commons.logging.LogFactory
101  Warning: org.apache.http.impl.conn.tsccm.AbstractConnPool: can‘t find referenced class org.apache.commons.logging.LogFactory
102  Warning: org.apache.http.impl.conn.tsccm.ConnPoolByRoute: can‘t find referenced class org.apache.commons.logging.LogFactory
103  Warning: org.apache.http.impl.conn.tsccm.ConnPoolByRoute: can‘t find referenced class org.apache.commons.logging.LogFactory
104  Warning: org.apache.http.impl.conn.tsccm.RouteSpecificPool: can‘t find referenced class org.apache.commons.logging.LogFactory
105  Warning: org.apache.http.impl.conn.tsccm.RouteSpecificPool: can‘t find referenced class org.apache.commons.logging.LogFactory
106  Warning: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager: can‘t find referenced class org.apache.commons.logging.LogFactory
107  Warning: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager: can‘t find referenced class org.apache.commons.logging.LogFactory
108        You should check if you need to specify additional program jars.
109  Warning: there were 104 unresolved references to classes or interfaces.
110           You may need to specify additional library jars (using ‘-libraryjars‘).
111  Warning: there were 1 instances of library classes depending on program classes.
112           You must avoid such dependencies, since the program classes will
113           be processed, while the library classes will remain unchanged.
114  java.io.IOException: Please correct the above warnings first.
115      at proguard.Initializer.execute(Initializer.java:321)
116      at proguard.ProGuard.initialize(ProGuard.java:211)
117      at proguard.ProGuard.execute(ProGuard.java:86)
118      at proguard.ProGuard.main(ProGuard.java:492)

解决方法很简单,就是在proguard.cfg中加入句-dontwarn即可。

第二个运行时报错,我用真机调试查看了下LogCat日志输出,发现报的错误是找不到资源,而报错地方是引入第三方jar的时候,原来是混淆时将第三方jar包也混淆了,那么只要不混淆第三方jar就可以吧。

以下是我最后的proguard.cfg配置。

 1 -keep class **.R$* {   *;  }//不混淆R文件
 2 -optimizationpasses 5
 3 -dontusemixedcaseclassnames
 4 -dontskipnonpubliclibraryclasses
 5 -dontpreverify
 6 -ignorewarnings
 7 -verbose
 8 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
 9
10 //不混淆的jar包,脚本中去掉该句
11 -libraryjars libs/jar包名称
12
13 -dontwarn//不警告,脚本中去掉该句
14 -dontskipnonpubliclibraryclassmembers
15
16 -keep public class * extends android.app.Activity
17 -keep public class * extends android.app.Application
18 -keep public class * extends android.app.Service
19 -keep public class * extends android.content.BroadcastReceiver
20 -keep public class * extends android.content.ContentProvider
21 -keep public class * extends android.app.backup.BackupAgentHelper
22 -keep public class * extends android.preference.Preference
23 -keep public class com.android.vending.licensing.ILicensingService
24
25 -keepclasseswithmembernames class * {
26     native <methods>;
27 }
28
29 -keepclasseswithmembers class * {
30     public <init>(android.content.Context, android.util.AttributeSet);
31 }
32
33 -keepclasseswithmembers class * {
34     public <init>(android.content.Context, android.util.AttributeSet, int);
35 }
36
37 -keepclassmembers class * extends android.app.Activity {
38    public void *(android.view.View);
39 }
40
41 -keepclassmembers enum * {
42     public static **[] values();
43     public static ** valueOf(java.lang.String);
44 }
45
46 -keep class * implements android.os.Parcelable {
47   public static final android.os.Parcelable$Creator *;
48 }
49
50 //不混淆某包中的指定内容,脚本中去掉该句
51 -keep class 包名.** {*;}

至此,就ok了

时间: 2024-11-01 21:49:02

Android之如何混淆代码和相关配置的相关文章

spring mvc 图片上传,图片压缩、跨域解决、 按天生成目录 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成目录 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE be

spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE b

Android studio ButterKnife8.1.0空指针/相关配置及其简单使用

ButterKnife 是这两年比较流行的开源库,是一个View注入框架,其主要功能是代替findViewByid()方法.同时还能够快速的添加监听事件,能够更容易的实现MVVM模式,是非常实用的工具之一. 刚开始使用ButterKnife的时候很多人可能会遇到NullPointerException或者“找不到符号”,这皆是由于ButterKnife配置有误.下面我们来详细的来说明ButterKnife的配置 首先我们需要导入ButterKnife 如图 也可以在这里添加 同时还需要安装But

APP 打包混淆代码

APP项目开发完成之后,当测试人员测试了,没有Bug了,一般情况下,公司会有一些技术人员负责发布.推广APP产品.这是一般正规的大公司会有专门的市场推广部门,技术维护部门,对APP进行加密混淆打包,如:使用Ant加密,还有使用第三方的加密方式(爱加密,梆梆,APKProtect等).想必APP产品为什么需要加密混淆,做为开发人员.老板都明白其中的隐含之处.然而,还有一种方式可以打包混淆APP,那就是 android 通过eclipse混淆代码 打包 + proguard 方式.具体的步骤如下:

android对app进行代码混淆

接到一个新的任务,对现有项目进行代码混淆.之前对混淆有过一些了解,但是不够详细和完整,知道有些东西混淆起来还是比较棘手的.不过幸好目前的项目不是太复杂(针对混淆这块来说),提前完成--现总结之. 第一部分 介绍下操作流程(eclipse): 1.打开混淆器:找到项目根目录下的project.properties文件,将"#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt"

Android Studio环境下代码混淆+签名打包

Android Studio环境下代码混淆+签名打包 作者 Mr_冯先生 关注 2016.08.21 01:10 字数 1040 阅读 734评论 5喜欢 34 注:本文使用的Android Studio版本是 2.1.2. 做公司项目,需要对项目进行代码混淆+签名打包,然后就各种搜集查看资料,算是小有成果吧.周末无事就想着总结一下以后再用可做参考.如有不对的地方烦请各位大神指导-- 一.代码混淆 android studio进行代码混淆需要在两个地方做配置:1.添加自己的混淆规则到对应Modu

利用android proguard混淆代码

利用android proguard混淆代码 2014-02-05 17:50 1207人阅读 评论(1) 收藏 举报 网上虽然有很多相关博客,不过貌似都不是最新版的..于是百度+谷歌+github上的开源demo,终于成功的配置了android proguard. 最新版的android sdk的默认配置已经可以满足我们的大多数要求了,我们只需要按照sdk的提示就可以配置大部分的必须配置,然后再加上一些基本的 自定义配置就行了. 第一步,取消project.properties里面关于prog

Android项目:proguard混淆之常见开源项目混淆配置

1.Gson混淆 ## ---------------------------------- ##   ########## Gson混淆    ########## ## ---------------------------------- -keepattributes Signature  -keep class sun.misc.Unsafe { *; }  -keep class com.google.gson.examples.android.model.** { *; } 2.gr

Android 编程下的代码混淆

什么是代码混淆 Java 是一种跨平台的.解释型语言,Java 源代码编译成中间”字节码”存储于 class 文件中.由于跨平台的需要,Java 字节码中包括了很多源代码信息,如变量名.方法名,并且通过这些名称来访问变量和方法,这些符号带有许多语义信息,很容易被反编译成 Java 源代码.为了防止这种现象,我们可以使用 Java 混淆器对 Java 字节码进行混淆. 混淆就是对发布出去的程序进行重新组织和处理,使得处理后的代码与处理前代码完成相同的功能,而混淆后的代码很难被反编译,即使反编译成功