Gradle:如何在Android构建中包含一个来自依赖java项目的本地jar ?

时间:2023-01-13 13:27:12

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:

在我的Android应用中,我得到了java.lang。当引用依赖的.jar中的代码时执行NoClassDefFoundError。我的项目包括一个Android模块和一个只有java的库模块,这是jar依赖的地方。我正在使用第1.10级来构建这个项目。这是我的项目布局:

myProject
- app (Android)
  - src
  - build.gradle

- lib (java)
  - src     
  - libs
    - local-dependency.jar
  - build.gradle

- build.gradle
- settings.gradle

The main project build.gradle is blank while the main project settings.gradle looks like:

主要的项目构建。当主项目设置时,gradle是空白的。它看起来像:

include ':app', ':lib'

The Android app build.gradle looks like:

Android应用程序构建。它看起来像:

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

The library build.gradle is:

图书馆建设。它是:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile <some-dependency-in-maven>
    compile files('libs/local-dependency.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?

所有的编译和包都没有错误,我在IDE中没有看到任何错误(IntelliJ 13)。出于某种原因,我的本地依赖。在Android编译期间,jar没有被添加到dex处理过程中。lib项目中指定的任何maven依赖项都可以添加到Android .apk中;这只是我的本地jar依赖项。我是不是漏掉了什么?

Thanks!

谢谢!

2 个解决方案

#1


5  

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

这是不可能的,因为本地jar在Gradle中没有被声明为传递依赖项。

You have two options:

你有两个选择:

  • merge the two jars in your java library so that the output contains the local jar.
  • 合并java库中的两个jar,使输出包含本地jar。
  • create a different project with no source, only the jar, and make the project depend on it.
  • 创建一个没有源、只有jar的不同项目,并使项目依赖于它。

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:

第二个选项使您能够让多个项目直接依赖于本地jar(在其之上成为传递性依赖)。为此,创建一个新的gradle项目,然后进行构建。gradle以下:

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.

这只需将jar注册为项目发布的默认构件,其他项目将使用它。

#2


0  

In recent (4+, IIRC) versions of gradle, you can achieve this with the following configuration:

在最近的(4+,IIRC)版本的gradle中,你可以通过以下配置实现:

compile (project(':ProjectIDependOn')) {
  transitive = true
}

Setting transitive to true when depending on another project will expose all of that project's libraries to this project.

在依赖于另一个项目时,将transitive设置为true将向这个项目公开该项目的所有库。

#1


5  

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

这是不可能的,因为本地jar在Gradle中没有被声明为传递依赖项。

You have two options:

你有两个选择:

  • merge the two jars in your java library so that the output contains the local jar.
  • 合并java库中的两个jar,使输出包含本地jar。
  • create a different project with no source, only the jar, and make the project depend on it.
  • 创建一个没有源、只有jar的不同项目,并使项目依赖于它。

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:

第二个选项使您能够让多个项目直接依赖于本地jar(在其之上成为传递性依赖)。为此,创建一个新的gradle项目,然后进行构建。gradle以下:

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.

这只需将jar注册为项目发布的默认构件,其他项目将使用它。

#2


0  

In recent (4+, IIRC) versions of gradle, you can achieve this with the following configuration:

在最近的(4+,IIRC)版本的gradle中,你可以通过以下配置实现:

compile (project(':ProjectIDependOn')) {
  transitive = true
}

Setting transitive to true when depending on another project will expose all of that project's libraries to this project.

在依赖于另一个项目时,将transitive设置为true将向这个项目公开该项目的所有库。