Java 需要另一个目录中的 Gradle 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19299316/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:49:13  来源:igfitidea点击:

Require Gradle project from another directory

javaandroidgradleandroid-librarymulti-project

提问by Euan T

I have a directory/project setup like this:

我有一个这样的目录/项目设置:

C:\
    _dev\
        Projects\
            Logger
            MyProject

Loggeris an Android library project using Gradle. MyProjectis a standard Android project project that needs to make use of the Loggerlibrary.

Logger是一个使用 Gradle 的 Android 库项目。MyProject是一个标准的 Android 项目项目,需要使用Logger库。

I am using Android Studio and have tried adding Loggerto the external libraries. Whilst this works during development, I get messages about the class not being found when building.

我正在使用 Android Studio 并尝试将Logger添加到外部库。虽然这在开发过程中有效,但我会收到有关在构建时找不到该类的消息。

I'm completely new to Gradle, but have tried the following in my build.gradle within MyProject:

我是 Gradle 的新手,但在MyProject中的 build.gradle 中尝试了以下内容:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 18
    }

    dependencies {
        compile files("../Logger")
    }
}

dependencies {
    compile 'com.android.support:gridlayout-v7:18.0.0'
    compile 'com.android.support:appcompat-v7:18.0.0'
}

采纳答案by Rene Groeschke

The simplest way is to make MyProjecta multi project with the Loggerproject as a subproject.

最简单的方法是MyProjectLogger项目作为子项目制作一个多项目。

settings.gradlein MyProjectdirectory:

settings.gradleMyProject目录中:

include ":logger"
project(":logger").projectDir = file("../logger")

In the build.gradleof MyProjectyou can now reference this lib as a project:

build.gradleMyProject,你现在可以参考这个LIB作为一个项目:

dependencies {
     compile 'com.android.support:gridlayout-v7:18.0.0'
     compile 'com.android.support:appcompat-v7:18.0.0'
     compile project(":logger")
}

回答by miw

Try adding the dependency to the global "dependencies" section, not the "android > dependencies". During development, the "android" configuration is used, but not to package the runtime.

尝试将依赖项添加到全局“依赖项”部分,而不是“android > 依赖项”。在开发过程中,使用“android”配置,但不打包运行时。

dependencies {
    compile 'com.android.support:gridlayout-v7:18.0.0'
    compile 'com.android.support:appcompat-v7:18.0.0'
    compile files("../Logger")
}

It may also be worthwhile to look into setting up a multi-project gradle configuration, with a build.gradle and settings.gradle in the shared parent directory like here: http://www.gradle.org/docs/current/userguide/multi_project_builds.html

考虑设置多项目 gradle 配置也可能值得,在共享父目录中使用 build.gradle 和 settings.gradle,如下所示:http: //www.gradle.org/docs/current/userguide/ multi_project_builds.html

回答by Dave Tyler

Android Studio 2.2.3:

安卓工作室 2.2.3:

Add to settings.gradle.

添加到 settings.gradle。

include ':app', ':new_lib'
project(':new_lib').projectDir = new File('../new_lib/app')
  • The path must be relative from the root of the project you're working on.
  • The module you're referencing must have a reference to it's "app" directory.
  • 该路径必须相对于您正在处理的项目的根目录。
  • 您引用的模块必须引用它的“app”目录。

Then edit your Project Structure | Modules to setup dependencies.

然后编辑您的项目结构 | 用于设置依赖项的模块。