在 Android Studio 中重用模块 [英] Reuse modules in Android Studio
问题描述
每当我将一个 android library-project 作为模块添加到我的 Android Studio 项目时,源代码都会被复制到 Android Studio 项目文件夹中.
Whenever I add a android library-project as module to my Android Studio project, the sources get copied to the Android Studio project folder.
有没有办法像eclipse那样解决,库项目只有一个副本,有多少个项目可以引用?
Is there a way to solve it like in eclipse, where there is only one copy of library project, any many projects can reference it?
推荐答案
你有不同的方法来实现它:
You have different ways to achieve it:
- 使用本地模块引用正确的路径
- 添加aar文件
- 使用 maven 存储库
- using a local module referring the right path
- adding an aar file
- using a maven repo
案例 1:
使用 gradle 和本地库,您可以在项目中引用外部模块.
CASE 1:
Using gradle and a local library, inside a project you can refer an external module.
只需使用:
Project
|__build.gradle
|__settings.gradle
|__app (application module)
|__build.gradle
在 settings.gradle
中:
include ':app'
include ':myLib'
project(':myLib').projectDir=new File('pathLibrary')
在app/build.gradle
中:
dependencies {
compile project(':myLib')
}
关注myLib.
您必须使用其他项目中的库路径,而不是项目的根目录.
Pay attention to myLib.
You have to use the path of the library inside the other project, not the root of the project.
案例 2:
编译库模块,得到aar文件,然后添加到主工程中:
CASE 2:
Compile the library module, get the aar file, and then add to the main project:
添加放置 aar 文件的文件夹作为存储库:
Add the folder where you put the aar file as repository:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
然后添加依赖:
dependencies {
compile(name:'nameOfAarFile', ext:'aar')
}
案例 3:另一种方法是将您的模块发布到 Maven 存储库中.
这样,只需使用:
dependencies {
compile ('mypackage:mymodule:X.Y.Z')
}
这篇关于在 Android Studio 中重用模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!