當前位置:首頁 » 安卓系統 » androidstudio打包命令

androidstudio打包命令

發布時間: 2023-07-27 10:51:32

㈠ Android Studio中gradle命令指定打jar包...

近期在項目中需要對網路訪問介面進行jar封裝。

在eclipse中進行指定類進行打jar包很簡單,但是在android studio中則有點困難,其主要的原因是需要掌握gradle命令形式。接下來就直接上干貨。

首先,在你需要進行分裝jar的模塊進行build.gradle文件下進行以下添加:

然後在Windos CMD中跳轉到你的項目路徑下,然後執行gradle makeJar命令,如果提示:gradle命令不可用。那麼你需要在你的系統環境變數中進行變數的添加:

找到你的gradle插件路徑進行添加,然後再在path屬性的末尾進行

「;%GRADLE_HOME%\bin」添加。

然後輸入 "gradle -v"或者"gradle -version"都可以.

如圖就說明gradle配置成功了。

然後再cmd命令到你的項目路徑下,然後再次輸入gradle makeJar回車,

這里提示我們在我需要進行jar分裝的模塊出錯了, 我們按照錯誤提示進行修改 ,在buold.gradle文件中添加

lintOptions {

   abortOnErrorfalse

}

然後點擊Sync Now。再次運行gradle makeJar。然後再一長串的編譯後在末尾看到

然後我們在項目模塊的build/libs文件夾下看到了我們命名的.jar文件

OK,到此就結束了。上面是個簡單的例子,只能打包某個包下面的所有文件,如果要實現只打某個包下面的某些子包或者文件可參考如下示例:

taskmakeSdkJar(type:org.gradle.api.tasks.bundling.Jar){

baseName'pluginsdk'

//只打包org.cmdmac下的org.cmdmac.pluginsdk.impl和org.cmdmac.gamecenter,其他子包不會被打包進去

from('build/intermediates/classes/debug/org/cmdmac/'){

include'pluginsdk/impl'

include'gamecenter'

}

into('org/cmdmac/')

//    exclude('R.class')

//    exclude{ it.name.startsWith('R$');}

taskmakeSdkJar(type:org.gradle.api.tasks.bundling.Jar){

baseName'pluginsdk'

//只打包org.cmdmac下的org.cmdmac.pluginsdk.impl和org.cmdmac.gamecenter,其他子包不會被打包進去

from('build/intermediates/classes/debug/org/cmdmac/'){

include'pluginsdk/impl'

include'gamecenter'

}

into('org/cmdmac/')

//    exclude('R.class')

//    exclude{ it.name.startsWith('R$');}

}

㈡ androidstudio怎麼將寫好的so文件工程打包成jar包

Android Studio的so庫導入和Eclipse的方式有些不同。在Android Studio中,要在工程的src/main下面新建一個jniLibs文件夾,然後將所用到的第三方so庫復制進來,然後找到Project下的build.gradle文件,在其中添加以下幾行代碼:

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
destinationDir file("$projectDir/libs")
baseName "Native_Libs2"
extension "jar"
from fileTree(dir: "libs", include: "**/*.so")
into "lib"
}

然後重新Gradle一下代碼,第三方so庫就加了進來。

參考http://www.cnblogs.com/devpan/p/5536238.html

㈢ androidstudio項目簽名打包生成apk出錯整理

本人由於一些誤操作接觸到打包文件的問題,故作此文避免再次踩坑。

1.入口:build->generate signed bundle/APK,打開後如下圖1.1:

2.選中apk打包,進入next:

3.選擇打包類型:

4.箭頭所指即為生成的混淆+簽名後的打包apk文件。(*簽名文件不可以隨便更改,如果有申請第三方的功能,都需要這個簽名文件去檢驗的,比如說集成了微信支付,我們換簽名文件的話,微信那邊就會檢驗不通過)

5.本文重點並非上述步驟,而是我自己重新生成了一個密鑰後,打包文件變成了.aab後綴文件。於是我想研究下它的打包過程和原理:

5.1在我們的build.gradle中可以更新打包文件版本號名稱(自己配一下):

5.2簽名文件會在此處進行對應生成信息:

5.3當然,你的項目文件也會對應生成相應的簽名文件,例如我此處的文件為yoocarjks.jks:

5.4自己配一下打包生成文件的命名:

以上over

6.*問題點:

6.1.簽名文件生成的亂碼是些啥?

6.2.由於我當時把項目扔垃圾桶了,還有些文件並沒有搞清楚是幹嘛的,如下圖:

buildoutputcleanuo.lock/executionhistory.bin/executionhistory.lock/filehashes.bin/filehashes.lock/outputfiles.bin/xxx.app.iml這些文件分別幹嘛的,從包名看像是打包生成的文件。

作此文激勵下自己空了去學習查閱下,若有知道的小夥伴歡迎留言分享~此文會繼續追更~

7.1關於xxx.app.iml看到一篇不錯的出錯解決方案:

https://blog.csdn.net/qq_38998213/article/details/81907253

設計原理如下:

這個文件,後續遇到問題我再深入了解下

㈣ 如何使用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

示例展示

  • 我這里以混淆library-banner庫為例

  • 1.首先我們要看看下我們的buildTool的配置,如下圖:


    [email protected]

    混淆報錯解決辦法個人遇到的

  • #log提示缺少依賴Jar,或者路徑不對


  • 解決辦法:乖乖的引入缺少的依賴jar和修改路徑

  • #提示如下異常[INFO] java.io.IOException: Can't read [D:Program

  • FilesJavajdk1.8.0_91jrelib t.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包。

  • proguard5.2.1下載地址

  • 閱讀

熱點內容
安卓哪裡填寫apple代碼 發布:2025-02-05 00:28:54 瀏覽:287
oppo手機鎖屏密碼忘記後如何更換 發布:2025-02-05 00:28:19 瀏覽:25
幼兒思維編程 發布:2025-02-05 00:18:21 瀏覽:25
我的世界電腦正版如何進入伺服器 發布:2025-02-05 00:18:06 瀏覽:880
疫情防控健康碼預警機制演練腳本 發布:2025-02-04 23:58:46 瀏覽:39
分治演算法java 發布:2025-02-04 23:41:15 瀏覽:592
安卓app點進去就閃退怎麼回事 發布:2025-02-04 23:36:56 瀏覽:779
宏按鍵編程 發布:2025-02-04 23:05:11 瀏覽:904
微信隱形密碼在哪裡設置 發布:2025-02-04 23:05:01 瀏覽:866
android的補間動畫 發布:2025-02-04 23:03:42 瀏覽:416