Java 如何将 JAR 依赖项包含到 AAR 库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33445270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include JAR dependency into an AAR library
提问by rastadrian
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.
我有一个依赖于 JAR 文件的 AAR 文件,当我构建 AAR 项目时,它不包含 JAR 代码。
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).
我有一个 Java SDK 库项目,其中包含我们用于 Java Web 项目等的代码,该库是使用Gradle
内部连接服务器(作为 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.
目标是通过 AAR 库提供此 JAR 库的“Android 配置”版本,多个 Android 应用程序可以使用该库并最大限度地减少实现它的工作量(和样板代码)。此 AAR 文件也会上传到 nexus 服务器以供 Android 应用程序项目使用。
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.
我的 AAR 项目包含我的 Java SDK 库(JAR)的 gradle 依赖项,但在构建时,AAR 不包含其中的任何类。
Code:
代码:
This is the Java SDK project's gradle file:
这是 Java SDK 项目的 gradle 文件:
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.
这是我的 AAR 项目的 gradle 文件,请注意,我从中删除了对我的 nexus 服务器的 Maven 存储库声明。对于这个问题,我想这应该无关紧要。
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:
这是我的 Android 项目的 gradle 文件,我再次删除了 nexus 服务器引用:
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.
注意:我最初通过在 AAR 项目的 lib 目录中添加 JAR 解决了这个问题,但这是不希望的。它使拥有 nexus 服务器变得毫无用处。最好我们可以在 AAR 项目的 gradle 文件中增加 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.
NOTE2:我尝试transitive=true
在 Android 项目中添加我的 AAR 依赖项,但它没有解决任何问题,真正的问题是在构建 AAR 项目时,JAR 项目代码没有被捆绑。
采纳答案by Héctor
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
.
依赖项将从您的 Nexus 下载,但是当您需要打包库时,首先执行此任务,jar
文件将被复制并包含在 final 中aar
。
回答by nayan
By default, AAR does not include any dependencies. Solution mentioned by @Hector should work for gradle plugin < 3.0. For Gradle plugin 3.0+, try custom config as mentioned here.
默认情况下,AAR 不包含任何依赖项。@Hector 提到的解决方案应该适用于 gradle 插件 < 3.0。对于 Gradle 插件 3.0+,请尝试此处提到的自定义配置。
android { ... }
// Add a new configuration to hold your dependencies
configurations {
myConfig
}
dependencies {
....
myConfig 'com.android.support:appcompat-v7:26.1.0'
myConfig 'com.android.support:support-v4:26.1.0'
...
}
task copyLibs(type: Copy) {
from configurations.myConfig
into "libs"
}
回答by stepio
None of the suggestions helped me for Gradle 4.6, so I wasted the whole day inventing my own.
对于 Gradle 4.6,这些建议都没有帮助我,所以我浪费了一整天的时间来发明自己的建议。
Eventually I found a good Gist and modified it for my version of Gradle: https://gist.github.com/stepio/824ef073447eb8d8d654f22d73f9f30b
最终我找到了一个很好的 Gist 并为我的 Gradle 版本修改了它:https: //gist.github.com/stepio/824ef073447eb8d8d654f22d73f9f30b