Initialize Your Own Android Library Project from Scratch

Raka Westu Mogandhi
prismapp
Published in
3 min readMar 12, 2017

In many business we saw that not only application can succeed but a tools or plugin can be too. So that’s the basic principle of SAAS or Software as a Service. SAAS was built to help other softwares.

So how about SAAS in mobile engineering? Yes we can use that principle on mobile. In Android we call that library project. Library is a module that can’t be run independently. It needs to be run from application module.

So why is library becoming more important now?

  • Can you imagine when initializing project you need to build your own HTTP call handlers, database handlers, image loaders?
  • In the middle of running project you need to add some specific functions. For example: real time notifications.
    Can you imagine that you need to write it yourself? Why not using existing library?
  • Library module can help us decoupling recurring task that we need to write every time we start a project.

Nowadays Android has many popular libraries. Be it open source, closed source or even paid one.

  • Open source → the source code was publicly available. You can even customize it for your requirements.
    For example: Retrofit, RxJava, OkHttp, Glide, etc.
  • Closed source → the source code was not publicly available. You can use the provided binary but you can’t modify it.
    For example: Android Support library, Google Play Services library, etc.
  • Paid → closed source and need to pay to use the binary.
    For example: PubNub, Stripe, etc.

To build an Android library, you can initialize your project with Android Studio and add a library module then you can start writing on it.

Initialize Library Instance Class

Have you ever used Retrofit ? It have easy to use builder pattern so people can use it easily.

Imagine people can use your library like above codes at initExampleLibrary method. Using builder pattern like above codes is recommended because it’s easy to read and write by our library user.

For the builder pattern implementation example you can look at first part on ExampleLibrary class. It has inner class named Builder that must be used to make an ExampleLibrary instance because its constructor itself is private.

Public Function or Method in Library Instance

After initializing the library, user will start using your library. To keep it simple you need to make your public function or method directly on your library instance. User who want to use that function don’t need to know what happen inside this function so we need to make it is simple.

Look at above code blocks. When using the library you can make a static getter method to get your ExampleLibrary instance. In above example I use getInstance() method name. Then user can directly use the public function provided by your library without knowing what happens inside the library.

To make that happens, you need to make your library instance class as your user gate when using your library so if there are any internal implementation changes users don’t need to change their implementation.

Obfuscate Everything

When you published your library you can use proguard to obfuscate everything that doesn’t need to be exposed to users. Any internal implementations should be obfuscated. Consider this is a must when you are publishing paid or closed source library. The advantage of this was your binary size and the methods count should be smaller so application who uses your library won’t affected much by your library.

Look at above code blocks. In your library build.gradle you need to set minifyEnabled to true to enable the proguard obfuscation. Don’t forget to set consumerProguardFiles to automatically add your proguard rules into your library user.

--

--

Raka Westu Mogandhi
prismapp

Just an Ordinary Software Engineer that always Learn