APK混淆打包

来源:互联网 发布:centos ftp开启文件夹 编辑:程序博客网 时间:2024/05/20 12:23

如果需要打包时混淆APK

1.在project.properties文件中添加proguard.config=proguard-project.txt参数,proguard-project.txt是混淆说明文件,告诉系统需要保留那些类不被混淆

# This file is automatically generated by Android Tools.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!## This file must be checked in Version Control Systems.## To customize properties used by the Ant build system edit# "ant.properties", and override values to adapt the script to your# project structure.## To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt# Project target.target=android-19android.library=false#添加混淆proguard.config=proguard-project.txt 


2.在proguard-project.txt文件中添加需要保留的类

# To enable ProGuard in your project, edit project.properties# to define the proguard.config property as described in that file.## Add project specific ProGuard rules here.# By default, the flags in this file are appended to flags specified# in ${sdk.dir}/tools/proguard/proguard-android.txt# You can edit the include path and order by changing the ProGuard# include property in project.properties.## For more details, see#   http://developer.android.com/guide/developing/tools/proguard.html# Add any project specific keep options here:# If your project uses WebView with JS, uncomment the following# and specify the fully qualified class name to the JavaScript interface# class:#-keepclassmembers class fqcn.of.javascript.interface.for.webview {#   public *;#}#一些参数-optimizationpasses 5-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-dontpreverify-verbose-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*#忽略警告 也可以用-ignorewarnings-dontwarn#说明引入的第三方JAR包-libraryjars libs/android-support-v4.jar-libraryjars libs/gson-1.6.jar-libraryjars libs/mframework.jar-libraryjars libs/nineoldandroids-library-2.4.0.jar-libraryjars libs/ormlite-android-4.48.jar-libraryjars libs/ormlite-core-4.48.jar-libraryjars libs/ShareSDK-Core-2.4.0.jar-libraryjars libs/ShareSDK-QQ-2.4.0.jar-libraryjars libs/ShareSDK-SinaWeibo-2.4.0.jar-libraryjars libs/ShareSDK-TencentWeibo-2.4.0.jar-libraryjars libs/ShareSDK-Wechat-2.4.0.jar-libraryjars libs/ShareSDK-Wechat-Core-2.4.0.jar-libraryjars libs/ShareSDK-Wechat-Moments-2.4.0.jar#不混淆第三方jar包中的类-keep class m.framework.** {*;}-keep class cn.sharesdk.wechat.moments.** {*;}-keep class cn.sharesdk.framework.** {*;}-keep class cn.sharesdk.wechat.friends.** {*;}-keep class com.nineoldandroids.** {*;}-keep class com.j256.ormlite.** {*;}-keep class cn.sharesdk.sina.weibo.** {*;}-keep class com.sina.sso.** {*;}-keep class android.support.v4.** {*;}-keep class cn.sharesdk.tencent.weibo.** {*;}-keep class cn.sharesdk.tencent.qq.** {*;}-keep class cn.sharesdk.wechat.utils.** {*;}#继承系统的类默认是不混淆的-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#很重要  : 如果代码中引入的注解,必须添加这条,否则注解可能被忽略掉,造成注解失败-keepattributes *Annotation*-keep class * extends java.lang.annotation.Annotation { *; }#不混淆native方法-keepclasseswithmembernames class * {native <methods>;}#保留构造方法以Context为参数的一些类-keepclasseswithmembers class * {public <init>(android.content.Context);}-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 * implements java.io.Serializable { *;}#这些类是我们自己的一些不需要混淆的类,如带注解的实体类,需要由GSON进行转换的类,如果被混淆可能GSON 会转换失败-keep class com.xxxx.xxxxxxxxxx.http.util.JsonUtils {*;}-keep class com.xxxx.xxxxxxxxxx.entity.**{*;}-keep class com.xxxx.xxxxxxxxxx.http.action.entity.**{*;}-keep class com.xxxx.xxxxxxxxxx.ui.**{*;}-keep class com.xxxx.xxxxxxxxxx.ui.run.**{*;}#--------------以下部分是确保GSON转换成功需要保留的一些类,不能进行混淆--------------#这个参数很重要 : 和-keepattributes *Annotation* 一样 如果没有添加,GSON可能转换一些实体类会失败#保留GSON的同时还需要实体类也不能被混淆,否则可能会失败-keepattributes Signature# 保留Gson 的一些类-keep class sun.misc.Unsafe { *; }-keep class com.google.gson.stream.** { *; }-keep class com.google.gson.** { *;}##---------------^上面是GSON的混淆^  ----------

部署完成,开始打包吧

0 0
原创粉丝点击