[英]How does one add an external library jar to 'External Libraries' section of an Android Studio 1.3 project?
[英]How to add a jar in External Libraries in Android Studio?
我是 Android Studio 的新手。
如何在<JDK>文件夾下的External Libraries
中添加幾個jar的文件?
將您的 JAR 放在 app/libs 中,然后在 app/build.gradle 中的dependencies
部分中添加:
implementation fileTree(dir: 'libs', include: ['*.jar'])
在應用程序目錄中創建“libs”文件夾在libs文件夾中復制你的jar文件在Android Studio中右鍵單擊你的jar文件並添加為庫...然后打開build.gradle並添加以下內容:
dependencies {
implementation files('libs/your jar file.jar')
}
帶有解析罐的示例...
從項目視圖將 jars 添加到 libs 文件夾……如果不存在則創建 lib 文件夾
復制那里的所有罐子...
將庫添加到 gradle.... 在 build.gradle 文件中:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:percent:23.0.0'
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs', include: 'Parse-*.jar’)
}
要添加 lib 文件夾的所有 jar... 將 Parse-*.jar 更改為 *.jar
在 Android Studio 3.0 或更高版本中,我使用了如下代碼:
Project
視圖在build.gradle添加這些行
// Add this line if was not added before. implementation fileTree(dir: 'libs', include: ['*.jar']) implementation files('libs/com.ibm.icu_3.4.4.1.jar')
基於 GUI 的方法是在您的項目中添加一個額外的模塊。
最后一條建議。 確保您包含的 JAR 文件最多使用 JDK 1.7 構建。 許多與錯誤消息“com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)”相關的問題直接指向這個:0。
Android Studio 添加 Jar 文件的簡單方法步驟:
如果有人正在尋找另一種解決方案,而沒有將 jar 文件實際復制到項目目錄中,例如在多個項目中使用 jar 時:
and add打開並添加
def myJarFolder = 'C:/Path/To/My/Jars'
[...]
dependencies {
[...]
compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
[...]
}
variable, I find it useful though.請注意,當然您不必使用變量,但我發現它很有用。 .路徑也可以是相對的,例如 。
使用 AndroidStudio 3.0 測試
instead of :更新:對於 Gradle Plugin > 3.0 使用而不是 :
dependencies {
[...]
implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
[...]
}
libs
文件夾並將 jar 文件復制到其中。build.gradle
文件中的build.gradle
:implementation fileTree(include: '*.jar', dir: 'libs')
如果您沒有看到“添加為庫”選項,請確保您解壓縮(解壓縮)您的文件,以便您擁有mail.jar
而不是mail.zip
。
然后右鍵單擊您的文件,您可以看到“添加為庫”選項。
請在 build.gradle 中提供 jar 文件位置
implementation fileTree(dir: '<DirName>', include: ['*.jar'])
例子:
implementation fileTree(dir: 'C:\Downloads', include: ['*.jar'])
添加單個jar文件
implementation files('libs/foo.jar')
注意: compile 在最新的 gradle 中已被棄用,因此請改用 implementation。
將 jar 作為外部庫添加到您的 android 項目中的“官方方法”是將 jar 添加到build.gradle中的dependencies { }部分。
如果您已完成上述所有操作,但上述操作均無效,則還有另外兩種可能性:
package abc;
應該與文件夾 a > 文件夾 b > 文件夾 c 匹配。但是,如果您要使用cordova ,這里有一些添加外部 jar 的技巧。
“ build-extras.gradle ”是管理 gradle 文件的更好方法。
以下是在基於cordova 的android 項目中管理額外設置的步驟:
//Other settings go here, eg: buildscript { ... } ext.postBuildExtras = { // you may have some other settings, eg: android { ... } dependencies { compile files('libs/abc.jar') } }
(這里有更詳細的步驟: 擴展cordova gradle文件以包含谷歌服務)
cordova build android
手動:
libs
文件夾( build.gradle
所在的位置)。 例如應用程序libs
implementation files('libs/<name>.jar')
到build.gradle
。 和 UI Add as library
一樣這對我有用:
implementation files('libs\\myFile.jar')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.