中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android studio利用gradle打jar包并混淆的方法詳解

發布時間:2020-09-12 23:48:10 來源:腳本之家 閱讀:394 作者:jacpy 欄目:移動開發

本文主要介紹了Android studio利用gradle打jar包并混淆的方法,下面話不多說,來看看詳細的介紹吧。

首先打jar包的配置很簡單,使用jar的task,可以參考gradle官方文檔,具體代碼如下:

task buildJar(type: Jar, dependsOn: ['assembleRelease']) {
 destinationDir = file('build/outputs/jar/')
 appendix = ""
 baseName = ""
 version = SDK_VERSION
 // manifest信息
 def map = ['Version': SDK_VERSION,
  'Gradle': project.gradle.gradleVersion,
  'Vendor': '',
  'Date': new Date().getDateTimeString()
 ]
 manifest.attributes(map)
 from('build/intermediates/classes/release/')
 exclude('**/BuildConfig.class')
 exclude('**/BuildConfig\$*.class')
 exclude('**/R.class')
 exclude('**/R\$*.class')
 include('**/*.class')
}

但是發現上面打出來的jar包中的代碼沒有被混淆,于是加混淆。使用混淆的proguard.gradle.ProGuardTask task時,發現總是報如下錯誤:

java.io.IOException: The output jar [....jar] must be specified after an input jar, or it will be empty.

按照上面的提示,outjars是寫在injars后面啊,it will be empty是提示哪里有問題?以為是proguard-rules.pro文件中的配置有問題,結果把文件清空,還是報上面的錯誤,錯誤原因不得而知了。在這個問題上耗了很長時間,最終還是沒有解決。
突然看到上面的assembleRelease task想起了平常打APK時,會使用到這個task,打出來的APK會自動混淆。

那這個assembleRelease task就會依賴處理混淆的task,于是查看了輸出日志:

To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html.
Observed package id 'system-images;android-22;google_apis;x86' in inconsistent location 'android-sdk-windows-studio\system-images\addon-google_apis-google-22
\x86' (Expected 'android-sdk-windows-studio\system-images\android-22\google_apis\x86')
Observed package id 'system-images;android-22;google_apis;x86' in inconsistent location 'android-sdk-windows-studio\system-images\addon-google_apis-google-22
\x86' (Expected 'android-sdk-windows-studio\system-images\android-22\google_apis\x86')
Incremental java compilation is an incubating feature.  
:sdk:preBuild UP-TO-DATE                  
:sdk:extractProguardFiles
:sdk:preReleaseBuild
:sdk:checkReleaseManifest
:sdk:preDebugAndroidTestBuild UP-TO-DATE
:sdk:preDebugBuild UP-TO-DATE
:sdk:preDebugUnitTestBuild UP-TO-DATE
:sdk:preReleaseUnitTestBuild UP-TO-DATE
:sdk:prepareComAndroidSupportAnimatedVectorDrawable2510Library UP-TO-DATE 
:sdk:prepareComAndroidSupportAppcompatV72510Library UP-TO-DATE 
:sdk:prepareComAndroidSupportSupportCompat2510Library UP-TO-DATE
:sdk:prepareComAndroidSupportSupportCoreUi2510Library UP-TO-DATE
:sdk:prepareComAndroidSupportSupportCoreUtils2510Library UP-TO-DATE
:sdk:prepareComAndroidSupportSupportFragment2510Library UP-TO-DATE 
:sdk:prepareComAndroidSupportSupportMediaCompat2510Library UP-TO-DATE 
:sdk:prepareComAndroidSupportSupportV42510Library UP-TO-DATE
:sdk:prepareComAndroidSupportSupportVectorDrawable2510Library UP-TO-DATE
:sdk:prepareReleaseDependencies
:sdk:compileReleaseAidl UP-TO-DATE 
:sdk:compileReleaseNdk UP-TO-DATE
:sdk:compileLint UP-TO-DATE
:sdk:copyReleaseLint UP-TO-DATE
:sdk:compileReleaseRenderscript UP-TO-DATE
:sdk:generateReleaseBuildConfig   
:sdk:generateReleaseResValues UP-TO-DATE
:sdk:generateReleaseResources UP-TO-DATE
:sdk:mergeReleaseResources UP-TO-DATE 
:sdk:processReleaseManifest UP-TO-DATE
:sdk:processReleaseResources UP-TO-DATE 
:sdk:generateReleaseSources
:sdk:incrementalReleaseJavaCompilationSafeguard   
:sdk:compileReleaseJavaWithJavac   
:sdk:compileReleaseJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
注: 某些輸入文件使用或覆蓋了已過時的 API。                
注: 有關詳細信息, 請使用 -Xlint:deprecation 重新編譯。               
:sdk:extractReleaseAnnotations   
:sdk:mergeReleaseShaders UP-TO-DATE 
:sdk:compileReleaseShaders UP-TO-DATE
:sdk:generateReleaseAssets UP-TO-DATE
:sdk:mergeReleaseAssets UP-TO-DATE
:sdk:mergeReleaseProguardFiles UP-TO-DATE
:sdk:packageReleaseRenderscript UP-TO-DATE
:sdk:packageReleaseResources UP-TO-DATE 
:sdk:processReleaseJavaRes UP-TO-DATE
:sdk:transformResourcesWithMergeJavaResForRelease UP-TO-DATE
:sdk:transformClassesAndResourcesWithProguardForRelease   
ProGuard, version 5.2.1
Reading input...       
Reading program directory [sdk\build\intermediates\classes\release] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-media-compat\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-compat\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-fragment\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-core-ui\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\animated-vector-drawable\25.1.0\jars\classes.jar] (filte
red)
Reading library jar [android-sdk-windows-studio\extras\android\m2repository\com\android\support\support-annotations\25.1.0\support-annotations-25.1.0.jar] (f
iltered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-v4\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-vector-drawable\25.1.0\jars\classes.jar] (filter
ed)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\appcompat-v7\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-core-utils\25.1.0\jars\classes.jar] (filtered)
Reading library jar [\android-sdk-windows-studio\platforms\android-25\android.jar]
Reading library jar [\android-sdk-windows-studio\platforms\android-25\optional\org.apache.http.legacy.jar]
Note: duplicate definition of library class [android.net.http.HttpResponseCache]
Note: duplicate definition of library class [android.net.http.SslCertificate$DName]
Note: duplicate definition of library class [android.net.http.SslError]
Note: duplicate definition of library class [android.net.http.SslCertificate]
Note: duplicate definition of library class [org.apache.http.params.CoreConnectionPNames]
Note: duplicate definition of library class [org.apache.http.params.HttpConnectionParams]
Note: duplicate definition of library class [org.apache.http.params.HttpParams]
Note: duplicate definition of library class [org.apache.http.conn.scheme.SocketFactory]
Note: duplicate definition of library class [org.apache.http.conn.scheme.LayeredSocketFactory]
Note: duplicate definition of library class [org.apache.http.conn.scheme.HostNameResolver]
Note: duplicate definition of library class [org.apache.http.conn.ConnectTimeoutException]
Note: there were 11 duplicate class definitions.
 (http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass)
