【问题标题】:Create an AAR with multiple AARs/JARs创建具有多个 AAR/JAR 的 AAR
【发布时间】:2015-12-23 23:43:32
【问题描述】:

我看到了各种开发人员发布的问题(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),因此应用程序无法找到类。

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

【问题讨论】:

    标签: android-gradle-plugin dependency-management aar


    【解决方案1】:

    我还没有看到一个明确的回应,使我能够创建一个包含 1 个或多个 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),因此应用程序无法找到类。

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

    我假设您希望应用程序可以使用您的库,而无需指定您的库依赖项 core-release 和 midware-release。我在这里做了完整的解释android studio generate aar with dependency,但这是你需要做的:

    1. 将core-release 和midware-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 会自动下载你的库传递依赖。

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 我试过了,但我无法通过No such property: transitive for class: org.gradle.api.internal.project.DefaultProject_Decorated 错误。即使在构建工具中切换到默认的 gradle 包装器 -> 从本地 gradle 分发中的 Gradle 设置也无济于事
        • 你能不能试试:compile (project(':sdk3-debug')) { transitive = true }
        • 这种新格式确保 gradle 现在很开心。但是,我的应用程序项目无法在 AAR 文件中的库项目(中间件)中找到类。直接在 AAR 文件中的类是可访问的。正如我所提到的,我的 AAR 文件有两个依赖项目(核心和中间件)。
        • 您可以尝试使用传递标志更新库项目依赖项并重试吗?
        • 为此,我建议您使用jitpack.io,请不要忘记将我的答案标记为正确:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-24
        • 1970-01-01
        • 2015-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多