问题描述
每当我将一个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?
推荐答案
您有不同的实现方法:
- 使用本地模块引用正确的路径
- 添加 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存储库.
通过这种方式,只需使用:
In this way, just use:
dependencies {
compile ('mypackage:mymodule:X.Y.Z')
}
这篇关于重用Android Studio中的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!