Initializing...
Note: you're ignoring all warnings!     
Ignoring unused library classes...     
 Original number of library classes: 5857
 Final number of library classes: 383
Printing kept classes, fields, and methods...
Shrinking...
Printing usage to [sdk\build\outputs\mapping\release\usage.txt]...
Removing unused program classes and class elements...   
 Original number of program classes: 52    
 Final number of program classes: 48
Obfuscating...
Printing mapping to [sdk\build\outputs\mapping\release\mapping.txt]...
Writing output...
Preparing output jar [sdk\build\intermediates\transforms\proguard\release\jars\3\3\main.jar]
 Copying resources from program directory [sdk\build\intermediates\classes\release] (filtered)
Printing classes to [sdk\build\outputs\mapping\release\dump.txt]...
:sdk:transformClassesAndResourcesWithSyncLibJarsForRelease   
:sdk:mergeReleaseJniLibFolders UP-TO-DATE
:sdk:transformNative_libsWithMergeJniLibsForRelease UP-TO-DATE 
:sdk:transformNative_libsWithSyncJniLibsForRelease UP-TO-DATE
:sdk:bundleRelease   
:sdk:compileReleaseSources
:sdk:assembleRelease
:sdk:buildJar   
  
BUILD SUCCESSFUL.

好家伙,被我發現了。其中從:sdk:transformClassesAndResourcesWithProguardForRelease這個task就可以看到,下面一堆日志是關于ProGuard混淆工具的,最后可以看到:

Preparing output jar [sdk\build\intermediates\transforms\proguard\release\jars\3\3\main.jar]

這個main.jar就是混淆后的jar包,只不過這個jar還包含了R類和BuildConfig類的信息,所以將這個信息過濾掉就可以。但問題也來了,jar包都已經打好了,怎么配置混淆?解決方法是只要把jar包當輸入就行了,最終配置如下:

task buildJar(type: Jar, dependsOn: ['assembleRelease']) {
 destinationDir = file('build/outputs/jar/')
 appendix = "" // SDK的后綴名稱
 baseName = "" // SDK名稱
 version = SDK_VERSION // 這個常量是在gradle.properties中配置的
 // manifest信息
 def map = ['Version': SDK_VERSION,
  'Gradle': project.gradle.gradleVersion,
  'Vendor': '',
  'Date': new Date().getDateTimeString()
 ]
 manifest.attributes(map)
 from(project.zipTree('build/intermediates/transforms/proguard/release/jars/3/3/main.jar'))
 exclude('**/BuildConfig.class')
 exclude('**/BuildConfig\$*.class')
 exclude('**/R.class')
 exclude('**/R\$*.class')
 include('**/*.class')
}

總結

OK,任務搞定。以上就是這篇文章的全部內容了,希望本文的內容對各位Android開發者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

孝昌县| 惠来县| 晋江市| 湟源县| 永吉县| 宁都县| 家居| 泰顺县| 新安县| 诸城市| 牟定县| 仁怀市| 龙游县| 德令哈市| 托克逊县| 洛宁县| 蒲城县| 沧州市| 阳东县| 贵德县| 怀柔区| 漠河县| 太仓市| 德兴市| 泰兴市| 漳浦县| 河北省| 师宗县| 揭阳市| 新昌县| 红桥区| 沐川县| 赣榆县| 石阡县| 孝感市| 汽车| 获嘉县| 柘荣县| 云林县| 汉源县| 丰原市|