将本地库项目作为依赖添加到 Android Studio 中的多个项目

将本地库项目作为依赖添加到 Android Studio 中的多个项目

问题描述:

我通过选择 File > 在 Android Studio(当前为 0.5.2)中创建了一个库项目.新项目... >将此项目标记为库".

我还有另外两个非库项目,我想向该库项目添加依赖项.

I have two other non-library projects that I would like to add a dependency to this library project.

-My Library
-Project 1 (depends on My Library)
-Project 2 (depends on My Library)

我的目标是保持每个项目独立,避免重复模块/代码.如果不将库模块复制到其他项目中,如何做到这一点?

My goal is to keep each project independent and avoid duplicating modules/code. How can this be done without copying the library module into the other projects?

更新:Android Studio 0.6.0 允许您导入模块,不过,这只是将模块源复制到项目中.

Update: Android Studio 0.6.0 allows you to Import a module, though, this simply copies the module source into the Project.

您还可以使用 project().projectDir 属性引用项目文件夹之外的库.如果您的外部库与您的项目相关

You can also refer to a library outside of your project folder using the project().projectDir property. If your external library is relative to your project like so

- MyLibrary
   - library
- MyProject
    - app

在 MyProject/settings.gradle

in MyProject/settings.gradle

include ':library'
project(':library').projectDir = new File(settingsDir, '../MyLibrary/library')

在 MyProject/app/build.gradle

in MyProject/app/build.gradle

dependencies {
   compile project(':library')
}