首先看下效果图,左边是封装之前的代码,右边是生成混淆jar包 供"兄弟公司"使用.
目前最常见的有*.so,*.jar,*.aar三种(.so一般C或者C++使用,我们说下后两者)
做之前感觉有点神秘且高大上,其实很简单,大致分为四步:
创建好moudle将需要生成jar的类准备好 > 配置buidl.gradle文件 > 配置混淆规则 > 生成jar包
moudle准备好以后先配置buidl.gradle
打开minifyEnabled开关,然后将下面代码复制到dependencies同级别下(AndroidStudio3.0以下需要更改路径):
def SDK_BASENAME = "roy"
def SDK_VERSION = "1.0.0"
def sdkDestinationPath = "build/outputs/jar/"
def zipFile = file('build/intermediates/packaged-classes/release/classes.jar')
task deleteBuild(type: Delete) {
delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"
}
task makeJar(type: Jar) {
from zipTree(zipFile)
// 打包assets目录下的所有文件
from fileTree(dir: 'src/main', includes: ['assets/**'])
baseName = SDK_BASENAME + SDK_VERSION
destinationDir = file(sdkDestinationPath)
}
makeJar.dependsOn(deleteBuild, build)
配置proguard-rules.pro
混淆规则分为两部分,部分代码需要自己手动配置,先看下需要手动配置的代码,博客最后会粘贴整个文件
红框内容需要手动配置本地路径,包名,类名
生成jar包
打开Terminal控制台,输入gradlew makeJar运行,出现BUILD SUCCESSFUL代表成功
注释:如果失败,仔细查看失败提示,一步一步解决;如果出现空包,代表混淆规则有问题。
复制到需要使用的项目中可以看到:
最后附上proguard-rules.pro文件全部代码:
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# 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 *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
# 表示混淆时不使用大小写混合类名
-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
#指定不混淆所有的JNI方法
-keepclasseswithmembernames class * {
native <methods>;
}
# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
#所有View的子类及其子类的get、set方法都不进行混淆
-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
# 不混淆Activity中参数类型为View的所有方法
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
# 不混淆Enum类型的指定方法
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# 不混淆Parcelable和它的子类,还有Creator成员变量
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator CREATOR;
}
# 不混淆R类里及其所有内部static类中的所有static变量字段
-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>(...);
}
###########################以下是需要手动的混淆配置协议###############################
# 注意:以下两个路径是本地jar包的位置
-libraryjars "C:\Program Files\Java\jre1.8.0_181\lib\rt.jar"
-libraryjars "C:\Users\THINKPAD\AppData\Local\Android\Sdk\platforms\android-23\android.jar"
#代码迭代优化的次数,默认5
-optimizationpasses 5
#混淆时不会产生形形色色的类名
-dontusemixedcaseclassnames
#忽略警告
-ignorewarnings
#以下是不需要混淆的文件,需要将用到的类和方法暴露出去供兄弟公司使用
-keep class com.example.jarlibrary.SocketUtil{
# 保持了类mylibrary里面public 修饰的成员变量和public修饰的方法。
public <fields>;
public <methods>;
}
生成aar包
aar相比jar包有所区别,包含所有资源文件全部打包,打包方式及其简单,如上面jar一样,生成位置:
aar包中引入的三方库不会打进去,所以在使用的项目中需重新引入,使用方式和jar有所不同(两步);
第一步:将aar包拷贝到libs目录下
第二部:配置build.gradle
repositories {
flatDir {
dirs'libs'
}
}
dependencies {
compile(name:'aar名字', ext:'aar')
}