Convert an Android Project into a Library - Androhub

Convert android project into library banner

Convert an Android Project into a Library

Have you ever run into a situation where you want to convert an Android project into a Library so that you can use it in your project?

Why do we need this?
We like some project which can be useful in our project but there is no library available for it. Instead of copy pasting code into our project we want to use that as a module which will make that code independent and easy to manage.

If you have any such requirements, this article is for you.

Prerequisites

  1. Create an Android Library
  2. Adding External Library
  3. Finding Right Library

VIDEO TUTORIAL

Steps to Convert

1. To convert take any Android project, it can be your old project, existing project, or any git project.

2. Navigate to the downloaded Project path and just copy the app directory.

Project path

3. Go back to your Android Studio switch the mode from Android to Project and paste the copied directory under your root directory.

Paste dialog for module

While pasting it will ask you to rename your module which will avoid overriding your main app module as right now both directory names are app only.

4. After copy-pasting the project this is how your project structure will look like.

Project structure before adding module
Project structure before adding the project

 

Project structure after adding module
Project structure after adding the project

5. Now we have to convert the project into a library. To do that open build.gradle.kts file and make the following changes:

Change the plugin id from com.android.application to com.android.library

Remove the applicationId, versionName, and versionCode as they are not applicable in the library and will give you an error if used.

6. Open AndroidManifest.xml of the copied module and remove all the attributes from application tag and also remove the Launcher Intent Filter from your activity. As there can only be one launcher activity.

Note: Always rename all the files including resources of converted library with prefix of the module name or any other identical name to avoid name conflict with other modules. Otherwise you will see wrong behavior of application where wrong icons, colors or files might be picked.

This thing also applicable when you are creating your own library.

7. Till the above step we have converted the Project into a Library. Now we have to integrate the library with the main app module.

8. Open settings.gradle.kts file and add the following code

Full code of settings.gradle.kts file:

This process will include the library in your project. The below image shows the project structure after including the module:

Project structure after including library

9. Now open build.gradle.kts file of your main app module and add the included library by adding it under dependencies:

This way you can access the code from the module.

10. By following all the above steps you can convert any Android project into a Library.

Challenge

The Android project that you will try to convert into a library can have different dependencies and gradle versions which make it incompatible with your current project (if your project is running on old or new versions).
It is always good to check the compatibility otherwise there will be efforts involved in making the versions compatible.

Happy Coding 😊

Post comment

Your email address will not be published. Required fields are marked *