昨天,客户想看一下目前项目开发到什么程度了,于是需要将项目签名打包成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包。

所报错误如下

  Proguard returned with error code 1. See console
Note: there were 1465 duplicate class definitions.
Warning: library class android.net.http.AndroidHttpClient extends or implements program class org.apache.http.client.HttpClient
Warning: org.apache.http.client.protocol.RequestAddCookies: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.RequestAddCookies: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.RequestAuthCache: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.RequestAuthCache: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.RequestAuthenticationBase: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.RequestAuthenticationBase: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.RequestClientConnControl: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.RequestClientConnControl: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.ResponseAuthCache: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.ResponseAuthCache: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.ResponseProcessCookies: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.client.protocol.ResponseProcessCookies: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.auth.BasicScheme: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.BasicScheme: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSManager
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSName
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSManager
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSName
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSManager
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSContext
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSContext
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSContext
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSManager
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSName
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSContext
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSManager
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSManager
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSName
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSContext
Warning: org.apache.http.impl.auth.GGSSchemeBase: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.auth.KerberosScheme: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.KerberosScheme: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.KerberosScheme: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.NTLMEngineImpl$NTLMMessage: can't find referenced class org.apache.commons.codec.binary.Base64
Warning: org.apache.http.impl.auth.NegotiateScheme: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.auth.NegotiateScheme: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.NegotiateScheme: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.NegotiateScheme: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.auth.NegotiateScheme: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.auth.NegotiateScheme: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.auth.NegotiateScheme: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.auth.NegotiateScheme: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.SPNegoScheme: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.SPNegoScheme: can't find referenced class org.ietf.jgss.Oid
Warning: org.apache.http.impl.auth.SPNegoScheme: can't find referenced class org.ietf.jgss.GSSException
Warning: org.apache.http.impl.client.AbstractAuthenticationHandler: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AbstractAuthenticationHandler: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AbstractHttpClient: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AbstractHttpClient: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AuthenticationStrategyAdaptor: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AuthenticationStrategyAdaptor: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AuthenticationStrategyImpl: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AuthenticationStrategyImpl: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AutoRetryHttpClient: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.AutoRetryHttpClient: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.DefaultRedirectHandler: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.DefaultRedirectHandler: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.DefaultRedirectStrategy: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.DefaultRedirectStrategy: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.DefaultRequestDirector: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.DefaultRequestDirector: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.HttpAuthenticator: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.client.HttpAuthenticator: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.BasicClientConnectionManager: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.BasicClientConnectionManager: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultClientConnection: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultClientConnection: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultClientConnection: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultClientConnectionOperator: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultClientConnectionOperator: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultHttpResponseParser: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultHttpResponseParser: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultResponseParser: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.DefaultResponseParser: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.IdleConnectionHandler: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.IdleConnectionHandler: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.InMemoryDnsResolver: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.InMemoryDnsResolver: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.PoolingClientConnectionManager: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.PoolingClientConnectionManager: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.SingleClientConnManager: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.SingleClientConnManager: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.tsccm.AbstractConnPool: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.tsccm.AbstractConnPool: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.tsccm.ConnPoolByRoute: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.tsccm.ConnPoolByRoute: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.tsccm.RouteSpecificPool: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.tsccm.RouteSpecificPool: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager: can't find referenced class org.apache.commons.logging.LogFactory
Warning: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager: can't find referenced class org.apache.commons.logging.LogFactory
You should check if you need to specify additional program jars.
Warning: there were 104 unresolved references to classes or interfaces.
You may need to specify additional library jars (using '-libraryjars').
Warning: there were 1 instances of library classes depending on program classes.
You must avoid such dependencies, since the program classes will
be processed, while the library classes will remain unchanged.
java.io.IOException: Please correct the above warnings first.
at proguard.Initializer.execute(Initializer.java:321)
at proguard.ProGuard.initialize(ProGuard.java:211)
at proguard.ProGuard.execute(ProGuard.java:86)
at proguard.ProGuard.main(ProGuard.java:492)

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

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

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

 -keep class **.R$* {   *;  }//不混淆R文件
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-ignorewarnings
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* //不混淆的jar包,脚本中去掉该句
-libraryjars libs/jar包名称 -dontwarn//不警告,脚本中去掉该句
-dontskipnonpubliclibraryclassmembers -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 *;
} //不混淆某包中的指定内容,脚本中去掉该句
-keep class 包名.** {*;}

