Android代碼混淆及項目發布步驟記錄

來源:互聯網
上載者:User

標籤:android   項目打包   代碼混淆   proguard.config   

本來整理了一份Android項目混淆與發布的文檔,突然想到何不寫篇部落格,分享一下呢,如是便有了本文。

Android代碼混淆及項目發布步驟記錄

一、清理代碼中的調試資訊,如Log、System.out

二、在資訊清單檔中修改版本為目前的版本,如果需要更新資料庫,則需要在配置類或設定檔中修改程式資料庫版本。

三、在資訊清單檔中將項目的debugable設定為false

四、建立簽署憑證keystore檔案

五、在項目中的project.properites檔案中添加語句proguard.config=proguard-project.txt來指定混淆規則檔案

六、配置proguard-project.txt檔案

七、如果項目引用了Library Project,則Eclipse應該會在project.properties檔案中自動生產android.library.reference.1..n=../LibraryProjectName

八、如果項目中包含svntmp(通常位於項目的bin檔案夾下),在打包時應及時刪除,否則會導致打包失敗。

九、項目打包,安裝測試(最好是使用現有產生包進行升級測試)

 

附:樣本proguard-project.txt檔案及相應說明:

# This is a configuration file for ProGuard.

# http://proguard.sourceforge.net/index.html#manual/usage.html

 

# Optimizations: If you don‘t want to optimize, use the

# proguard-android.txt configuration file instead of this one, which

# turns off the optimization flags. Adding optimization introduces

# certain risks, since for example not all optimizations performed by

# ProGuard works on all versions of Dalvik. The following flags turn

# off various optimizations known to have issues, but the list may not

# be complete or up to date. (The "arithmetic" optimization can be

# used if you are only targeting Android 2.0 or later.) Make sure you

# test thoroughly if you go this route.

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*

-optimizationpasses 5

-allowaccessmodification

-dontpreverify

#-dontoptimize

 

# The remainder of this file is identical to the non-optimized version

# of the Proguard configuration file (except that the other file has

# flags to turn off optimization).

-dontusemixedcaseclassnames

-dontskipnonpubliclibraryclasses

-keepattributes Signature

-verbose

 

-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

 

-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$* {

    *;

}

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

 

-keep class * extends android.view.View{*;}

-keep class * extends android.app.Dialog{*;}

-keep class * implements java.io.Serializable{*;}

 

#-ignorewarnings

-libraryjars libs/locSDK_4.1.jar

-libraryjars libs/pinyin4j-2.5.0.jar

-libraryjars libs/libammsdk.jar

-libraryjars libs/WebtrendsAndroidClientLib.jar

#-libraryjars libs/afinallib.jar

#-libraryjars libs/stickylistheaders_lib.jar

 

-keep class android.support.v4.** {*;}

-keep class com.emilsjolander.** {*;}

-keep class org.kobjects.** {*;}

-keep class org.kxml2.** {*;}

-keep class org.xmlpull.** {*;}

-keep class net.tsz.** {*;}

-keep class com.hp.** {*;}

-keep class com.baidu.** {*;}

-keep class net.sourceforget.** {*;}

-keep class com.tencent.** {*;}

-dontwarn demo.**

-keep class demo {*;}

 

-keep class com.wly.xxx.bean.** {*;}

 

-keep class com.wly.xxx.tool.DbModelUtils{*;}

-keep class com.wly.xxx.tool.JsonUtils{*;}

 

-keep class com.wly.xxx.activity.InsuranceQuotesActivity

-keep public class com.wly.xxx.activity.InsuranceQuotesActivity$MyJavaScriptInterface

-keep public class * implements com.wly.xxx.activity.InsuranceQuotesActivity$MyJavaScriptInterface

-keepclassmembers class com.wly.xxx.activity.InsuranceQuotesActivity$MyJavaScriptInterface {

    public *;

       private *;

}

 

檔案說明:

0.以上檔案拷貝自筆者現在開發的項目,出於項目保護的目的,已將工程包名替換com.wly.xxx,讀者可以根據自己的項目加以修改!

1.藍色內容具有通用性質,可以複製黏貼;

2.橙色內容用於指定程式中用到的jar檔案(可以看到引用的Library Project不需包含,因為他們已經在project.properties檔案中指定了)。

3.紅色內容用於表示保留(不混淆)引用的jar包中的內容。

4.草綠色內容用於表示保留本地的bean檔案下的實體類不被混淆。

5.紫色內容用於表示保留本地涉及反射的類不被混淆。

6.綠色內容用於特別處理Web JS與本地原生組件之間的調用過程不被混淆。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.