摇篮:我怎么包括一个依赖Java项目在Android构建一个本地罐子? [英] Gradle: how do I include a local jar from a dependent java project in an Android build?
问题描述
在我的Android应用程序,我得到一个 java.lang.NoClassDefFoundError的
执行code,引用code在相关的.jar时。我的项目包括机器人模块,以及一个纯Java的库模块,这是那里的罐子依赖性。我使用的摇篮1.10来构建项目。这里是我的项目布局:
In my Android app, I'm getting a java.lang.NoClassDefFoundError
when the code that references code in a dependent .jar is executed. My project includes an Android module as well as a java-only library module, which is where the jar dependency is. I'm using gradle 1.10 to build the project. Here is my project layout:
myProject
- app (Android)
- src
- build.gradle
- lib (java)
- src
- libs
- local-dependency.jar
- build.gradle
- build.gradle
- settings.gradle
主要项目 build.gradle
是空白,而主体工程 settings.gradle
是这样的:
The main project build.gradle
is blank while the main project settings.gradle
looks like:
include ':app', ':lib'
Android应用 build.gradle
是这样的:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "19.0.2"
defaultConfig {
minSdkVersion 18
targetSdkVersion 18
versionCode 1
versionName "1.0"
}
}
dependencies {
compile project(':lib')
}
库 build.gradle
是:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile <some-dependency-in-maven>
compile files('libs/local-dependency.jar')
}
一切都编译并没有错误的包,我没有看到在IDE中的任何错误(的IntelliJ 13)。出于某种原因,我的本地dependency.jar
是没有得到Android的编译过程中加入地塞米松,荷兰国际集团的过程。在 LIB
项目指定的任何Maven依赖被添加到Android的apk文件就好了;这只是我的本地JAR依赖。是否有什么我失踪?
Everything compiles and packages with no errors and I'm not seeing any errors in the IDE (IntelliJ 13). For some reason, my local-dependency.jar
is not getting added to the dex-ing process during the Android compile. Any maven dependencies specified in the lib
project get added to the Android .apk just fine; it's just my local jar dependency. Is there something I'm missing?
谢谢!
推荐答案
这是不能直接为当地的罐子不声明为在摇篮传递依赖。
This is not directly possible as local jars are not declared as transitive dependencies in Gradle.
您有两种选择:
- 合并两个罐子在Java库,以便输出包含本地罐子。
- 创建一个不同的项目,没有源代码,只是罐子,使项目依赖于它。
第二个选项让你有直接多个项目取决于当地瓶的能力(在它的上面成为一个传递依赖)。要做到这一点,创建一个新的摇篮的项目,只是把它 build.gradle
如下:
The second option gives you the ability to have more than one project depend directly on the local jar (on top of it becoming a transitive dependency). To do it, create a new gradle project and just put in its build.gradle
the following:
configurations.create("default")
artifacts.add("default", file('somelib.jar'))
这只是注册的水瓶中的项目发布的默认神器,这将在其它项目得到消耗。
This simply register your jar as the default artifact published by the project and this will get consumed by the other projects.
这篇关于摇篮:我怎么包括一个依赖Java项目在Android构建一个本地罐子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!