KMM: Use a Shared Binary Library in an Android Project Manually

March 6, 2023
 
Manually add an Android Archive (AAR) File Dependency to Your Local Native Android Application

In my previous article, we used Kotlin Multiplatform Mobile (KMM) to build shared binary libraries: AAR and XCFramework for Android and iOS respectively. In this article, I will show you how to manually add it to your local native Android application.

Download the Shared Library

You can build the Android Archive (AAR) files yourself by following the previous article, or you can download from here:

https://bitbucket.org/CodeGorilla/cg-lib-kmm-shared/src/master/source/codeGorillaLibrary/build/outputs/aar/

Create a New Android Project

Open Android Studio and create a new project using an Empty Activity.


Now run the Hello World template code to ensure that your project is initialized properly.

Manually Add AAR Dependency

For this example, we will manually copy the AAR files into the libs folder of your project. The libs folder is created automatically with the Android Studio template.


Add libs Folder to Repositories

Add the libs folder to your repositories as a flat directory repository by editing the settings.gradle file to add the libs folder using flatDir as follows:

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
flatDir {
dirs 'libs'
}
}
}

Add AAR File Dependency using Gradle

If you're using a shared library from KMM you may have two AAR files for debug and release. To use each one appropriately, you can add your dependencies to your build.gradle (:app) as follows:
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

// The following lines will add your AAR files for debug and release
debugImplementation files('libs/codeGorillaLibrary-debug.aar')
releaseImplementation files('libs/codeGorillaLibrary-release.aar')
}

Note: If you only have one AAR file for both debug and release you can include any AAR files within the libs folder using a single implementation statement, instead of specifying for debugImplementation and releaseImplementation as follows:

implementation fileTree(dir: "libs", include: ["*.aar"])


Using the Dependency in the Code

At this point, you're all set to begin using your dependency. Make sure you perform a Gradle sync prior to usage. Please make sure to review the status of your Gradle sync to ensure that it has found your dependency properly.

To stay focused on our objective, we will be making minimal updates to the template code to simply confirm that we have access to the AAR shared library that was created from KMM.

Edit the MainActivity.kt file to import our AAR file and use it as follows:

package com.code_gorilla.kmmapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.code_gorilla.kmmlibrary.TemperatureConverter

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val celsius = TemperatureConverter().fahrenheitToCelsius(fahrenheit = 0.00)
val fahrenheit = TemperatureConverter().celsiusToFahrenheit(celsius = 0.00)
Log.i("KMM", "Celsius: " + celsius)
Log.i("KMM", "Fahrenheit: " + fahrenheit)
setContentView(R.layout.activity_main)
}
}

Run your code and you should now see the Celsius and Fahrenheit results in LogCat. Because we are using the KMM tag you can filter the results to find your results quickly.


Download the Code

You can download the code for this article here:

git clone https://CodeGorilla@bitbucket.org/CodeGorilla/cg-app-using-kmm-lib-android.git

References



 
Return to articles