现在的位置: 首页 > java > android > 正文
android混淆打包
2016年03月21日 android ⁄ 共 11641字 暂无评论

转载地址:http://blog.csdn.net/wangbofei/article/details/8266553

参考博客地址:

http://hubingforever.blog.163.com/blog/static/171040579201342982553107/

http://blog.csdn.net/wotoumingzxy/article/details/8279646

http://www.cnblogs.com/sunzn/archive/2013/03/06/2946952.html

http://blog.csdn.net/atrueman123/article/details/6281658

一、理论知识  

ProGuard 是一款免费的Java类文件压缩器、优化器和混淆器。它能发现并删除无用类、字段(field)、方法和属性值(attribute)。它也能优化字节码 并删除无用的指令。最后,它使用简单无意义的名字来重命名你的类名、字段名和方法名。经过以上操作的jar文件会变得更小,并很难进行逆向工程。
二、基本使用

  在Android应用程序也可以使用

ProGuard来进行混洗打包,大大的优化Apk包的大小。但是注意ProGuard对文件路径的名名很有讲究,不支持括号,也不支持空格在混淆过后,可以在工程目录的proguard中的mapping.txt看到混淆后的类名,方法名,变量名和混淆前的类名,方法名,变量名。

 在使用Eclipse新建一个工程,都会在工程目录下生产配置project.properties和proguard-project.tx

# 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-10
project.properties用于配置Android工程的一些属性,#号的话表示当前行是注释,这里的proguard.config就用于指定ProGuard的混淆配置文件,并对使用release方式打包应用程序时开启代码混淆功能。对于是否是使用release方式打包,和AndroidManifest.xml中application的android:debuggable属性有很多关系。如果该值为android:debuggable="true",那么最终就是debug方式打包。最明智的方式就是在AndroidManifest.xml并不显示的指定它,而是是打包工具在打包时来决定它最终的值。对于ant就是ant releaseant debug。而对于直接在Eclipse中使用run debgu来打包的话就是debug,使用export的话就是release.
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

这里的话指定了混淆的基本配置文件proguard-android.txt,和混淆的个性化配置文件proguard-project.txt。这里proguard-project.txt文件用于对前面的基本的混淆配置文件proguard-android.txt的配置进行override和添加。

------------------------------------------------------------------------------------------------------------------------------------------

下面是混淆打包第三方jar包的处理:

在project.properties里加上   proguard.config=proguard.cfg

proguard.cfg 配置如下:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

#忽略警告 也可以用-ignorewarnings
-dontwarn

#声明第三方jar包,不用管第三方jar包中的.so文件(如果有)
-libraryjars libs/baidumapapi.jar
-libraryjars libs/tencent_openapi.jar
-libraryjars libs/httpmime-4.1.3.jar
-libraryjars libs/weibo.sdk.android.sso.jar
-libraryjars libs/android-support-v4.jar

-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

-keepclasseswithmembernames class * {
native <methods>;
}

-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 *;
}

#不混淆第三方jar包中的类
-keep class com.baidu.mapapi.** {*;}
-keep class com.tencent.tauth.** {*;}
-keep class org.apache.http.entity.mime.** {*;}
-keep class android.support.v4.** {*;}
-keep class android.net.http.** {*;}
-keep class com.weibo.sdk.android.** {*;}
-keep class com.sina.sso.** {*;}

说明一下,第三方jar包中如果有.so文件,不用去理会,引入的第三方jar文件不要混淆,否则可能会报异常


The same input jar is specified twice 完美解决办法

http://www.xiaoyunduo.org/article/236/

Warning:Dependency org.json:json:20090211 is ignored for debug as it may be conflicting with the internal version provided by Android.

http://stackoverflow.com/questions/32921328/dependency-org-jsonjson20090211-is-ignored-for-debug-as-it-may-be-conflicting

Error:(2, 0) Plugin with id 'com.github.dcendents.android-maven' not found

http://stackoverflow.com/questions/32022171/error2-0-plugin-with-id-com-github-dcendents-android-maven-not-found

 

Error:Cause: org/gradle/api/publication/maven/internal/DefaultMavenFactory Android

http://blog.csdn.net/yunnywu/article/details/48826653

http://blog.csdn.net/hudashi/article/details/9016805

GSON 混淆

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-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  ----------

https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

http://www.cnblogs.com/royi123/archive/2013/02/28/2937657.html

踩过的坑

http://www.cnblogs.com/dyllove98/archive/2013/07/25/3215037.html

 
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.weilian.phonelive" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile fileTree(include: '*/so', dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.jakewharton:butterknife:6.1.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile files('libs/gson-2.3.1.jar') compile 'com.zhy:okhttputils:2.3.5' compile 'org.xutils:xutils:3.3.20' compile 'org.kymjs.kjframe:kjframe:2.6' compile 'com.github.johnkil.android-appmsg:appmsg:1.2.0' compile 'com.astuetz:pagerslidingtabstrip:1.0.1' compile files('libs/universal-image-loader-1.9.0.jar') compile 'com.zhy:percent-support-extends:1.1.1' compile 'in.srain.cube:ultra-ptr:1.0.11' compile files('libs/pldroid-player-1.1.6.jar') compile files('libs/ijkmediaplayer.jar') compile ('io.socket:socket.io-client:0.7.0') { exclude group: 'org.json', module: 'json' } compile 'me.drakeet.materialdialog:library:1.3.0' compile files('libs/libksyplayer.jar') compile files('libs/libksystat.jar') compile files('libs/bottomView.jar') compile files('libs/alipaySDK-20151014.jar') compile files('libs/libammsdk.jar') compile files('libs/MobCommons-2016.0406.1915.jar') compile files('libs/MobTools-2016.0406.1915.jar') compile files('libs/ShareSDK-Core-2.7.1.jar') compile files('libs/ShareSDK-QQ-2.7.1.jar') compile files('libs/ShareSDK-QZone-2.7.1.jar') compile files('libs/ShareSDK-ShortMessage-2.7.1.jar') compile files('libs/ShareSDK-SinaWeibo-2.7.1.jar') compile files('libs/ShareSDK-TencentWeibo-2.7.1.jar') compile files('libs/ShareSDK-Wechat-2.7.1.jar') compile files('libs/ShareSDK-Wechat-Core-2.7.1.jar') compile files('libs/ShareSDK-Wechat-Favorite-2.7.1.jar') compile files('libs/ShareSDK-Wechat-Moments-2.7.1.jar') }




# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# 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 *;
#}
#shrink,测试后发现会将一些无效代码给移除,即没有被显示调用的代码,该选项 表示 不启用 shrink。
-dontshrink
#指定重新打包,所有包重命名,这个选项会进一步模糊包名,将包里的类混淆成n个再重新打包到一个个的package中
-flattenpackagehierarchy
#优化时允许访问并修改有修饰符的类和类的成员
-allowaccessmodification
#混淆前后的映射
-printmapping map.txt
#不跳过(混淆) jars中的 非public classes   默认选项
-dontskipnonpubliclibraryclassmembers
#忽略警告
-ignorewarnings
#指定代码的压缩级别
-optimizationpasses 5
#不使用大小写混合类名
-dontusemixedcaseclassnames
#不去忽略非公共的库类
-dontskipnonpubliclibraryclasses
#不启用优化  不优化输入的类文件
-dontoptimize
#不预校验
-dontpreverify
#混淆时是否记录日志
-verbose
#混淆时所采用的算法
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
#保护注解
-renamesourcefileattribute SourceFile
#保持源文件和行号的信息,用于混淆后定位错误位置
-keepattributes SourceFile,LineNumberTable
-keepattributes SourceFile,LineNumberTable
#保持含有Annotation字符串的 attributes
-keepattributes *Annotation*
-keepattributes Signature
-keepattributes Exceptions,InnerClasses

-dontwarn org.apache.**
-dontwarn android.support.**

#基础配置
# 保持哪些类不被混淆
# 系统组件
-keep public class * extends android.app.Fragment
-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
#如果有引用v4包可以添加下面这行
-keep public class * extends android.support.v4.app.Fragment
#自定义View
-keep public class * extends android.view.View
# V4,V7
-keep class android.support.v4.**{ *; }
-keep class android.support.v7.**{ *; }
-keep class android.webkit.**{*;}
-keep interface android.support.v4.app.** { *; }
#保持 本化方法及其类声明
-keepclasseswithmembers class * {
    native <methods>;
}
#保持view的子类成员: getter setter
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}
#保持Activity的子类成员:参数为一个View类型的方法   如setContentView(View v)
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}
#保持枚举类的成员:values方法和valueOf  (每个enum 类都默认有这两个方法)
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
#保持Parcelable的实现类和它的成员:类型为android.os.Parcelable$Creator 名字任意的 属性
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
#保持 任意包名.R类的类成员属性。  即保护R文件中的属性名不变
-keepclassmembers class **.R$* {
    public static <fields>;
}

#MPermission
-dontwarn com.zhy.m.**
-keep class com.zhy.m.** {*;}
-keep interface com.zhy.m.** { *; }
-keep class **$$PermissionProxy { *; }

#okio
-dontwarn okio.**
-keep class okio.**{*;}
-keep interface okio.**{*;}

#okhttputils
-dontwarn com.zhy.http.**
-keep class com.zhy.http.**{*;}
-keep interface com.zhy.http.**{*;}

#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}
-keep interface okhttp3.**{*;}

#phonelive
 #-keep class com.weilian.phonelive.ui.**{ *;}



#org
-dontwarn org.**
-keep class org.**{*;}
-keep interface org.**{*;}

## GSON 2.2.4 specific rules ##


-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

-keepattributes EnclosingMethod

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.weilian.phonelive.bean.** { *; }
# ButterKnife 6

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}


#astuetz

-keep class com.astuetz.**{ *;}
-dontwarn com.astuetz.**
#github
-keep class com.github.**{ *;}
-dontwarn com.github.**

#ksy player

 -keep class com.ksyun.media.player.annotations.**{ *;}
  -keep class com.ksyun.media.player.exceptions.**{ *;}
   -keep class com.ksyun.media.player.ffmpeg.**{ *;}
    -keep class com.ksyun.media.player.misc.**{ *;}
     -keep class com.ksyun.media.player.pragma.**{ *;}
      -keep class com.ksyun.media.player.stats.**{ *;}
      -keep class com.ksyun.media.player.util.**{ *;}
      -keep class com.ksyun.media.player.**{ *;}


#ksy publish
-keep class com.ksy.recordlib.service.core.**{ *;}
-keep class com.ksy.recordlib.service.filter.**{ *;}
-keep class com.ksy.recordlib.service.recorder.**{ *;}
-keep class com.ksy.recordlib.service.util.**{ *;}


#native jni
-keepclasseswithmembernames class * {
     native <methods>;
}


## ----------------------------------

##      sharesdk

## ----------------------------------

-keep class cn.sharesdk.**{*;}

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

-keep class **.R$* {*;}

-keep class **.R{*;}

-dontwarn cn.sharesdk.**

-dontwarn **.R$*

##  huanxin

-keep class com.hyphenate.** {*;}
-dontwarn  com.hyphenate.**

 

给我留言

您必须 [ 登录 ] 才能发表留言!

×