Convert existing project to library project in Android Studio

AndroidAndroid StudioAndroid Library

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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
}

Solution 3 - Android

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.

Solution 4 - Android

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

Solution 5 - Android

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.

Solution 6 - Android

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvijayView Question on Stackoverflow
Solution 1 - AndroidLéo LamView Answer on Stackoverflow
Solution 2 - AndroidRyan HeitnerView Answer on Stackoverflow
Solution 3 - AndroidNavin KumarView Answer on Stackoverflow
Solution 4 - AndroidchanakyaView Answer on Stackoverflow
Solution 5 - AndroidspeView Answer on Stackoverflow
Solution 6 - AndroidcranedView Answer on Stackoverflow