Machael.HW.Wong
Machael.HW.Wong

Reputation: 169

Android convert project to library project

I am trying to convert an Android Studio project as a library.

What is the best way to do this and the steps I need to follow?

What file can I remove from that library project, so that the library look cleaner?

Upvotes: 6

Views: 7648

Answers (2)

Mohammed Fathi
Mohammed Fathi

Reputation: 1475

You need to do THREE things :

1- change the apply plugin: 'com.android.application' in the build.gradle of the module to apply plugin: 'com.android.library'

2-delete applicationId "your.application.id" in the module's build.gradle

3-remove <application and it its content from your module's AndroidManifest.xml file

4-also remove the resources in mipmap directories and the themes and colors.xml (or any other resource file that might conflict with the parent or app module) to avoid any surprises and crashes when using this library module from a module that has the same files.(I have wasted 4 hours to know the reason of app crash at startup with no helpful logs).

Upvotes: 0

Gopal
Gopal

Reputation: 1784

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. Unlike JAR files, AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.

Convert an app module to a library module

Open the build.gradle file for the existing app module. At the top, you should see the following:

apply plugin: 'com.android.application'

Change the plugin assignment as shown here:

apply plugin: 'com.android.library'

Also in this file, you have to delete this line

applicationId "your.application.id"

Click Sync Project with Gradle Files.

Check this for more details https://developer.android.com/studio/projects/android-library.html

Upvotes: 12

Related Questions