Android libraries play a vital role in Android application development. We use them in our day-to-day development. Every Android project consists lot of libraries which are nothing but our dependencies. Have you ever thought about how to create an Android library?
Today we will learn to Create an Android library.
Android libraries are just like our normal Android app module which includes everything needed to build an app like source code, resource files, an Android manifest, and build.gradle file.
Benefits of Android Library:
1. Click on File => New => New Module.
2. On the next window select Android Library as a template and enter your module name as shown in the image and click on Finish.
3. When you click on Finish the gradle build will start and add the newly created module to your project.
The below image shows the project structure before creating the library module and you can see there is only an app module.
The below image shows the structure after creating the library module and you can see that the new module is visible ie. networkmodule.
4. The Below image shows the structure of the library module and it is similar to a normal Android app module.
Now let’s see what happened under the hood when we created the library module.
1. Firstly the following plugin has been added to your root level build.gradle.kts.
1 |
id("com.android.library") version "8.2.1" apply false |
This is how your gradle file will look after adding the plugin:
1 2 3 4 5 6 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id("com.android.application") version "8.2.1" apply false id("org.jetbrains.kotlin.android") version "1.9.0" apply false id("com.android.library") version "8.2.1" apply false } |
2. Now navigate to settings.gradle.kts there at the bottom you will see the following code has been added. This will include the module in the project.
1 |
include(":networkmodule") |
Full view of settings.gradle.kts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "LibraryModuleExample" include(":app") include(":networkmodule") |
3. If you open build.gradle.kts file of networkmodule, you will see a slight difference with the main app module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
plugins { id("com.android.library") id("org.jetbrains.kotlin.android") } android { namespace = "com.androhub.networkmodule" compileSdk = 34 defaultConfig { minSdk = 24 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles("consumer-rules.pro") } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } } dependencies { implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.9.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") } |
Apart from these two changes rest are all the same.
Let’s create a sample project that has Retrofit to handle API calls and we will be printing the base URL in Log. We will be moving Retrofit code to the library module to make it independent and can be used in other projects.
1. Add networkmodule as a dependency to the main app module. To do that just copy and paste the below code and paste it under the dependencies section inside your app-level build.gradle.kts file.
1 |
implementation (project(":networkmodule")) |
2. Now sync the project and check if the sync is a success. The module will only be a success if we have included the module in settings.gradle.kts as we did earlier. By doing this we can access the classes from the networkmodule.
3. Move code from the main app module to networkmodule by dragging or using copy-paste. In our case, we will be moving RetrofitClient.kt class and cut-paste the Retrofit dependencies in networkmodule gradle file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
plugins { id("com.android.library") id("org.jetbrains.kotlin.android") } android { namespace = "com.androhub.networkmodule" compileSdk = 34 defaultConfig { minSdk = 24 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles("consumer-rules.pro") } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } } dependencies { implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.9.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") // retrofit dependencies implementation("com.squareup.retrofit2:retrofit:2.9.0") implementation ("com.squareup.retrofit2:converter-gson:2.7.2") implementation ("com.squareup.okhttp3:okhttp:3.10.0") } |
4. You can create new files under networkmodule also and can access them in your main app module.
In this way, we can create our own library module and make the project modularized.
In the next article, we will learn how to Publish this library and fetch the same library from a remote repository instead of a local one.
Happy Coding.
Subscribe to us and get the latest news.