vijay
vijay

Reputation: 2034

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.

Upvotes: 71

Views: 53534

Answers (6)

Navin Kumar
Navin Kumar

Reputation: 4027

Changing to Library

  1. Goto android project, where you need to change as Library module.

  2. In build.gradle(:app) file,

  • change this line to

    plugins {
      id 'com.android.application'
      }
    

    to

    plugins {
      id 'com.android.library'
      }
    
  1. Delete the line for the applicationId in same build.gradle(:app) file

    defaultConfig {
         applicationId "com.project.example" //remove this line
         minSdk 21
         targetSdk 31
         versionCode 1
         versionName "1.0"
    
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
     }
    
  2. Now Sync Project with Gradle Files.

Upvotes: 4

Léo Lam
Léo Lam

Reputation: 4139

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.

Upvotes: 99

spe
spe

Reputation: 1

If you make it with command line, like chanakya propose, you have to update it with:

android update lib-project \
--target <target_ID> \
--path path/to/your/project

see: http://developer.android.com/tools/projects/projects-cmdline.html#ReferencingLibraryProject

That work for eclipse, but not for android-studio since that update the build.xml.

Upvotes: 0

Ryan Heitner
Ryan Heitner

Reputation: 13652

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
}

Upvotes: 5

craned
craned

Reputation: 3061

This is a late response, but I've been trying to do the same thing. None of the above seem to have done the job for me, but I found this that I think works:

Right click on the project name -> Mark Directory As (at bottom) -> Sources Root

I don't know the difference between Resources Root and Sources Root, and a bit of googleing to turn up the answer, but hopefully that's right. I just know a Library isn't supposed to build an apk, and after setting this option, it's not able to so I'm assuming it works.

If anybody else knows more than me, please say so!

Upvotes: 0

chanakya
chanakya

Reputation: 97

open project in file explorer,open project.properties and try changing android.library=true in project.properties

Upvotes: 0

Related Questions