使用AS打包混淆Jar包惩歉,百度一下激率,一片一片的娄帖,但是很多都是零零散散的寫得不是很詳細(xì)或是直接拷貝姻成,按照他們的教程測試總不是很順利插龄,所以這里我就把我個(gè)人學(xué)習(xí)AS打包混淆Jar的成果總結(jié)出來,希望對大家有幫助科展。個(gè)人覺得寫得還是比較詳細(xì)的
使用gradle混淆打包Jar
使用AS開發(fā)項(xiàng)目辫狼,引入第三方庫是非常方便的,我們只需要在build.gradle
中配置一行代碼就可以輕松引入我們需要的開發(fā)庫辛润。那么gradle可以幫我們混淆打包Jar嗎膨处?答案是當(dāng)然可以!
那么我們?nèi)绾未虬麶ar呢砂竖?其實(shí)我們在編譯項(xiàng)目的時(shí)候真椿,AS已經(jīng)幫我們在目錄build/intermediates/bundles/release/classes.jar
打好了Jar。那么我們需要做的就是把Jar進(jìn)行混淆的工作了乎澄。這里以個(gè)人項(xiàng)目bannerDemo 為例突硝,混淆步驟如下:
在你的library的build.gradle
文件中加入如下代碼:</p>
task makeJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") {
// 未混淆的jar路徑
injars 'build/intermediates/bundles/release/classes.jar'
// 混淆后的jar輸出路徑
outjars 'build/outputs/cocolove2-banner-1.1.0.jar'
// 混淆協(xié)議
configuration 'proguard-rules.pro'
}
配置混淆協(xié)議
1.我們先把AS自帶的協(xié)議配置進(jìn)來中文注釋,筆者添加
# 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.
#表示混淆時(shí)不使用大小寫混合類名
-dontusemixedcaseclassnames
#表示不跳過library中的非public的類
-dontskipnonpubliclibraryclasses
#打印混淆的詳細(xì)信息
-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
##表示不進(jìn)行校驗(yàn),這個(gè)校驗(yàn)作用 在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>(...);
}
2.AS自帶的配置文檔還是不夠的置济,我們還需要加入如下配置
這里只展示基本操作解恰,在實(shí)際開發(fā)中可能需要更多依賴,要根據(jù)具體情況引入自己需要的依賴包
#下面代碼中的xx是指我個(gè)人的配置路徑,涉及個(gè)人信息浙于,這里以xx代替
#引入依賴包rt.jar(jdk路徑)
-libraryjars /xxx/xx/xx/jdk1.8.0_77.jdk/Contents/Home/jre/lib/rt.jar
#引入依賴包android.jar(android SDK路徑)
-libraryjars /xx/xx/xx/Android/sdk/platforms/android-24/android.jar
#如果用到Appcompat包护盈,需要引入
-libraryjars /xxx/xxx/xx/xxx/MyApplication/library-banner/build/intermediates/exploded-aar/com.android.support/appcompat-v7/24.1.1/jars/classes.jar
-libraryjars /xx/xx/xx/xx/MyApplication/library-banner/build/intermediates/exploded-aar/com.android.support/support-v4/24.1.1/jars/classes.jar
#忽略警告
-ignorewarnings
#保證是獨(dú)立的jar,沒有任何項(xiàng)目引用,如果不寫就會認(rèn)為我們所有的代碼是無用的,從而把所有的代碼壓縮掉,導(dǎo)出一個(gè)空的jar
-dontshrink
#保護(hù)泛型
-keepattributes Signature
3.加入自己不想混淆的配置根據(jù)實(shí)際需求配置
-keep class com.cocolove2.library_banner.view.**{*;}
在命令行執(zhí)行命令混淆Jar,提示BUILD SUCCESFUL
表示成功羞酗!
//mac
./gradlew makeJar
//windows
gradlew makeJar
示例展示
- 我這里以混淆library-banner 庫為例
1.首先我們要看看下我們的buildTool
的配置,如下圖:
如果你的項(xiàng)目的
buildTool#Gradle
配置如上圖腐宋,那你打包混淆的第一步已經(jīng)完成了,而如果選擇的是本地的gradle
,當(dāng)你執(zhí)行./gradlew makeJar
時(shí)胸竞,系統(tǒng)會先下載gradle,不知是網(wǎng)絡(luò)不好還是被墻掉了欺嗤,我每次嘗試下載都是等了很久,最終也沒成功卫枝。
2.在項(xiàng)目目錄下執(zhí)行./gradlew makeJar
打包輸出混淆的Jar煎饼,如下圖
3.查看混淆結(jié)果.
混淆報(bào)錯(cuò)解決辦法個(gè)人遇到的
#log提示缺少依賴Jar,或者路徑不對
解決辦法:乖乖的引入缺少的依賴jar和修改路徑
#提示如下異常
[INFO] java.io.IOException: Can't read [D:\Program
Files\Java\jdk1.8.0_91\jre\lib\rt.jar] (Can't process class
[com/oracle/net/Sdp$1.class] (Unsupported class version number
[52.0] (maximum 51.0, Java 1.7)))
解決辦法:
下載最新proguard(支持Java 8的版本),然后將下載的文件解壓校赤。
將andorid sdk/tools/proguard/lib中的jar包吆玖,替換為剛下載解壓文件中的lib包。
推薦閱讀 配合學(xué)習(xí)效果更佳哦
嗯~,就這么多了,喜歡的話記得點(diǎn)贊哦Y(_)Y铡羡!