创建一个包含多个 AARs/JARs 的 AAR

Create an AAR with multiple AARs/JARs

我看到了各种开发人员发布的问题(Android Studio combine 2 .aar into one 和其他问题),但我还没有看到明确的答复,使我能够创建一个包含 1 个或多个 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),因此应用程序无法找到 类。

我阅读了有关传递依赖性的内容,但找不到可能对我的情况有所帮助的示例实现。

这应该可以解决您的问题:

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 处包含传递标志。

我按照 Stan Kurdziel 的建议解决了这个问题:whosebug.com/questions/30052058/multiple-aar-files 以下是我为达成解决方案所采取的步骤:

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

Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.

希望这可以帮助可能 运行 遇到类似问题的其他人。

I haven't seen a definitive response that enables me to create an AAR that includes 1 or more AARs or JARs.

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

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

while I was able to generate one AAR file that I can use in my application, the application was unable to find the dependent library projects' classes so, I had to add the two library projects' AARs into my application.

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

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

I don't think the library project's AAR file is pulling in the dependent projects (AAR or JAR) and hence the application is unable to find the classes.

正确,您仍然需要在您的应用程序中包含库依赖项。

我假设您希望应用程序可以使用您的库,而不指定您的库依赖项 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/"
    

然后您的应用程序就可以使用您的库了。 Gradle 将自动下载您的库传递依赖项。