AndroidStudio 生成jar 和 aar(混淆)

首先看下效果圖,左邊是封裝之前的代碼,右邊是生成混淆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')                     
}

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章