6

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?

TofferJ
  • 4,678
  • 1
  • 37
  • 49
Machael.HW.Wong
  • 169
  • 2
  • 12

2 Answers2

11

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

kostyabakay
  • 1,649
  • 2
  • 21
  • 32
Gopal
  • 1,734
  • 1
  • 14
  • 34
  • after changing it, i am getting the following error `No resource identifier found for attribute 'labelerClass' in package 'com.googlecode.android.widgets.MyClass'`. How to resolve this problem? – hasnain_ahmad Jun 18 '17 at 18:12
  • 4
    You forgot to mention that he should delete the "applicationId" too from build.gradle – Zapdos Aug 03 '17 at 15:44
0

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).

Mohammed Fathi
  • 1,237
  • 1
  • 14
  • 12