Android Studio:在项目根目录之外添加库 [英] Android Studio: Adding a library outside of the project root
问题描述
我有两个项目,A和B,并希望通过库项目C之间共享一些代码。这样做的正确方法是什么:
I have two projects, A and B, and would like to share some code between them via a library project, C. What is the proper way to do this such that:
- A和B以及C都在不同的项目中。 (根本不在同一目录中)
- 对C的更改在A或B中自动使用,无需重新导入C.
我是Android Studio的新手,我在这里遇到过各种各样的问题。我真的只想使用位于项目根目录之外的源代码。谢谢!
I'm fairly new to Android Studio and I've been running into all sorts of issues here. I really just want to use source code that is located outside the project root. Thanks!
推荐答案
这听起来像问题(并回答)这里。
This sounds like the question asked (and answered) here.
总结斯科特Barta的回答:
To sum up Scott Barta's answer:
settings.gradle:
include ':module-custom-lib'
project(':module-custom-lib').projectDir = new File(settingsDir, '../../../libraryProject/workspace/projectSrc')
A和B的 build.gradle:
dependencies {
compile project(':module-custom-lib')
}
因此,假设文件结构如下:
So, assuming a file structure like this:
+-- AndroidStudioProjects
| +-- CoreLibs
| +-- app (empty)
| +-- myJavaCoreLib
| +-- anotherJavaCoreLib
| +-- AndroidApp1
| +-- app
| +-- AndroidApp2
| +-- app
| +-- AndroidApp3
| +-- app
...代码如下:
settings.gradle:
include ':coreLib'
project(':coreLib').projectDir = new File(settingsDir, '../CoreLibs/myJavaCoreLib')
AndroidApp中的
build.gradle [1,2和3] (您的A和B):
build.gradle in AndroidApp[1, 2 and 3] (your A's and B's):
compile project(':coreLib')
这篇关于Android Studio:在项目根目录之外添加库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!