前言
之前一直打包的sdk都是給內部項目使用的,沒有提供給別的客戶使用過,所以一直以來都是簡單的功能打包,也不混淆,等最後的項目再寫混淆方法。
最近要求提供sdk給客戶但又要混淆業務邏輯,只好摸索一下,百度和谷歌都沒有找到很好的教程。只能根據用過的第三方sdk的經驗寫一下,以下記錄一下自己的想法。不能保證是優秀的代碼。畢竟趕鴨子上架,有相關的經驗的大佬,希望也能提點建議。
新建model
由於我直接打包aar就好了,所以就不配置jar包的設置了。
新建接口
新建文件 ITestUtil
public interface ITestUtil {
/**
* 打印
* @param msg
*/
void doLog(String msg);
}
新建業務
新建文件 TestUtil
public class TestUtil implements ITestUtil {
@Override
public void doLog(String msg) {
Log.e("TestUtil", "打印:" + msg);
}
}
混淆
基本混淆代碼
直接放進你的混淆文件即可 proguard-rules.pro
#表示混淆時不使用大小寫混合類名
-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 <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>(...);
}
#忽略警告
-ignorewarnings
#保證是獨立的jar,沒有任何項目引用,如果不寫就會認爲我們所有的代碼是無用的,從而把所有的代碼壓縮掉,導出一個空的jar
-dontshrink
#保護泛型
-keepattributes Signature
自定義相關混淆
添加自己的相關混淆配置
# 不混淆某個類(使用者可以看到全部信息)
-keep class com.rdwl.testlib.ITestUtil {*;}
# 不混淆某個類(使用者可以看到類名)
-keep class com.rdwl.testlib.TestUtil
# 不混淆某個類中以 public 開始的方法(使用者可以看到該方法)
#-keepclassmembers class com.rdwl.testlib.TestUtil {
# public *;
#}
開啓混淆
打開 model 的 build.gradle 文件
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
混淆正式打包
點擊右側邊欄的 Gradle,在窗口中找到你的 model,選擇 Tasks -> build -> build
,如果你打開 Gradle 窗口顯示 Nothing to show,你可以看下我的這篇文章:Android studio右側Gradle窗口顯示 nothing to show
等待打包完成。你就能在model的build文件夾找到打包好的aar