天天看點

Ant 多管道 自動打包 混淆代碼 引入第三方項目

 最近在搗鼓android打包的問題,之前也沒接觸過,感覺這東西挺瑣碎的,各種問題,弄了一天,結果還算不錯。

首先多管道自動打包問題,網上比較多這方面的資料,講的也簡單易懂,我就分享個連結

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

接下來是混淆代碼,這個也是各種出狀況,看了很多資料

1.開啟打包混淆,在android項目目錄結構下,找到project.properties檔案,配置如下圖

Ant 多管道 自動打包 混淆代碼 引入第三方項目

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 多管道 自動打包 混淆代碼 引入第三方項目
Ant 多管道 自動打包 混淆代碼 引入第三方項目

然後使用指令行模式,分别進入這三個檔案目錄下(確定ant已經配置成功),分别使用android update project -p . 指令,

會自動生成build.xml檔案和local.properties檔案

Ant 多管道 自動打包 混淆代碼 引入第三方項目

之後進入到本項目的目錄下,建立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指令,開始打包

Ant 多管道 自動打包 混淆代碼 引入第三方項目

5.以上就是整個過程,回過頭來看,還是挺容易的。