SDKjar从打包到混淆

因为需要给客户提供特定功能的SDK,但是又不想让自己的代码暴露给别人,所以打包后的jar包还是有必要做下混淆工作的,都是亲测有用的


一.打包

首先说打包,在开发中需要测试所以可能在项目中写或者通过依赖库的形式写的,我是通过库的形式,在打包上as已经提供了打包好的jar包存放在build/intermediates/bundles/debug或者release下classes.jar,

SDKjar从打包到混淆_第1张图片


或者自己通过gradle来打包,将下面的代码添加到build.gradle中,忽略掉我们不需要的,比如BuildConfig这些不需要的

task makeJar(type: Jar) {
    //需要打包成Jar的类的路径
    from file('build/intermediates/classes/release/')
    //sdk名
    archiveName = 'sdk.jar'
    //sdk生成路径
    destinationDir = file('build/libs')
    //过滤不需要的class
    exclude "**/**/BuildConfig.class"
    exclude "**/**/BuildConfig\$*.class"
    exclude "**/R.class"
    exclude "**/R\$*.class"
    //忽略android文件夹
    exclude "**/**/android"
    exclude "**/**/android\$*"
}
makeJar.dependsOn(build)

在as的terminal中执行gradlew makeJar命令,然后等待打包过程


这里写图片描述


打包成功或者失败会有提示,这里打包成功之后就可以去指定的路径拿到jar包了


SDKjar从打包到混淆_第2张图片

SDKjar从打包到混淆_第3张图片


二.混淆

对jar包进行混淆,看到还有一些可以使用androidsdk提供的工具也可以混淆,但是比直接在as中混淆更麻烦,还是在build gradle中添加,注意上面打包的可以删掉了,或者直接指定未混淆的为classes.jar这样免去了打包过程


task makeJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") {
    // 未混淆的jar路径
    injars 'build/libs'
    // 混淆后的jar输出路径
    outjars 'build/libs/jar-1.1.1.jar'
    // 混淆协议
    configuration 'proguard-rules.pro'
}

proguard-rules.pro 这个我提供一份基础的,注意最后一个你不需要混淆的,我们提供Jar包总是还要有开放给用户调用的,我是通过定义基类的方式,基类不参与混淆,最好是把不需要混淆的类或者接口都放在一个包下 这样这个包不参与混淆就可以了


# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# Starting with version 2.2 of the Android plugin for Gradle, these files are no longer used. Newer
# versions are distributed with the plugin and unpacked at build time. Files in this directory are
# no longer maintained.

#表示混淆时不使用大小写混合类名
-dontusemixedcaseclassnames
#表示不跳过library中的非public的类
-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
##表示不进行校验,这个校验作用 在java平台上的
-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 ;
}

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

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

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

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


#忽略警告
-ignorewarnings
#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
-dontshrink
#保护泛型
-keepattributes Signature


#自己不想混淆的配置 xx是你自己的路径
-keep class xx.xx.xx.**{*;}

同样执行gradlew makeJar 成功后可以看到混淆成功了,去指定的路径就可以拿到混淆后的Jar包


SDKjar从打包到混淆_第4张图片



在打包或者混淆的过程中,总会遇到很多的问题,很多人都说网上的方法都试了很多报错,主要大家项目不同,所以别人成功打包或者混淆你拷贝过去可能不行,首先如果打包真的不成功完全也可以拷贝那个classse.jar来使用,只是会多一些不需要的文件,混淆注意proguard-rules.pro该忽略的,不是死拷贝,学会举一反三

你可能感兴趣的:(Android,工具)