Initialize Your Own Android Library Project from Scratch

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.

public class ExampleLibrary {
    
    private ExampleLibrary(String title, String message) {
        // Initialize your Library here
    }
    
    class Builder {
        private String title;
        private String message;
        
        public Builder addSomeTitle(String title) {
            this.title = title;
            return this;
        }
        
        public Builder addSomeMessage(String message) {
            this.message = message;
            return this;
        }
        
        public ExampleLibrary build() {
            return ExampleLibrary(title, message);
        }
    }
}
public void initExampleLibrary() {
    ExampleLibrary instance = new ExampleLibrary.Builder()
      .addSomeTitle("title")
      .addSomeMessage("message")
      .build();
  }

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.

public void useFunction1ExampleLibrary() {
	ExampleLibrary.getInstance().showExampleLibraryScreen(
		YourActivity.this,
		otherParams
	);
}

public void useFunction2ExampleLibrary() {
	ExampleLibrary.getInstance().exitExampleLibraryScreen(
		YourActivity.this,
		otherParams1,
		otherParams2
	);
}