Android導(dǎo)出aar時嵌套引用的那些坑

最近寫了個Android SDK工程,在代碼、測試統(tǒng)統(tǒng)完成后,居然在導(dǎo)出的一步折騰了兩三天,在此總結(jié)下查找資料的過程和結(jié)果,引以借鑒。
首先,這次趟坑解決了以下問題:

  1. 導(dǎo)出aar至本地Maven庫,包含引用的Module工程
  2. 導(dǎo)出eclipse適用的庫工程,并包含所有引用的jar包(包括嵌套引用)

1. 通用導(dǎo)出方式

通常來講,一個簡單的Android Library工程,導(dǎo)出aar有這幾種方式:

  1. 編譯后自動會在build/outputs/aar目錄下生成.aar文件。此aar僅打包了Library工程的class、libs和資源文件,但Library引用的其他庫(比如compile "com.squareup.okhttp3:okhttp:3.4.1")并未包含在aar中。使用Library時還需把它引用的庫再手動聲明一遍,差評!
  2. 發(fā)布到本地Maven庫(或jCenter、MavenCentral)。發(fā)布出來的內(nèi)容除了aar還包含了Library的所有dependencies信息。使用時直接設(shè)置好maven庫地址,聲明引用Libraray,gradle就會幫你自動引用Library中嵌套引用的所有dependencies了。

我的需求:
然而,我的SDK Library引用了自己的另一個Module工程common(用來提供基礎(chǔ)功能,服務(wù)于不同的項(xiàng)目),但導(dǎo)出的aar無法包含引用的common工程,我又不希望單獨(dú)導(dǎo)出common工程讓外部調(diào)用,這可怎么辦?

2. 導(dǎo)出包含嵌套引用的aar

一翻Google后在Github上找到一個庫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)建個demo工程使用此maven庫發(fā)現(xiàn)此類報(bào)錯(涉及包名處均用'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)錯demo工程找不到common工程的引用,但我已經(jīng)將common工程打包進(jìn)aar了啊,怎么回事?


pom腳本修改
原來,一個maven倉庫不僅僅包含自己庫的代碼,還具有引用信息dependencies的聲明(這就是其方便所在),而這些dependencies都列在了與aar文件同目錄的.pom文件中。上述的報(bào)錯就是因?yàn)閷?dǎo)出sdk的aar時,pom文件中依舊保留了對common工程的依賴,因此要在gradle中手動修改pom信息:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: uri('../../repo'))
            pom.project{
                groupId 'com.XXX'
                version = android.defaultConfig.versionName
            }
            //去除對common的引用
            pom.whenConfigured {pom ->
                def common = pom.dependencies.find {dep -> dep.groupId == 'XXX' && dep.artifactId == 'common' }
                pom.dependencies.remove(common)
            }
        }
    }
}

然而僅僅這樣是不夠的,還會發(fā)生諸如這樣的報(bào)錯:

Caused by: java.lang.NoClassDefFoundError: com.squareup.okhttp.xxxx

因?yàn)槟闳绻娴拇蜷_pom文件,就會發(fā)現(xiàn)common工程引用的所有第三方庫(比如okhttp什么的)全!都!沒!有!聲!明!依!賴!于是只好在腳本中手動修改pom文件(group name和包名請讀者自行替換):

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信息,使用時直接在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包含了多個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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容