Search code examples
javaandroidandroid-studiojarandroid-gradle-plugin

in Android Studio, how can I change an app project to a library?


I'm sure this is simple, but since I just started using Android Studio today, I can't find it. I have also googled for the past hour and found no posts that specify:

how to change an existing application project to a library.

Everything I found was about how to create a new library project. Not how to convert one.

A sub-question, is how can I see if a project is configured as an application or a library? I would hope that the answer to both of these questions is the same.


Solution

  • Open your build.gradle of your app (inside your app directory) and modify :

    apply plugin: 'com.android.application'
    

    with

    apply plugin: 'com.android.library'
    

    If you have an applicationId in your build.gradle remove it :

    defaultConfig {
         applicationId "com.your.application.id"
    }
    

    then clean your project and rebuild or just sync your gradle from Android Studio

    If you have added some extra gradle properties like applicationVariants.all you must replace with libraryVariants.all and vice-versa if you convert a library to application

    If you want to add a new step to your reconversion you can change the module name "app" created by default by Android-Studio with a name more adapted to a library module. You can rename the directory app with <your_module_name>. open settings.gradle file (at the root of your project) and replace app with <your_module_name>. Then Go to Menu Build > Make module <your_module_name> and there you are, your module is renamed.