android 打Jar包和混淆配置

记录下各配置文件,下面是打jar包的

//打jar包
def SDK_BASENAME = "***2.0";
def sdkJarPath = "build";
def zipFile = file('build/intermediates/bundles/debug/classes.jar')
task makeJar(type: Jar) {
    from zipTree(zipFile)
    // 包含本工程中assets 的文件 暂时不需要
//    from fileTree(dir: 'src/main', includes: ['assets/**'])
    //包含项目中的libs中的其他指定jar包
    from(project.zipTree("libs/xxx.jar"))

    //去掉config类
    exclude('**/BuildConfig.class')
    //去掉 其他第三方jar包的assets中的文件夹和图片
    exclude('**/**.png')
    exclude('**/xml/')
    exclude('**/drawable/')
    exclude('**/xmlaccount/')
    exclude('**/drawableaccount/')
    exclude('**/xxx . xxx ') //去除文件
    //去掉 android.mk
    exclude('**/Android.mk')
//    exclude('**\$**.class')
    /*exclude('com/example/mylibrary/BuildConfig\$*.class')
    exclude('**//*R.class')
    exclude('**//*R\$*.class')*/
    destinationDir = file(sdkJarPath)
    baseName = SDK_BASENAME
}
makeJar.dependsOn(build)
混淆命令 

//混淆jar 脚本 (许通过 执行 gradlew proguard 命令来执行混淆)
task proguard(dependsOn: ['makeJar'], type: proguard.gradle.ProGuardTask) {
    //输入的jar路径
    injars 'build/****.jar'
    //输出的jar路径
    outjars 'build/libs/ottsdkmanager2.0_proguard.jar'
    //混淆配置
    configuration 'proguard-rules.pro'
}

混淆配置文件  proguard-rules.pro内容 一般jar包混淆配置 要保证 bean  activity  和暴露接口类的不被混淆才可以 其他的可以混淆

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\adt-bundle-windows-x86-20130522\adt-bundle-windows-x86-20130522\sdk/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 *;
#}
#########################################################################

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

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

-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator 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.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

####################################################################### 下面是自定义的上面是原生带的
#引入依赖包rt.jar(jdk路径)(注意:如在makeJar的时候提示指定了两次,可以将其注释掉)
#-libraryjars 'E:\java\jdk1.8\jre\lib\rt.jar'

#引入依赖包android.jar(android SDK路径)(注意:如在makeJar的时候提示指定了两次,可以将其注释掉)
#-libraryjars 'D:\AndroidSDK\platforms\android-23\android.jar'

#忽略警告
-ignorewarnings
#不要压缩(这个必须,因为开启混淆的时候 默认 会把没有被调用的代码 全都排除掉)
-dontshrink
#避免混淆泛型 如果混淆报错建议关掉
-keepattributes Signature
# 继承activity,application,service,broadcastReceiver,contentprovider....不进行混淆
-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

#不混淆那些类 通用语法
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}

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

-dontwarn com.okhttp.utils.**
-keep class com.okhttp.utils.**{*;}


#json
-dontwarn com.google.gson.**
-keep class com.google.gson.**{*;}

-keep class com.xxx.OKUtils.**{*;}
-keep class com.xxx.activity.**{*;}
#加上{*;} 可以保证类中所有变量和方法都不被混淆  所以 class xxx.**{*;} 可以确保该类所有部分都不被混淆

#保证类 方法不被混淆
-keepclassmembers class * {
public <methods>;
}

#### -- OkHttp --
#-dontwarn com.squareup.okhttp.internal.**
#### -- Apache Commons --
#-dontwarn org.apache.commons.logging.**
-ignorewarnings
#-keep class * {
#public private *;
#}


猜你喜欢

转载自blog.csdn.net/ImTryCatchException/article/details/78849924