Gradle:如何在 Android 构建中包含来自依赖 Java 项目的本地 jar? [英] Gradle: how do I include a local jar from a dependent java project in an Android build?

查看:40
本文介绍了Gradle:如何在 Android 构建中包含来自依赖 Java 项目的本地 jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android 应用程序中,当执行引用依赖 .jar 中的代码的代码时,我收到一个 java.lang.NoClassDefFoundError.我的项目包括一个 Android 模块以及一个仅限 java 的库模块,这是 jar 依赖项所在的位置.我正在使用 gradle 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 看起来像:

The Android app build.gradle looks like:

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) 中没有看到任何错误.出于某种原因,我的 local-dependency.jar 在 Android 编译期间没有被添加到 dex-ing 过程中.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?

谢谢!

推荐答案

这不是直接可行的,因为本地 jar 未在 Gradle 中声明为传递依赖项.

This is not directly possible as local jars are not declared as transitive dependencies in Gradle.

您有两个选择:

  • 合并 Java 库中的两个 jar,以便输出包含本地 jar.
  • 创建一个没有源,只有 jar 的不同项目,并使项目依赖于它.

第二个选项使您能够让多个项目直接依赖于本地 jar(在它之上成为传递依赖).为此,创建一个新的 gradle 项目,并在其 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'))

这只是将您的 jar 注册为项目发布的默认工件,这将被其他项目使用.

This simply register your jar as the default artifact published by the project and this will get consumed by the other projects.

这篇关于Gradle:如何在 Android 构建中包含来自依赖 Java 项目的本地 jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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