1. 程式人生 > >AndroidStudio生成自定義的混淆jar包(同時將assets目錄打入jar包)(二)(by 星空武哥)

AndroidStudio生成自定義的混淆jar包(同時將assets目錄打入jar包)(二)(by 星空武哥)

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

轉載請標註原文地址:http://blog.csdn.net/lsyz0021/article/details/53107595

    在以前曾經寫過兩篇文章,關於是生成jar包和引用jar的文章,建議先看這兩篇文章。

《Android Studio生成自定義的jar包http://blog.csdn.net/lsyz0021/article/details/52162414

《Android Studio如何引用so、arr、jar包》:http://blog.csdn.net/lsyz0021/article/details/52976439


   Android Studio生成自定義jar包(同時將assets目錄打入jar包)(一)
    Android Studio生成自定義混淆jar包(同時將assets目錄打入jar包)(二)


    今天我就給大家交給大家studio如何生成帶混淆的jar包,其實和上一篇文章差不多,只不過是多了一些混淆檔案。首先我們先新建一個module(我把它新建成了一個library)。在用AndroidStudio生成混淆jar的時候也百度過很多文章,但是大多都沒有用。



為了一會給大家演示混淆後的效果,這裡建立了幾個檔案 MainActivity、UserBean、LogUtil詳細見demo



然後再在module的gradle中配置如下

def SDK_BASENAME = "mylibs";def SDK_VERSION = "_v1.0";def sdkDestinationPath = "build/outputs/jar/";def zipFile = file('build/intermediates/bundles/release/classes.jar')task deleteBuild(type: Delete)
{    delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"}task makeJar(type: Jar) {    from zipTree(zipFile)    from fileTree(dir: 'src/main', includes: ['assets/**']) // 打包assets目錄下的所有檔案    baseName = SDK_BASENAME + SDK_VERSION    destinationDir = file(sdkDestinationPath)}makeJar.dependsOn(deleteBuild, build)

    上面的配置具體我就不講解他們是什麼意思了。

     因為我們是自己手動混淆了,所以要指定混淆規則,然後開啟module的proguard-rules.pro檔案,將AndroidStudio預設的混淆檔案複製、貼上到proguard-rules.pro中。




我的proguard-rules.pro檔案



全部proguard-rules.pro配置如下

###########################以下是AndroidStudio自帶的混淆配置協議################################ 表示混淆時不使用大小寫混合類名-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>(...);}#################################以下是自己新增的混淆協議####################################下面程式碼中的路徑配置,你要修改成與你相對應的路徑#引入依賴包rt.jar(jdk路徑)(注意:如在makeJar的時候提示指定了兩次,可以將其註釋掉)-libraryjars 'C:\Android_Develop_Tools\Java\jdk1.8.0_101\jre\lib\rt.jar'#引入依賴包android.jar(android SDK路徑)(注意:如在makeJar的時候提示指定了兩次,可以將其註釋掉)#-libraryjars 'C:\Android_Develop_Tools\sdk\platforms\android-23\android.jar'#如果用到Appcompat包,需要引入(注意:如在makeJar的時候提示指定了兩次,可以將其註釋掉)#-libraryjars 'D:\AndroidStudioProjects\MyApplication\mylibrary\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.4.0\jars\classes.jar'#-libraryjars 'D:\AndroidStudioProjects\MyApplication\mylibrary\build\intermediates\exploded-aar\com.android.support\support-v4\23.4.0\jars\classes.jar'#忽略警告-ignorewarnings#保證是獨立的jar,沒有任何專案引用,如果不寫就會認為我們所有的程式碼是無用的,從而把所有的程式碼壓縮掉,匯出一個空的jar#-dontshrink#保護泛型-keepattributes Signature-keep class com.lcw.mylibrary.LogUtil{    public *;}

最後的我是禁止混淆LogUtil工具類,以便形成對比。根據混淆配置是AndroidStudio預設不混淆MainActivity,我們在也不混淆LogUtil。


然後你就在Terminal中輸入

gradlew makeJar

或者是雙擊 makeJar





我們將生成的jar包解壓可以看到MainActivity和LogUtil沒有被混淆,這就說明成功了。




注意:有可能你在執行命令的時候遇到下面類似的錯誤提示classes.jar is specified twice,說我們的classes.jar  指定了兩次


解決方式:在proguard-rules.pro中將其註釋掉即可



想及時獲取更多的文章,請關注我的微信訂閱號



           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述