導航:首頁 > 操作系統 > androidstudio編譯jar

androidstudio編譯jar

發布時間:2022-12-27 22:46:09

『壹』 如何使用android Studio打包混淆的Jar

使用AS打包混淆Jar包,網路一下,一片一片的,但是很多都是零零散散的寫得不是很詳細或是直接拷貝,按照他們的教程測試總不是很順利,所以這里我就把我個人學習AS打包混淆Jar的成果總結出來,希望對大家有幫助。個人覺得寫得還是比較詳細的

使用gradle混淆打包Jar

使用AS開發項目,引入第三方庫是非常方便的,我們只需要在build.gradle中配置一行代碼就可以輕松引入我們需要的開發庫。那麼gradle可以幫我們混淆打包Jar嗎?答案是當然可以!

那麼我們如何打包Jar呢?其實我們在編譯項目的時候,AS已經幫我們在目錄build/intermediates/bundles/release/classes.jar打好了Jar。那麼我們需要做的就是把Jar進行混淆的工作了。這里以個人項目bannerDemo為例,混淆步驟如下:

在你的library的build.gradle文件中加入如下代碼:

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'
// 混淆協議
configuration 'proguard-rules.pro'}

配置混淆協議

1.我們先把AS自帶的協議配置進來中文注釋,筆者添加

# 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.#表示混淆時不使用大小寫混合類名-dontusemixedcaseclassnames#表示不跳過library中的非public的類-#列印混淆的詳細信息-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>(...);
}

2.AS自帶的配置文檔還是不夠的,我們還需要加入如下配置
這里只展示基本操作,在實際開發中可能需要更多依賴,要根據具體情況引入自己需要的依賴包

#下面代碼中的xx是指我個人的配置路徑,涉及個人信息,這里以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
#保證是獨立的jar,沒有任何項目引用,如果不寫就會認為我們所有的代碼是無用的,從而把所有的代碼壓縮掉,導出一個空的jar
-dontshrink
#保護泛型
-keepattributes Signature

3.加入自己不想混淆的配置根據實際需求配置

-keep class com.cocolove2.library_banner.view.**{*;}

命令行執行命令混淆Jar,提示BUILD SUCCESFUL表示成功!

//mac./gradlew makeJar//windowsgradlew makeJar

示例展示

『貳』 Android Studio 3.5導入AOSP編譯的classes.jar

一、適用場景

    我們在寫應用時可能會需要調用系統隱藏的API,這些API是我們無法直接訪問的,報如下錯誤:

 此時,我們需要將系統AOSP的out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar導入到Android Studio中,使app能夠訪問這些介面。

二、實現步驟

Step1 . 在app目錄下建立libs文件夾,並將classes.jar包放入該路徑

Step2 .在app目錄上右鍵【Open Mole Settings】,點擊左側【Dependencies】,選擇app,點擊【+】選擇【2 Jar Dependency】,添加

選擇我們的libs/classes.jar,點擊【OK】

Step3 .在Mole的build.gradle中,dependencies自動增加依賴配置,注釋掉下面一行

implementation fileTree(dir: 'libs', include: ['*.jar'])  

並將implementation files('libs/classes.jar')改為

compileOnly files('libs/classes.jar')

最終如下圖所示

Step4 .修改Project的build.gradle,在allProjects中增加如下語句

    gradle.projectsEvaluated {

        tasks.withType(JavaCompile) {

            options.compilerArgs.add('-Xbootclasspath/p:app\\libs\\classes.jar')

        }

    }

Step5 .修改jar包優先順序,在Moels的app.iml文件中找到我們的classes.jar包所在行,

<orderEntry type="library" name="Gradle: __local_aars__:/Users/mrwong/AndroidStudioProjects/uart_daemon/app/libs/classes.jar:unspecified@jar" level="project" />

將其移動到

<orderEntry type="jdk" jdkName="Android API 29 Platform" jdkType="Android SDK" />

這一行之上,最終如下圖所示:

至此,app可以優先訪問我們自己的jar包 。

閱讀全文

與androidstudio編譯jar相關的資料

熱點內容
鋁膜構造柱要設置加密區嗎 瀏覽:340
考駕照怎麼找伺服器 瀏覽:882
阿里雲伺服器如何更換地區 瀏覽:970
手機app調音器怎麼調古箏 瀏覽:501
銳起無盤系統在伺服器上需要設置什麼嗎 瀏覽:17
紅旗計程車app怎麼應聘 瀏覽:978
如何編寫linux程序 瀏覽:870
吉利車解壓 瀏覽:248
java輸入流字元串 瀏覽:341
安卓軟體沒網怎麼回事 瀏覽:785
dvd壓縮碟怎麼導出電腦 瀏覽:274
冒險島什麼伺服器好玩 瀏覽:541
如何在伺服器上做性能測試 瀏覽:793
命令序列錯 瀏覽:259
javaif的條件表達式 瀏覽:576
手機app上傳的照片怎麼找 瀏覽:531
雲伺服器面臨哪些威脅 瀏覽:748
c語言各種編譯特點 瀏覽:177
路由器多種加密方法 瀏覽:604
程序員阻止電腦自動彈出定位 瀏覽:169