轉自:https://www.cnblogs.com/bluestorm/p/6757999.html
我使用了方案2解決
FAQ: AAR打包引用丟包問題, jar, aar, dependency 嵌套層級依賴的丟失
問: aar包中,如何包含第三方依賴庫?
如果直接作為module 依賴是沒有問題的,但是如果打包成aar,確實找不到相關的類。
Android Library項目中如果使用Android Gradle plugin打aar包,通過maven依賴的庫,或者是local依賴的aar都不會包含在生成的aar包里
如果項目是發布一個SDK,為了方便開發者使用,我們傾向於生成一個包含所有依賴庫以及.so等文件的aar包。
需要解決的問題:
1.aar打包丟失lib庫maven遠程依賴;
關於1的解決方案可以參考該鏈接:
建立本地或者遠程的maven庫……
Androidstudio 打包aar 無法引用類庫中 dependencies 遠程依賴 - Freetofly白的博客 - 博客頻道 - CSDN.NET
http://blog.csdn.net/u011840744/article/details/50608677
2.aar打包丟失本地aar依賴
關於2的解決方案如下:
方案一、 所有依賴 Module A 的 Module 都添加: ---給好aar的路徑:xxmodule/libs/xx.aar
1
2
3
4
5
|
repositories {
flatDir {
dirs
'xxx/libs'
// Module A的libs的目錄地址
}
}
|
把所有的 Module 都添加上 Module A 的 libs 目錄的相對地址。
方案二、 在Project
下的 build.gradle
中的 repositories
中添加相應的引用如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
allprojects {
repositories {
jcenter()
flatDir {
// 由於Library module中引用了 gif 庫的 aar,在多 module 的情況下,
// 其他的module編譯會報錯,所以需要在所有工程的repositories
// 下把Library module中的libs目錄添加到依賴關系中
dirs project(
':AppLibrary'
).file(
'libs'
)
}
}
}
|
方案3、編寫腳本,實現aar打包的時候把相應依賴全部打包進aar中
為項目增加一個sub project(如pkg_project
)專門用於打包,該項目中build.gradle
內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
apply plugin:
'java'
version =
1.0
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath
'com.android.tools.build:gradle:19.1.0'
}
}
repositories {
mavenCentral()
}
dependencies {
compile project(
':<your_library_project>'
)
//此處填寫需要打包的Android Library Project name
}
task sync_jars() << {
//把所有依賴的.jar庫都拷貝到build/aar/libs下
copy {
into buildDir.getPath() +
"/aar/libs"
from configurations.compile.
findAll
{
it.getName().endsWith(
".jar"
)
}
}
}
task sync_aars(dependsOn:
':<your_library_project>:assembleRelease'
) << {
//把所有依賴的.aar庫里包含的classes.jar都拷貝到build/aar/libs下,並重命名以不被覆蓋
def
jar_name
def
aar_path
def
dest_dir = buildDir.getPath()+
"/aar"
configurations.compile.
findAll
{
it.getName().endsWith(
".aar"
)
}.
collect
{
aar_path = it.getPath()
jar_name =
"libs/"
+it.getName().replace(
".aar"
,
".jar"
)
copy {
from zipTree(aar_path)
into dest_dir
include
"**/*"
rename
'classes.jar'
, jar_name
}
}
}
task fataar(dependsOn:[sync_aars, sync_jars]) << {
task (obfuse_classes_jar, type: proguard.gradle.ProGuardTask) {
//把build/aar/libs/*.jar混淆后生成build/aar/classes.jar
configuration
"proguard.cfg"
injars buildDir.getPath()+
"/aar/libs"
outjars buildDir.getPath()+
"/aar/classes.jar"
libraryjars
"${System.getProperty('java.home')}/lib/rt.jar"
libraryjars
"${System.getProperty('java.home')}/Contents/Classes/classes.jar"
libraryjars System.getenv(
"ANDROID_HOME"
)+
"/platforms/android-19/android.jar"
}.execute()
task (gen_aar, type: Zip) {
//把生成最終的aar包,注意libs目錄需要被排除
def
dest_dir = buildDir.getPath()+
"/aar/"
baseName =
"mysdk-all"
extension =
"aar"
destinationDir = file(buildDir.getPath())
from dest_dir
exclude
"libs"
}.execute()
}
|
最后就可以使用gradlew pkg_project:fataar
來打包了
解決辦法:
使用相對路勁來找到這個aar文件。
repositories { flatDir { dirs '../myLibrary2/libs';dirs 'libs' // 申明本地庫,給aar的全路徑 } }
ref:
請問aar包中,如何包含第三方依賴庫 · Issue #76 · lovetuzitong/MultiImageSelector
https://github.com/lovetuzitong/MultiImageSelector/issues/76
Android Studio 生成aar包多Module引用問題|Android Studio|項目管理|雲計算教程網
http://www.yjs001.cn/project/androidstudio/35538217209882050505.html
Android Studio多Module使用aar編譯報錯的解決方案
http://leehong2005.com/2016/08/28/android-studio-use-aar-issues/
Android導出aar時嵌套引用的那些坑 - 簡書
http://www.jianshu.com/p/7a532de0b111
在AndroidStudio中,使用Gradle打包aar到JCenter倉庫!完整版教程!填補各種坑 - 阿衰的專欄 - 博客頻道 - CSDN.NET
http://blog.csdn.net/ls1110924/article/details/46470059
使用Gradle生成包含所有依賴庫(.jar或.aar)的aar包 - When Go meets Raspberry Pi
http://hugozhu.myalert.info/2014/11/05/52-assemble-aar-with-all-dependencies-with-gradle.html
通用的 Android studio 打包 jar Gradle 插件 - Android - 掘金
https://juejin.im/entry/57c3bc547db2a200680a07a8