6

How to Create an Android Library

 2 years ago
source link: https://hackernoon.com/how-to-create-an-android-library
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
How to Create an Android Library

When developing an Android application, we can generate an AAR file and use it in different android projects.

Creating an Android Library will save you time compiling and running your project, and you can share existing services, images, resources, layouts with other people.

Libraries make reusing existing code very convenient.

As you build your library, remember this quote:

“If you wish to make an apple pie from scratch, you must first invent the universe.” ― Carl Sagan, Cosmos

So let’s get started.

Audio Presented by

Speed:
Read by:
Your browser does not support theaudio element.
Timofey Krestyanov

I'm 5+ years of experience in building Mobile apps developer, I have worked on Android, and iOS projects

When developing an Android application, we can generate an AAR file and use it in different android projects.

Creating an Android Library will save you time compiling and running your project, and you can share existing services, images, resources, layouts with other people.

Libraries make reusing existing code very convenient.

As you build your library, remember this quote:

If you wish to make an apple pie from scratch, you must first invent the universe.” ― Carl Sagan, Cosmos

So let’s get started.

Creating an Android Library

Starting process:

Project Setup → Open a new project → Select an Empty Project Template

This would create an empty Android project.

Click File > New > New Module.

In the Create New Module window that appears, click Android Library

-4d139vp.png

Then give your library a name and select a minimum SDK version for the code in the library, then click Finish.

The library module appears in the Project panel on the left.

At the top of the file, you should include the following line if it is not already there.

apply plugin: 'com.android.library'

~/woah/build.gradle

plugins {
    id 'org.jetbrains.kotlin.android'
    id 'com.android.library'
}

android {
    compileSdk 31

    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
}

Try to simply Build > Rebuild project or execute in terminal:

./gradlew assembleRelease 

or choose assembleRelease from Android Studio's Gradle menu.

After a successful build, First switch to the project view in the left panel and navigate as sown below.

AAR file should appear in ~/woah/build/outputs/aar/

-sd239fq.png

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.

AAR files can contain C/C++ libraries for use by the app module's C/C++ code.

Implement Your logic in your Library Module

Let’s write a simple library that is shown when your app gets crashed.

interface CrashListener {
    fun onUncaughtException(thread: Thread, throwable: Throwable)
}

object Woah {
    @JvmStatic
    fun init(crashListener: CrashListener) {
        Thread.setDefaultUncaughtExceptionHandler { p0, p1 ->
             crashListener.onUncaughtException(p0, p1)
        }
    }
}

Adding @Keep on a class protects the class from removing by proguard during optimization.

Don’t use default resource names such as colorAccent or colorPrimary in resources.

Default host app resources will always be prioritized.

How to Integrate AAR Library

Just move the AAR file to the libs directory.

-pma3ihb.png

Then add in build.gradle file:

dependencies {
    implementation project(':woah')
    implementation fileTree(dir: "libs", include: ["*.aar"])
    ...
}

Conclusion

In this guide, we have covered all the steps you need to take to create an Android Library.

The goal of creating an Android Library should be to develop more user-friendly interfaces that can be reused.

Resources:

Sample (full code)

https://github.com/tkrest/woah

Android-library

https://developer.android.com/studio/projects/android-library

Dependencies

https://developer.android.com/studio/build/dependencies


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK