Android代码混淆及项目发布方法记录

时间:2022-02-17 18:16:35


Android代码混淆及项目发布步骤记录

本来整理了一份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与本地原生组件之间的调用过程不被混淆