Libraries in Android

An android library is structurally the same as an Android App Module. It contains everything you need to build an app, including source code, resource files and an Android Manifest. On compiling, 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 foe an Android app module.

A library module is useful in the following situations:

  1. When you are building multiple apps that use some of the same components, such as activities, services or UI layout.
  2. When you are building an app that exists in multiple APK variations, such as a free and paid version, and you need the same core components in both.

In either of these cases, simply move the files you want to reuse into a library module then add the library as a dependency for each app module.

Using external libraries in Android

There are three ways to use an external library:

  1.  Add your jar files to libs folder of the project and declare it as a library to use.
  2. Create an Android module and copy your jar file to this module, and then declare your project using the newly created module.
  3. Declare and use a remote library.

ButterKnife library in Android

What is it?

ButterKnife library is a view binding library developed by Jake Wharton, that uses annotation to generate boilerplate code for us. It makes your code more compact and more clear. To avoid writing repetitive code just like “findViewById(R.id.yourview)”, ButterKnife helps you to bind fields, methods and views.

How does ButterKnife library work?

ButterKnife library uses annotation processing to generated modified Java classes based on your annotations. Annotation processing is a tool built in javac for scanning and processing annotations on compile time. You can define custom annotations and a custom processor to handle them(these annotations are scanned and processed at compile time).

The annotation processor does not change the existing input class, but it generates a new Java class, and this generated Java code is compiled again as a regular Java class.

The ButterKnife annotation processor scans all Java classes looking for the ButterKnife annotations, if a class contains these annotations, it generates a new class based on the original class view-binding schema.

How to add ButterKnife library to your project

You can find the library here: https://github.com/JakeWharton/butterknife

Simply add the latest version as compile dependency in your build.gradle file.

Comparing code with and without ButterKnife library

For a simple example we will take an onClick method for a sample Button. This is how the code looks like without the ButterKnife library:

And this is how the same code looks like when using the ButterKnife library:

We can clearly see that the code is shorter and more clear. It can save you a lot of precious time when coding. It is a highly recommended library to use.

 

Spread the love

1 Comment

  1. Howdy! I’m at work browsing your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the outstanding work!|

Leave a Reply

Your email address will not be published.