Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a standalone library module in Android studio

Creating a standalone library application is a common task in 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. So I decided to share my experiences with you.

like image 859
Ali Behzadian Nejad Avatar asked Jun 07 '15 15:06

Ali Behzadian Nejad


1 Answers

To create a standalone and reusable library module in Android Studio:

1- Create a new project with no Activity.

2- New project's default module is named app. Right click on module and refactor/rename it to something like 'library'. Close Android Studio.

3- Open file explorer and rename module's folder from app to library.

4- Open .idea folder. There are some XML files there that have references to app folder. Replace app into library in these files.

5- Open module's gradle file (library/build.gradle) and change plugin to com.android.library. Then remove applicationId.

6- Open Android Studio. Rebuild module. If there is no error, we are done here.

7- Open application which is dependent on that module. Open settings.gradle and include library module as below:

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:

compile project(':library')

9- Rebuild your project. If everything is right, you will see library module in your project. 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!

I hope google will make this process a lot easier in future releases of Android Studio!

EDIT:

I checked Android Studio version 1.4 and hopefully in this version we can omit steps 3 and 4.

like image 160
2 revs Avatar answered Sep 19 '22 16:09

2 revs