Contact Us

Contact Us



Pleas confirm by checkbox


App DevelopmentTechnical

Develop an Android Library - Part 1

Author_img
By nandand February 08, 2017

What is a Library?

What does a “library” mean in programming world?
“ A library is a precompiled code and resources that can be used in other projects easily “

In Android, mainly two types of libraries are used: JAR and AAR.

What is JAR:
Full form of JAR is Java Archived. A JAR file is a Zipped formatted file with .jar extension, containing Java Class files, metadata and other resources like text, images etc. JAR files may also contain one Manifest.MF file. This file contains information like which Java class is the entry point, classpaths etc.

What is AAR:
Android Archive or AAR files allows you to contain not only the source class files but also other resources, permissions, assets etc. we use in Android Projects. AAR file contains the following mandatory entries:

  •  /AndroidManifest.xml
  •  /classes.jar
  •  /res/
  •  /R.txt

It may also include one or more of the following entries:

  • /assets/
  • /libs/lib_name.jar
  • /jni/abi_name/bin_name.so
  • /proguard.txt
  • /lint.jar

Build a library:

Create a JAR in Android Studio:

Select File -> New -> New Module and select “Java Library” from the selection :

screen-shot-2017-02-07-at-6-13-33-pm

Click Next -> Finish 
It will create a new java library project.
But you cannot add any android Framework dependency in this library.

How to create a library for android then? Simply select “Android Library” instead of “Java Library” in the last step above . done !!

screen-shot-2017-02-07-at-6-18-06-pm

But how to get the .jar file for both of these libraries?

For “Java Library”, use ./gradlew build to create .jar . Output file will be inside /build/libs/ folder.

For “Android Library”, we will have to use one custom Gradle task:
Copy-paste the following code snippet inside build.gradle file:

task deleteJar(type : Delete){
    delete 'libs/myLib.jar'
}

task createJar(type : Copy){
    from ('build/intermediates/bundles/release/')
    into ('libs')
    include ('classes.jar')
    rename ('classes.jar','myLib.jar')
}

createJar.dependsOn(deleteJar,build)

Now use the command ./gradlew createJar to build the jar. The output file will be inside /yourLibrary/libs/ folder.

screen-shot-2017-02-08-at-11-45-13-am

Create an AAR in Android Studio:
Generating an AAR is easier than jar. Just use “./gradlew assembleRelease” and it will generate release AAR inside /yourLib/build/outputs/aar/ folder.
screen-shot-2017-02-08-at-11-49-17-am

Use AAR or JAR file in Android Studio:

JAR :
add the following line inside dependencies {} tab of build.gradle
compile files(‘libs/myJarLib.jar’)
myJarLib.jar should be inside ‘libs’ folder.

screen-shot-2017-02-08-at-3-20-25-pm

 

AAR :
Add the following inside dependencies {} tab :
compile(name:”myAarLib” ,ext : ‘aar’)

and ( not inside dependencies{})

repositories{
    flatDir{
        dirs 'libs'
    }
}

Rebuild the project and it should work.

– Nandan

Related posts
Angular — How to render HTML containing Angular Components dynamically at run-time
App Development

Angular — How to render HTML containing Angular Components dynamically at run-time

By nandand May 05, 2021
Part-3: Building a bidirectional-streaming gRPC service using Golang
App Development

Part-3: Building a bidirectional-streaming gRPC service using Golang

By nandand April 22, 2021
A Step-by-Step Guide to Easy Android in-App Review Setup.
App Development

A Step-by-Step Guide to Easy Android in-App Review Setup.

By nandand April 16, 2021
Part -2: Building a unidirectional-streaming gRPC service using Golang
App Development

Part -2: Building a unidirectional-streaming gRPC service using Golang

By nandand April 15, 2021
How to Integrate Firebase Authentication for Google Sign-in Functionality?
App Development

How to Integrate Firebase Authentication for Google Sign-in Functionality?

By nandand April 09, 2021
Part-1: A Quick Overview of gRPC in Golang
App Development

Part-1: A Quick Overview of gRPC in Golang

By nandand April 07, 2021
Publish Your Android Library on JitPack for Better Reachability
App Development

Publish Your Android Library on JitPack for Better Reachability

By nandand April 02, 2021
How to Use Firebase Remote Config Efficiently?
App Development

How to Use Firebase Remote Config Efficiently?

By nandand March 26, 2021
How to simplify Android app distribution with Fastlane and improve workflow?
App Development

How to simplify Android app distribution with Fastlane and improve workflow?

By nandand March 18, 2021
Google Play Instant Run Integration
App Development

Google Play Instant Run Integration

By nandand September 09, 2019

Stay updated

Get the latest creative news from Fubiz about art, design and pop-culture.