Ant 多渠道 自动打包 混淆代码 引入第三方项目

 最近在捣鼓android打包的问题,之前也没接触过,感觉这东西挺琐碎的,各种问题,弄了一天,结果还算不错。


首先多渠道自动打包问题,网上比较多这方面的资料,讲的也简单易懂,我就分享个链接

http://www.cnblogs.com/stay/archive/2013/05/27/3102027.html


接下来是混淆代码,这个也是各种出状况,看了很多资料

1.开启打包混淆,在android项目目录结构下,找到project.properties文件,配置如下图


2.接下来是混淆代码

因为项目使用了Gson,打包后会出现Gson无法解析数据,参考了StackOverflow上的例子,解决Gson打包问题

http://stackoverflow.com/questions/23826171/proguard-for-android-and-gson

以下是参考的通用proguard.cfg配置

##---------------Begin: proguard configuration common for all Android apps ----------

-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 public class * extends android.os.AsyncTask

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

##---------------End: proguard configuration common for all Android apps ----------
</pre></p><p></p><p><span style="font-size:14px;">下面是引入第三方jar包,打包Gson等问题配置</span><span style="font-size:14px;"></span><pre class="html" name="code" snippet_file_name="blog_20141108_3_3834077" code_snippet_id="512106">## 因为需要保持Gson不混淆,所以转换中用到的模型都不能混淆
-keep class com.xxx.domain.** {*;}

## ShareOneKey 需要 保持,否则分享会出错
-keep class cn.sharesdk.**{*;}

## Gson保持
-keepattributes Signature

##---------------Start: proguard configuration for Gson  ----------

-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------

## 以下Jar包不进行混淆(这里是引入的第三方jar包,打包过程中可能会出现某些类异常,如果确定没有用到该jar包中的某些类,可以忽略)

-dontwarn com.sun.crypto.provider.**
-keep class com.sun.crypto.provider.**{ *;}

-dontwarn org.bouncycastle.jce.provider.**
-keep class org.bouncycastle.jce.provider.**{ *;}

-dontwarn org.bouncycastle.util.**
-keep class org.bouncycastle.util.**{ *;}

-dontwarn org.bouncycastle.x509.**
-keep class org.bouncycastle.x509.**{ *;}

## 以下是依赖Jar包(这是依赖的第三方jar包,点代表当前路径,需要确保引入路径正确)
-libraryjars ./libs/BASE64.jar
-libraryjars ./libs/gson-2.2.4.jar
-libraryjars ./libs/zxing.jar
-libraryjars ./libs/bcprov-jdk16-145.jar
-libraryjars ./libs/sunjce_provider.jar

这样混淆代码的配置就好了


3.项目引用了其他library项目

项目中引用了第三方的项目SlidingMenu和OneKeyShare

方法一:将这两个文件用Build Fat Jar打成Jar包,引入到本项目的libs下,如果存在资源文件同样引入到本项目下

方法二:将这三个项目放在同一个目录下,例如(确保项目文件project.properties中配置的第三方项目引入路径正确):


然后使用命令行模式,分别进入这三个文件目录下(确保ant已经配置成功),分别使用android update project -p . 命令,

会自动生成build.xml文件和local.properties文件


之后进入到本项目的目录下,创建ant.properties和custom_rules.xml文件。ant.properties文件定义一些变量例如keystore密码,apk存放目录等;而custom_rules.xml这个文件就是用户自定义的编译规则文件(具体参考http://www.cnblogs.com/stay/archive/2013/05/27/3102027.html


4.最后就是执行ant deploy命令,开始打包



5.以上就是整个过程,回过头来看,还是挺容易的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值