至此,就ok了

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

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

    spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...

  2. android 通过eclipse混淆代码 打包 + proguard 总结

    android应用程序的混淆打包 1 . 在工程文件project.properties中加入下proguard.config=proguard.cfg , 如下所示: target=android- ...

  3. 【新手指南】Android Studio中应用App的相关配置

    前言: 注意这是一个对于Android开发入门学习者而言的一个教程,因为自己平时很少使用Android进行原生应用的开发,对于使用Android Studio配置Android App应用的一些参数( ...

  4. 利用android proguard混淆代码

    利用android proguard混淆代码 2014-02-05 17:50 1207人阅读 评论(1) 收藏 举报 网上虽然有很多相关博客,不过貌似都不是最新版的..于是百度+谷歌+github上 ...

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

    Android Studio环境下代码混淆+签名打包 作者 Mr_冯先生 关注 2016.08.21 01:10 字数 1040 阅读 734评论 5喜欢 34 注:本文使用的Android Stud ...

  6. Android Studio(十一):代码混淆及打包apk

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

  7. Android 编程下的代码混淆

    什么是代码混淆 Java 是一种跨平台的.解释型语言,Java 源代码编译成中间”字节码”存储于 class 文件中.由于跨平台的需要,Java 字节码中包括了很多源代码信息,如变量名.方法名,并且通 ...

  8. 【Android - 进阶】之代码打包签名与混淆

    代码打包签名 Android Studio为大家集成了代码打包混淆的功能,具体操作流程如下组图所示: 1.点击Android Studio上方工具栏的  Build -> Generate Si ...

  9. 【转】Android 编程下的代码混淆

    什么是代码混淆 代码混淆(Obfuscated code)亦称花指令,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读和理解的形式的行为.代码混淆可以用于程序源代码,也可以用于程序编译而成的中 ...

随机推荐

  1. java 伪共享

    MESI协议及RFO请求典型的CPU微架构有3级缓存, 每个核都有自己私有的L1, L2缓存. 那么多线程编程时, 另外一个核的线程想要访问当前核内L1, L2 缓存行的数据, 该怎么办呢?有人说可以 ...

  2. 如何使用 Java 构建微服务?

    [编者按]微服务背后的大理念是将大型.复杂且历时长久的应用在架构上设计为内聚的服务,这些服务能够随着时间的流逝而演化.本文主要介绍了利用 Java 生态系统构建微服务的多种方法,并分析了每种方法的利弊 ...

  3. python包管理-distutils,setuptools,pip,virtualenv等介绍

    python包管理-distutils,setuptools,pip,virtualenv等介绍 对于每个编程语言来说打包和发布开发包往往非常重要,而作为一个编程者能够快速容易的获得并应用这些由第三方 ...

  4. 手动更改WIN远程桌面端口,要改两个地方的注册表哟

    看到我的服务器有老多人在用桌面连接,虽然进不去,但他们不停地试,浪费掉不少服务器资源,我看到网上有不少关于修改3389的介绍.修改3389的工具,一些工具一点用都没有,纯属扯淡.修改后照样是3389. ...

  5. WordPress Simple Login Registration插件’username‘参数跨站脚本漏洞

    漏洞名称: WordPress Simple Login Registration插件’username‘参数跨站脚本漏洞 CNNVD编号: CNNVD-201308-519 发布时间: 2013-0 ...

  6. 深入浅出Node.js (9) - 玩转进程

    9.1 服务模型的变迁 9.1.1 石器时代:同步 9.1.2 青铜时代:复制进程 9.1.3 白银时代:多线程 9.1.4 黄金时代:事件驱动 9.2 多进程架构 9.2.1 创建子进程 9.2.2 ...

  7. 纯css切换左侧菜单

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 移植lrzsz串口文件传输工具到mini2440

    1.下载源码2.解压源码 tar -xzf lrzsz-0.12.20.tar.gz 3.检查配置 ./configure 4.修改Makefile 有三个Makefile需要修改,分别是lrzsz- ...

  9. Hibernate(十)多对多单向关联映射

    上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的 相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映 射之前,首 ...

  10. HDU 2089 不要62(挖个坑=-=)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...