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

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

问题描述

总结:

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



详细信息: 我有一个Java SDK库项目,其中包含我们用于Java Web项目的代码,这个库使用 Gradle 创建,驻留在内部的nexus服务器(作为JAR)。



目标是通过一个AAR库为多个Android应用程序可以使用的JAR库提供一个Android配置版本,并最小化实现它的工作量(和样板代码)。这个AAR文件也被上传到nexus服务器以供Android应用程序项目使用。



我的AAR项目包括我的Java SDK库的gradle依赖项JAR),但是构建时,AAR不包含任何类。



代码:



这是Java SDK项目的gradle文件:

  apply plugin:'java' 

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

仓库{
mavenCentral()
}

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

依赖项{
testCompile组:'junit',名称:'junit',版本:'4.11'
testCompile'org.apache.directory.studio:org.apache.commons.io:2.4'
compile'org.springframework:spring-web:3.1.1.RELEASE'
compile'c​​om .google.code.gson:gson:2.3'
}

这是gradle文件对于我的AAR项目,请注意我将Maven仓库声明从它删除到我的联系服务器。我猜这对于这个问题应该没有关系。

  apply plugin:'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion23.0.1

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

依赖关系{
compile fileTree(dir:'libs',include:['* .jar'])
testCompile'junit:junit:4.12'
compile'c​​om.android.support:appcompat-v7 :23.1.0'

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

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

pre $ apply $ c $ app $'
$ b android {
compileSdkVersion 23
buildToolsVersion23.0.1

defaultConfig {
applicationIdcom.mycompany.application1
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName1.0
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(' 'proguard-android.txt'),'proguard-rules.pro'
}
}
}

依赖关系{
编译fileTree(dir:' libs',包括:['* .jar'])
testCompile'junit:junit:4.12'
compile'c​​om.android.support:appcompat-v7:23.1.0'

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

注意:我启动通过在AAR项目的lib目录中添加JAR来解决问题,但这是不希望的。它使联网服务器无用。我们可以在AAR项目的gradle文件中加入JAR的版本号并且在编译时自动更新。这是很好的做法。



注2: strong>我尝试在Android项目中向我的AAR依赖项添加 transitive = true ,但它没有解决任何问题,真正的问题是在构建AAR项目时,JAR项目代码不会捆绑在一起。

解决方案

您可以添加此任务:

 任务copyLibs(类型:复制){
from configurations.compile
到'libs'
}

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


Summary:

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

Details:

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).

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.

My AAR project includes a gradle dependency for my Java SDK library (the JAR) but when built, the AAR doesn't include any classes from it.

Code:

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'
}

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')
}

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')
}

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.

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.

解决方案

You can add this task:

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

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天全站免登陆