71

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.

Léo Lam
  • 3,870
  • 4
  • 34
  • 44
vijay
  • 2,034
  • 3
  • 19
  • 38

6 Answers6

100

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.

Léo Lam
  • 3,870
  • 4
  • 34
  • 44
  • Posting it as a separate answer because editing the actual answer to add a few details will probably change too much to it (and will probably be rejected, as it's too substantial)! – Léo Lam May 20 '14 at 16:29
  • 4
    One more error after this will appear as “Library projects cannot set applicationId”. To solve this remove only one line from defaultconfig section i.e. mentioning applicationid [reference](http://stackoverflow.com/a/27375066/2641380) – SHS Dec 10 '14 at 12:31
  • @SHS: I'm not sure what you mean? – Léo Lam Dec 10 '14 at 12:32
  • TO: Léo Lam In comments section of stack overflow, pressing enter posts the uncompleted comment. So my uncompleted comment was seen by you. With reference error, the applicationid tag is only available when the project is of App. In Lib project we need to delete the tag. Sorry for inconvenience. – SHS Dec 10 '14 at 12:51
  • @SHS Thanks! I've added the information in the answer. Note that with 50 rep, you can now suggest edits to posts. – Léo Lam Dec 10 '14 at 14:05
  • after the library is made, where to find the library in the explorer? – BlueLeaf Jul 12 '15 at 15:30
  • I've an app that is a library for other apps. I usually change that in Eclipse using `Project > Properties > Android`, and (un)clicking in `Is Library`. How can I do this in Studio without editing the `build.gradle` file? – Luis A. Florit Nov 08 '15 at 12:51
  • Studio uses .iml files for the project, so you would change the `LIBRARY_PROJECT` option in your .iml manually. Not sure if that would work though. – Léo Lam Nov 08 '15 at 12:54
  • Not sure if I understood. I don't want to manually edit the file each time I process it... isn't a Studio option for this, like in Eclipse? – Luis A. Florit Nov 08 '15 at 12:58
  • The above recommendations compiled with no problem. But I can't find the resulting library (.jar) file. Where should I find the library file? – Hephaestus Apr 02 '16 at 00:27
  • @LéoLam I would like to echo BlueLeaf's question, as it seems this was not addressed. Using this method, where do I find the compiled library in the file hierarchy? I don't see it anywhere - do i need to do something else to get a compiled file that can be added to other projects? – CaptainForge Jul 06 '16 at 15:30
  • I'm not sure if this has changed in 2.0. I haven't been using Studio recently. Sorry. – Léo Lam Jul 06 '16 at 17:19
5

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
}
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
3

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.

Navin Kumar
  • 3,393
  • 3
  • 21
  • 46
0

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

chanakya
  • 97
  • 8
0

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.

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
spe
  • 1
  • 1
0

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!

craned
  • 2,991
  • 2
  • 34
  • 38