標籤:
方式4:
1. 把 AAR 放入 libs 2.
方式3:
1. 把 AAR 放入 libs 2. 在 build.gradle 添加 repositories{flatDir{dirs ‘libs‘}} 3. 在 build.gradle 添加 dependencies{compile ‘包名:類庫名:版本號碼@aar‘}
優點√:
- 自己類庫可以自己維護自己內部的AAR引用.
- 能像維護libs裡的jar類庫一樣簡單.
- dependencies 設定方式和線上解析引用的方式一樣.
缺點×:
- 同方式2的缺點
- dependencies 設定時需要放在 compile fileTree 的上面,否則無法識別.
- dependencies 設定的名字 和 線上解析引用的方式不一樣.如
線上解析方式:compile ‘com.android.support:support-v4:[email protected]‘
本地AAR方式:compile ‘android.support:v4:[email protected]‘
如何設定正確的本地AAR名稱?
- 解壓AAR包,看AndroidManifest.xml裡的 package="android.support.v4"
- 對應的就是名稱就是 android.support:v4
- 然後必須設定AAR檔案名稱為:v4-1.0.0.aar
- 最後拼接正確的版本號碼就是 android.support:v4:1.0.0
方式2:
1. 把 AAR 放入 libs 2. 在 build.gradle 添加 repositories{flatDir{dirs ‘libs‘}} 3. 在 build.gradle 添加 dependencies{compile(name:‘nameOfYourAARFile‘, ext:‘aar‘)}
優點√:
- 自己類庫可以自己維護自己內部的AAR引用.
- 能像維護libs裡的jar類庫一樣簡單.
缺點×:
- 在類庫(library)中引用AAR庫,在別的類庫(library)或項目(application)中引用該類庫時無法解析識別其引用的AAR庫.
此問題可通過以下方法不太完美的解決掉:
1.複製一份引用的AAR庫檔案到類庫、項目的libs中,同時設定 flatDir.
(意味著有多少地方引用該類庫,就要複製多少份AAR,都要設定 flatDir)
2.在所有引用該類庫的 build.gradle 中 設定 flatDir 為以下代碼即可.
repositories {
flatDir {
dirs ‘libs‘, project(‘:類庫名‘).file(‘libs‘)
}
}
(意味著有所有引用該類庫的地方的 build.gradle,都要設定一遍 flatDir)
方式1:
1. File -> New Module -> Import .JAR/.AAR 2. import the .AAR. 3. 在 build.gradle 添加 dependencies{compile project(‘:Name-Of-Your-Module-aar‘)}
優點√:
- 可以像引用自己類庫一樣.
- 可以供多個庫、項目引用此AAR類庫.
缺點×:
- 需要像自己類庫一樣去維護.
參考資料:
- How to manually include external aar package using new Gradle Android Build System - Stack Overflow
官網解釋:當同一個JAR,AAR類庫需要多個地方同時引用時的折衷配置方案.(其實就是方式1)
Handling transitive dependencies for local artifacts (jars and aar)
If you have a local jar or aar library that you want to use in more than one project, you cannot just reference it directly as a local dependency. This is because the android plugin will complain if it finds the same jar file twice when dexing the project and all its dependencies. (Note that right now you can‘t actually use a local aar file even if you only reference it once).
One way to fix this is to deploy the artifact in a repository. While it‘s possible, it might not be convenient due to the overhead of managing such a repository.
Another option is to create a new Gradle sub-project, and to make this project‘s published artifact be the jar or aar file that you want to reuse. Then you can simply have other Gradle sub-projects depend on this new sub-project.
In this new sub-project, simply create a build.gradle with the following:
configurations.create("default") artifacts.add("default", file(‘somelib.jar‘))
Android Gradle 引用本地 AAR 的幾種方式