[英]Create a standalone library module in Android studio
Creating a standalone library application is a common task in Eclipse + ADT. 创建独立库应用程序是Eclipse + ADT中的常见任务。 I thought that this should be a simple task in Android Studio (1.2 or above), but after struggling with this problem for two days, I figured out that google did nothing about this and implementing a standalone library module is not as simple as I thought.
我认为这应该是Android Studio(1.2或更高版本)中的一个简单任务,但在经历了两天这个问题的努力之后,我发现google没有对此做任何事情并且实现一个独立的库模块并不像我想象的那么简单。 So I decided to share my experiences with you.
所以我决定与你分享我的经历。
To create a standalone and reusable library module in Android Studio: 要在Android Studio中创建独立且可重用的库模块,请执行以下操作:
1- Create a new project with no Activity. 1-创建没有活动的新项目。
2- New project's default module is named app
. 2-新项目的默认模块名为
app
。 Right click on module and refactor/rename it to something like 'library'. 右键单击模块并将其重构/重命名为“library”。 Close Android Studio.
关闭Android Studio。
3- Open file explorer and rename module's folder from app
to library
. 3-打开文件资源管理器并将模块的文件夹从
app
重命名为library
。
4- Open .idea
folder. 4-打开
.idea
文件夹。 There are some XML files there that have references to app
folder. 那里有一些XML文件引用了
app
文件夹。 Replace app
into library
in these files. 将
app
替换为这些文件中的library
。
5- Open module's gradle file (library/build.gradle) and change plugin to com.android.library
. 5-打开模块的gradle文件(library / build.gradle)并将插件更改为
com.android.library
。 Then remove applicationId
. 然后删除
applicationId
。
6- Open Android Studio. 6-打开Android Studio。 Rebuild module.
重建模块。 If there is no error, we are done here.
如果没有错误,我们就在这里完成。
7- Open application which is dependent on that module. 7-打开依赖于该模块的应用程序。 Open
settings.gradle
and include library
module as below: 打开
settings.gradle
并包含library
模块,如下所示:
include ':library'
project(':library').projectDir = new File('/Path/To/LibraryProject/library')
8- Open application's app
module build.gradle
file and add this line into dependencies
section: 8-打开应用程序的
app
模块build.gradle
文件,并将此行添加到dependencies
部分:
compile project(':library')
9- Rebuild your project. 9-重建您的项目。 If everything is right, you will see
library
module in your project. 如果一切正常,您将在项目中看到
library
模块。 You can edit library
module from there and/or its project and more important: Now you have a standalone library module that you can reuse in multiple projects! 您可以从那里和/或其项目编辑
library
模块,更重要的是:现在您有一个可以在多个项目中重用的独立库模块!
I hope google will make this process a lot easier in future releases of Android Studio! 我希望谷歌能够在未来的Android Studio版本中轻松完成这一过程!
EDIT: 编辑:
I checked Android Studio version 1.4 and hopefully in this version we can omit steps 3 and 4. 我检查了Android Studio 1.4版,希望在这个版本中我们可以省略步骤3和4。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.