My Hexaville
Spead the Word
Start writing libraries

Start Writing Libraries!

How’s it going y’all? In my previous post I talked about great advantages that your own library gives you on BinTray. Once you realize that you copy paste your previous code time and time again or even worse – just write the same functions all over again, you should consider using a library.

Why Libraries

From the first day you stated coding you used libraries. Somebody’s else libraries. And you got used to it probably, you type a Google search query with something like ‘github image loading library…’ or search in GitHub search. That’s great! You get results faster by using somebody’s code. Imagine how much time would it take you to write even simple app if you had to stick with Android SDK only?

But what if you can’t find a library that meets your needs? You create many projects and feel like doing the same thing all over and over again. And that stuff is not actually what most libraries are about? Like setting up layouts,  adding basic Android Views etc… Then you should check my post about Android Templates. Or you create your own library. And the coolest thing is to upload it to BinTray , because you can add it with just one line of code. And all your previous work won’t be lost in between switching projects. Once I feel like I write the same code for more that five times I realize it’s a huge waste of time.

Creating an Android Library

Alright, now let’s get to business. We’re going to create an Android Library containing just one class – Data Binding Adapter. This class will have bunch of methods which will going to serve as a custom previous View attribute setters from my post. You can create any attribute with any name you wish and tell what your views should do with it. It’s like extending a Views and adding your attribute handling, but with way, way less overhead.

Let’s create our own library! Click File -> New Project, give it some name and boom! One the next page there’s no Android Library. What? But when you choose File -> New Module there’s an Android Library as a choice. Did you know that Android Libraries have exactly the same structure as Android App Modules? Build.gradle files are exactly the same except for one line. In Android App it’s

and in Android Library it’s

that’s all the difference in code. And don’t forget to remove this line from build.gradle

So you can create New Project with Android module and choose Add No Activity. And then just replace the build.gradle lines described above. Here you go, done. Converted to a library.

If you’re still confused why there’s two gradle files in every of you projects or just don’t feel confident with Gradle I suggest you take this course. Seriously, it free! Take it.

Creating Binding Adapters

I will add those 4 methods which were described in a previous post

they handle custom View attributes declared in the @BindingAdapter annotation.

Build an AAR

Now when we have all methods that we need let’s build it. Add those lines to your module’s builld.gradle

the link leads to Gradle tasks that build an AAR, and it uses those ext variables to build it. I got it from this awesome tutorial. Now open terminal in your project root and run this command on Linux/Mac

on Windows

and your .zip file will be in the build directory.

Upload to Bintray

Create a new account at Bintray if you don’t have and

Android Libraries

When it’s created click Add New Package and provide all the necessary fields. After it’s created go back to this screen where you’ll see New Version button

Android Libraries

Create new version and go back to its root where you’ll see an Upload Files button


Go there and click on Click to Add Files. And after you uploaded a file don’t forget to choose Explode this archive checkbox.

Now your library is up there, but nobody will be able to use it. Go to your package page and click Add to jCenter

Android Libraries

When they process your request and you’ll get a confirmation email after a while. You will find your  Gradle compile line in you package page. This site is a bit overwhelming when you first use it, you need to move between about three pages.


So now we have our own library on jCenter. It has a few simple Binding Adapters for ImageView attributes. Let’s see what we can do with it now.

Let’s create a new project with any template and add this line to app’s build.grale

now add this  attribute to any ImageView.

Of course you’ll have to add a url varialbe in the layout and set it in Javat. Little overhead with Data Binding library in here. Data Binding adds overhead for testing projects, but for real projects you save a lot of time using it especially having a library like that.

This Data Binding Aux library has a dependency on Glide, I chose it because it’s more memory efficient than Picasso, if you want to use your own version of Glide just replace compile line with this

and use any version of Glide that you want.


Thanks for reading, hope you find it useful. Feel free to ask any questions in comments, share this if you like this and don’t forget to subscribe to get new weekly post!


About the Author Ihor Klimov

Formerly an Android developer, lately picked up some Flutter. This blog is everything that I find exciting about Android and Flutter development. Stay tuned and hope to see you again!