plevintampabay
plevintampabay

Reputation: 570

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.

Upvotes: 9

Views: 6195

Answers (2)

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: 0

Bertrand Martel
Bertrand Martel

Reputation: 45513

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.

Upvotes: 15

Related Questions