使用多个AAR / JAR创建AAR

时间:2015-12-23 23:43:33

标签: android-gradle dependency-management aar

我已经看到各种开发者发布的问题(Android Studio combine 2 .aar into one和其他人),但我没有看到一个明确的回应,这使我能够创建一个包含一个或多个AAR或JAR的AAR(我可以使用JAR)因为我不需要共享任何资源;只有类)。这是我的图书馆项目的app.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.google.code.gson:gson:2.1'
    compile ('libs/eventbus.jar')
    compile project(':core-release')
    compile project(':midware-release')
}

同样,这个应用程序是一个库项目,需要另外两个库项目('core-release','midware-release'),而我能够生成一个我可以在我的应用程序中使用的AAR文件,该应用程序无法找到依赖库项目的类,所以我不得不将两个库项目的AAR添加到我的应用程序中。

这是app.gradle应用程序项目(无需手动添加JAR),无法找到依赖项目的类:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.app.sample"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile files('libs/eventbus.jar')
    compile project(':sdk3-debug')
}

我认为库项目的AAR文件不会引入依赖项目(AAR或JAR),因此应用程序无法找到类。

我读过有关传递依赖的内容,但我无法找到可能对我的情况有帮助的示例实现。

3 个答案:

答案 0 :(得分:2)

  

我没有看到一个明确的回应,使我能够创建一个包含一个或多个AAR或JAR的AAR。

是的,我认为因为这个主题不仅限于AAR或JAR,而是Maven如何管理依赖。

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

  

虽然我能够生成一个我可以在我的应用程序中使用的AAR文件,但是应用程序无法找到依赖库项目的类,因此,我必须将两个库项目的AAR添加到我的应用程序中。

包含依赖项不是您的AAR责任,您的POM文件应该包含有关依赖项的信息。

https://maven.apache.org/pom.html

  

我认为库项目的AAR文件不会引入依赖项目(AAR或JAR),因此应用程序无法找到类。

正确,您仍需要在Application中包含库依赖项。

我假设您希望您的库可以被Application使用,而不指定库依赖项core-releasemidware-release。我在这里做了一个完整的解释android studio generate aar with dependency,但这是你需要做的:

  1. core-releasemidware-release上传到您的Maven资料库
  2. 为您的库创建一个包含依赖项的POM文件

    <project>
       <modelVersion>4.0.0</modelVersion>
       <parent>...</parent>
       <artifactId>okhttp</artifactId>
       <name>OkHttp</name>
       <dependencies>
          <dependency>
             <groupId>com.example</groupId>
             <artifactId>core-release</artifactId>
          </dependency>
          <dependency>
             <groupId>com.example</groupId>
             <artifactId>midware-release</artifactId>
          </dependency>
       </dependencies>
       <build>...</build>
    </project>
    
  3. 使用该POM文件发布您的AAR

    mvn deploy:deploy-file \
          -DgroupId=com.example \
          -DartifactId=your-library \
          -Dversion=1.0.1 \
          -Dpackaging=aar \
          -Dfile=your-library.aar \
          -DpomFile=path-to-your-pom.xml \
          -DgeneratePom=true \
          -DupdateReleaseInfo=true \
          -Durl="https://mavenUserName:mavenPassword@nexus.example.com/repository/maven-releases/"
    
  4. 然后您的应用程序可以使用您的库。 Gradle将自动下载您的库传递依赖项。

答案 1 :(得分:1)

这可以解决您的问题:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile files('libs/eventbus.jar')
    compile project(':sdk3-debug') { transitive = true }
}

true包含传递标记。

答案 2 :(得分:1)

我能够按照Stan Kurdziel的建议解决这个问题:stackoverflow.com/questions/30052058/multiple-aar-files以下是我为解决方案而采取的步骤:

  • 我创建了一个我发布到本地maven存储库的AAR
  • 现在可以在应用程序项目中访问此AAR
  • 此AAR还引用了核心和中间件库中的类。我遵循了Stan Kurdziel的方法#2:
  

手动将库项目2的依赖项添加到应用程序项目中 - 以便您的应用程序具有两个库的依赖项。根据您的具体情况,这可能是也可能不是可行的解决方案。

希望这有助于其他可能遇到类似问题的人。