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

时间:2020-12-21 15:03:50

I have a directory/project setup like this:

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

C:\
    _dev\
        Projects\
            Logger
            MyProject

Logger is an Android library project using Gradle. MyProject is a standard Android project project that needs to make use of the Logger library.

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

I am using Android Studio and have tried adding Logger to 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'
}

3 个解决方案

#1


98  

The simplest way is to make MyProject a multi project with the Logger project as a subproject.

最简单的方法是使用Logger项目作为子项目使MyProject成为一个多项目。

settings.gradle in MyProject directory:

MyProject目录中的settings.gradle:

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

In the build.gradle of MyProject you can now reference this lib as a project:

在MyProject的build.gradle中,您现在可以将此lib作为项目引用:

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

#2


7  

Android Studio 2.2.3:

Android Studio 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.

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

#3


4  

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

#1


98  

The simplest way is to make MyProject a multi project with the Logger project as a subproject.

最简单的方法是使用Logger项目作为子项目使MyProject成为一个多项目。

settings.gradle in MyProject directory:

MyProject目录中的settings.gradle:

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

In the build.gradle of MyProject you can now reference this lib as a project:

在MyProject的build.gradle中,您现在可以将此lib作为项目引用:

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

#2


7  

Android Studio 2.2.3:

Android Studio 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.

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

#3


4  

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