如何将 JAR 依赖项包含到 AAR 库中 [英] How to include JAR dependency into an AAR library

查看:41
本文介绍了如何将 JAR 依赖项包含到 AAR 库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:

我有一个依赖于 JAR 文件的 AAR 文件,当我构建 AAR 项目时,它不包含 JAR 代码.

I have an AAR file that depends on a JAR file, when I build the AAR project, it doesn't contain the JAR code.

详情:

我有一个 Java SDK 库项目,其中包含我们用于 Java Web 项目等的代码,该库是使用 Gradle 创建的,并驻留在内部连接服务器(作为 JAR)中.

I have a Java SDK library project that contains code that we use for Java web projects and such, this library is created using Gradle and resides in an internal nexus server (as a JAR).

目标是通过多个 Android 应用程序可以使用的 AAR 库提供此 JAR 库的Android 配置"版本,并最大限度地减少实现它的工作量(和样板代码).此 AAR 文件也会上传到 nexus 服务器以供 Android 应用程序项目使用.

The goal is to provide an "Android configured" version of this JAR library through an AAR library that multiple Android Applications can use and minimize the effort (and boilerplate code) to implement it. This AAR file is also uploaded to the nexus server to be used by the Android Application projects.

我的 AAR 项目包含我的 Java SDK 库(JAR)的 gradle 依赖项,但在构建时,AAR 不包含其中的任何类.

代码:

这是 Java SDK 项目的 gradle 文件:

This is the Java SDK project's gradle file:

apply plugin: 'java'

//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.7
version = '1.1.1'

repositories {
    mavenCentral()
}

jar {
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile 'org.apache.directory.studio:org.apache.commons.io:2.4'
    compile 'org.springframework:spring-web:3.1.1.RELEASE'
    compile 'com.google.code.gson:gson:2.3'
}

这是我的 AAR 项目的 gradle 文件,请注意,我从中删除了对我的 nexus 服务器的 Maven 存储库声明.对于这个问题,我想这应该无关紧要.

This is the gradle file for my AAR Project, note that I removed the Maven repository declarations to my nexus server from it. I guess it shouldn't matter for the sake of this question.

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "2.2.2"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'

    compile ('com.mycompany:javasdk:1.1.1')
}

这是我的 Android 项目的 gradle 文件,我再次删除了 nexus 服务器引用:

This is the gradle file for my Android Project, again I removed the nexus server references:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.mycompany.application1"
        minSdkVersion 16
        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.0'

    compile ('com.mycompany:androidsdk:2.2.2@aar')
}

注意:我最初通过在 AAR 项目的 lib 目录中添加 JAR 解决了该问题,但这是不希望的.它使拥有 nexus 服务器变得毫无用处.最好我们可以在 AAR 项目的 gradle 文件中增加 JAR 的版本号,并且更新会在编译时自动发生.

NOTE: I initially solved the issue by adding the JAR in the lib directory of the AAR project, but this is undesired. It makes having a nexus server useless. It would be good that we can just bump the JAR's version number in the AAR project's gradle file and the update happens automatically at compile time.

注意2:我尝试在 Android 项目中将 transitive=true 添加到我的 AAR 依赖项中,但没有解决任何问题,真正的问题是在构建 AAR 时项目,JAR 项目代码没有被捆绑.

NOTE2: I tried adding transitive=true to my AAR dependency in the Android Project but it didn't solved anything, the real issue is that when building the AAR project, the JAR project code doesn't get bundled.

推荐答案

您可以添加此任务:

task copyLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

依赖项将从您的 Nexus 下载,但是当您需要打包库时,首先执行此任务,jar 文件将被复制并包含在最终的 aar 中.

Dependencies will be downloaded from your Nexus, but when you need package the library, execute this task first and jar files will be copied and included inside final aar.

这篇关于如何将 JAR 依赖项包含到 AAR 库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