Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert existing project to library project in Android Studio

How can I convert an existing Android project into an Android library project in Android Studio? In Eclipse, that is possible.

Actually, I want to convert an old Android project into an Android library project so that I can use existing code of that Android project to build a new Android project with minor changes in Android Studio.

like image 414
vijay Avatar asked Jul 12 '13 11:07

vijay


People also ask

How do I create a library project in Android Studio?

Using library projects helps you to structure your application code. To create a new library module in Android Studio, select File New Module and select Android Library .

How do I copy one Android project to another?

Select your project then go to Refactor -> Copy... . Android Studio will ask you the new name and where you want to copy the project. Provide the same. After the copying is done, open your new project in Android Studio.


2 Answers

In your module's build.gradle file (not the root project, if you use modules!), simply replace:

apply plugin: 'com.android.application' // or, if you're on an old version apply plugin: 'android' // note: this one is deprecated 

...with:

apply plugin: 'com.android.library' // or, if you're on an old version apply plugin: 'android-library' // note: this one is deprecated 

Note that recently, 'android' has changed to 'com.android.application', while 'android-library' has been changed to 'com.android.library'. Avoid using the old names in new projects.

After updating your build.gradle file, you should Sync Project with Gradle files (which is in the toolbar), as not doing it might result in errors and things not working correctly.

Android Studio will then update some files to indicate that the module is now a library; as this will be added into your .iml file:

<option name="LIBRARY_PROJECT" value="true" /> 

As you might already know, you will not be able to run your (now) library project -- you will need to include it into an app project.

If you're using Android Studio 1.0 and you are getting “Library projects cannot set applicationId”, make sure you do not have applicationId in your Gradle build file.

like image 149
Léo Lam Avatar answered Oct 05 '22 06:10

Léo Lam


Looking at this document http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup

I think all you have to do is add this to your build.gradle file,

Creating a Library Project

apply plugin: 'android-library' 

From the link

Creating a Library Project

A Library project is very similar to a regular Android project with a few differences.

Since building libraries is different than building applications, a different plugin is used. Internally both plugins share most of the same code and they are both provided by the same com.android.tools.build.gradle jar.

buildscript {     repositories {         mavenCentral()     }      dependencies {         classpath 'com.android.tools.build:gradle:0.5.6'     } }  apply plugin: 'android-library'  android {     compileSdkVersion 15 } 
like image 36
Ryan Heitner Avatar answered Oct 05 '22 07:10

Ryan Heitner