简体   繁体   English

打包android库,并在生成的aar中包含其他库

[英]Packaging android library and includes others libraries in generated aar

I would like to build an android library including others libraries. 我想建立一个包括其他库的android库。 I have 2 workspaces like this : 我有2个这样的工作区:

/workspace1
  /libA
  /myLib

/workspace2
  /myApp

The android library myLib depends on libA. android库myLib取决于libA。

The android application myApp depends on myLib (and use classes from libA). android应用程序myApp依赖于myLib(并使用libA中的类)。

I would like to generate AAR for myLib to my local maven repository (it's OK). 我想为myLib生成AAR到我的本地Maven存储库(可以)。 But the generated AAR does not contain classes and resources from libA. 但是生成的AAR不包含来自libA的类和资源。

Please see the build.gradle under workspace1 : 请参阅workspace1下的build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath 'com.github.dcendents:android-maven-plugin:1.0'
    }
}

def groupVal = "com.mycompany.android"
def versionVal = "0.1-SNAPSHOT"

def compileSdkVersionVal = 19
def buildToolsVersionVal = "19.0.3"
def minSdkVersionVal = 10
def targetSdkVersionVal = 19

allprojects {
    group = groupVal
    version = versionVal

    repositories {
        mavenCentral()
        mavenLocal()
    }

}

project(':libA') {
    apply plugin: 'android-library'

    android {
        compileSdkVersion compileSdkVersionVal
        buildToolsVersion buildToolsVersionVal

        defaultConfig {
            minSdkVersion minSdkVersionVal
            targetSdkVersion targetSdkVersionVal
            versionCode 1
            versionName versionVal
        }

        lintOptions {
            abortOnError false
        }

        packagingOptions {
            exclude 'META-INF/ASL2.0'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
        }

    }

}


project(':myLib') {
    apply plugin: 'android-library'
    apply plugin: 'android-maven'

    android {
        compileSdkVersion compileSdkVersionVal
        buildToolsVersion buildToolsVersionVal

        defaultConfig {
            minSdkVersion minSdkVersionVal
            targetSdkVersion targetSdkVersionVal
            versionCode 1
            versionName versionVal
        }

        lintOptions {
            abortOnError false
        }

        packagingOptions {
            exclude 'META-INF/ASL2.0'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
        }

    }
    dependencies {
        compile project(':libA') {
            export = true
        }
    }
}

Do you have any suggestions ? 你有什么建议吗 ?

Many thanks, 非常感谢,

Leo 狮子座

You should depend on libA.jar instead of libA.aar in your myLib project. 您应该在myLib项目中依赖libA.jar而不是libA.aar。 Then, assuming you are building the apk through myApp/pom.xml, you should depend on libA.aar in that pom file. 然后,假设您要通过myApp / pom.xml构建apk,则应依赖该pom文件中的libA.aar。

  • If libA.jar does not exists, you have to manually create and install it into your maven repository (eg, .m2) using 'maven install ...' command to be able to depend on it. 如果libA.jar不存在,则必须使用“ maven install ...”命令手动创建并将其安装到maven存储库(例如.m2)中,以便能够依赖它。

If you want myLib to contain the classes and resources of it's dependencies, you need to create a so called 'fatJar' from it. 如果希望myLib包含其依赖项的类和资源,则需要从中创建一个所谓的“ fatJar”。 This means that the all, (or selected) dependencies will be packaged into single archive alongside the code itself. 这意味着所有(或选定的)依赖项都将与代码本身一起打包到单个存档中。

Beware, that this way the consumer of the library will lose the ability to dynamically adjust dependency versions, because they'll be stuck with what you packaged into your library. 当心,以这种方式,库的使用者将失去动态调整依赖项版本的能力,因为它们将卡在打包到库中的内容上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM