最近寫了個(gè)Android SDK工程,在代碼钥弯、測(cè)試統(tǒng)統(tǒng)完成后径荔,居然在導(dǎo)出的一步折騰了兩三天,在此總結(jié)下查找資料的過程和結(jié)果脆霎,引以借鑒总处。
首先,這次趟坑解決了以下問題:
- 導(dǎo)出aar至本地Maven庫睛蛛,包含引用的Module工程
- 導(dǎo)出eclipse適用的庫工程鹦马,并包含所有引用的jar包(包括嵌套引用)
1. 通用導(dǎo)出方式
通常來講胧谈,一個(gè)簡(jiǎn)單的Android Library工程,導(dǎo)出aar有這幾種方式:
- 編譯后自動(dòng)會(huì)在build/outputs/aar目錄下生成.aar文件荸频。此aar僅打包了Library工程的class第岖、libs和資源文件,但Library引用的其他庫(比如
compile "com.squareup.okhttp3:okhttp:3.4.1"
)并未包含在aar中试溯。使用Library時(shí)還需把它引用的庫再手動(dòng)聲明一遍,差評(píng)郊酒! - 發(fā)布到本地Maven庫(或jCenter遇绞、MavenCentral)。發(fā)布出來的內(nèi)容除了aar還包含了Library的所有dependencies信息燎窘。使用時(shí)直接設(shè)置好maven庫地址摹闽,聲明引用Libraray,gradle就會(huì)幫你自動(dòng)引用Library中嵌套引用的所有dependencies了褐健。
我的需求:
然而付鹿,我的SDK Library引用了自己的另一個(gè)Module工程common(用來提供基礎(chǔ)功能,服務(wù)于不同的項(xiàng)目)蚜迅,但導(dǎo)出的aar無法包含引用的common工程舵匾,我又不希望單獨(dú)導(dǎo)出common工程讓外部調(diào)用,這可怎么辦谁不?
2. 導(dǎo)出包含嵌套引用的aar
一翻Google后在Github上找到一個(gè)庫android-fat-aar(https://github.com/adwiv/android-fat-aar )坐梯,它可以將項(xiàng)目引用的module打包進(jìn)aar,并且統(tǒng)一進(jìn)行混淆(這點(diǎn)也很重要)刹帕。雖然它有無法合并AIDL吵血、無法改變build type等缺點(diǎn)(詳細(xì)見其文檔),但作為大眾的需求來講已經(jīng)足夠了偷溺。
按照文檔蹋辅,導(dǎo)入fat-aar腳本,編譯sdk library工程挫掏,打包sdk aar發(fā)布到本地maven庫侦另,一切正常,但創(chuàng)建個(gè)demo工程使用此maven庫發(fā)現(xiàn)此類報(bào)錯(cuò)(涉及包名處均用'xxx'代替):
Error:A problem occurred configuring project ':demo'.
> Could not resolve all dependencies for configuration ':demo:_debugApkCopy'.
> Could not find xxx:common:unspecified.
Required by:
xxx:demo:unspecified > com.xxx:sdk:0.0.7
報(bào)錯(cuò)demo工程找不到common工程的引用砍濒,但我已經(jīng)將common工程打包進(jìn)aar了啊淋肾,怎么回事?
pom腳本修改
原來爸邢,一個(gè)maven倉(cāng)庫不僅僅包含自己庫的代碼樊卓,還具有引用信息dependencies的聲明(這就是其方便所在),而這些dependencies都列在了與aar文件同目錄的.pom文件中杠河。上述的報(bào)錯(cuò)就是因?yàn)閷?dǎo)出sdk的aar時(shí)碌尔,pom文件中依舊保留了對(duì)common工程的依賴浇辜,因此要在gradle中手動(dòng)修改pom信息:
uploadArchives {
repositories {
mavenDeployer {
repository(url: uri('../../repo'))
pom.project{
groupId 'com.XXX'
version = android.defaultConfig.versionName
}
//去除對(duì)common的引用
pom.whenConfigured {pom ->
def common = pom.dependencies.find {dep -> dep.groupId == 'XXX' && dep.artifactId == 'common' }
pom.dependencies.remove(common)
}
}
}
}
然而僅僅這樣是不夠的,還會(huì)發(fā)生諸如這樣的報(bào)錯(cuò):
Caused by: java.lang.NoClassDefFoundError: com.squareup.okhttp.xxxx
因?yàn)槟闳绻娴拇蜷_pom文件唾戚,就會(huì)發(fā)現(xiàn)common工程引用的所有第三方庫(比如okhttp什么的)全柳洋!都!沒叹坦!有熊镣!聲!明募书!依绪囱!賴!于是只好在腳本中手動(dòng)修改pom文件(group name和包名請(qǐng)讀者自行替換):
task uploadArchivesNew(dependsOn: uploadArchives) {
doLast {
println "uploadArchivesNew..."
// Get existing pom file
def pomFileLocation = "../repo/com/xxx/sdk/" + android.defaultConfig.versionName + "/sdk-" + android.defaultConfig.versionName + ".pom"
Node xml = new XmlParser().parse(pomFileLocation)
def dependencies = xml.dependencies.first();
configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each {
//將common的引用加入pom(但不包括common項(xiàng)目本身)
if (it.moduleGroup.equals("xxx") && it.moduleName.equals("common")) {
it.allModuleArtifacts.each {
String moduleGroup = it.getModuleVersion().getId().getGroup()
String moduleName = it.getModuleVersion().getId().getName()
String moduleVersion = it.getModuleVersion().getId().getVersion()
if (!moduleGroup.equals("xxx") && !moduleName.equals("common")) {
def newDepNode = dependencies.appendNode('dependency')
newDepNode.appendNode('groupId', moduleGroup)
newDepNode.appendNode('artifactId', moduleName)
newDepNode.appendNode('version', moduleVersion)
newDepNode.appendNode('scope', 'compile')
}
}
}
}
// Overwrite existing pom file
new XmlNodePrinter(new PrintWriter(new FileWriter(pomFileLocation))).print(xml)
}
}
大功告成莹捡!每次運(yùn)行此task即可完成混淆鬼吵、打包、上傳至本地maven庫篮赢、修改pom信息齿椅,使用時(shí)直接在gradle中定義好maven庫地址,聲明引用即可:
allprojects {
repositories {
maven{url uri('../../repo')}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.xxx:sdk:0.0.7'
}
3. 導(dǎo)出包含所有引用jar包的eclipse工程
雖然大部分開發(fā)者在用Android Studio了启泣,但可能還有不少項(xiàng)目在eclipse里苦苦掙扎涣脚,本著方便開發(fā)者的思想,sdk還是打包出了eclipse版本寥茫,gradle腳本如下:
task releaseJar(type: Copy, dependsOn: 'build') {
from('build/intermediates/bundles/release/')
into('build/outputJar')
String jarName = 'xxx_' + android.defaultConfig.versionName + '.jar'
rename('classes.jar', jarName)
}
導(dǎo)出后在build/outputJar目錄下即可找到所有class涩澡、res等資源文件,將其創(chuàng)建成eclipse庫工程即可坠敷。
附:常見問題
1. so庫與ABI選擇
由于sdk包含了多個(gè)ABI(包括armeabi, armeabi-v7a, arm64-v8a, x86, x86_64)的so庫妙同,而應(yīng)用可能只采用了一種ABI(如armeabi-v7a),所以導(dǎo)致接入sdk后有些cpu架構(gòu)讀取不到應(yīng)用中的so文件膝迎。
解決辦法:在項(xiàng)目gradle腳本中添加:
android {
defaultConfig {
ndk {
abiFilters "armeabi-v7a"
}
}
}
這樣既可過濾sdk中其他ABI的so庫粥帚。
2. DuplicateFileException:
Error:Execution failed for task ':Grow:transformResourcesWithMergeJavaResForDebug'.
com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/NOTICE
File1: /Users/wlg/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.8.0/eeed20590bf2a6e367e6e5ce33f44d353881fa23/jackson-core-2.8.0.jar
File2: /Users/wlg/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.8.0/95505afd940fedb0d674a83583ae65a9c25ec9f/jackson-databind-2.8.0.jar
這是由于jar包中的META-INF/NOTICE沖突,解決辦法:在項(xiàng)目gradle腳本中添加:
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
參考文獻(xiàn)
android-fat-aar庫:https://github.com/adwiv/android-fat-aar
Android gradle User Guide:http://tools.android.com/tech-docs/new-build-system/user-guide
Gradle User Guide: https://docs.gradle.org/current/userguide/userguide.html
Gradle Java doc: https://docs.gradle.org/current/javadoc/
gradle執(zhí)行順序:https://docs.gradle.org/current/dsl/org.gradle.api.Project.html
maven plugin: https://docs.gradle.org/current/userguide/maven_plugin.html
Publishing to Maven: Remove dependency: http://gradle.1045684.n5.nabble.com/Publishing-to-Maven-Remove-dependency-td4381438.html
How to generate a maven pom using maven-publish plugin with actual version numbers?: https://discuss.gradle.org/t/how-to-generate-a-maven-pom-using-maven-publish-plugin-with-actual-version-numbers/5237
In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used: http://stackoverflow.com/questions/20959558/in-gradle-how-can-i-generate-a-pom-file-with-dynamic-dependencies-resolved-to-t