Categories
Studio

How to Create and Publish a Library for Android using Jitpack(Public)

If you use a module (can be a utility class) in more than two projects, you should consider creating a library and use it for your future projects and also share it to the world to make Android community a better place.

Today, I will explain how to create a public library for Android and publish it using Jitpack so that anyone in the world can benefit from your work. And we will learn this using a simple example – SharedPreferences Library.

How to Create a Library

Create a new Project in Android Studio. We will name it SharedPrefs for our example.

Now you should see an empty project.

Go to File → New → New Module.
Android Studio Creating a new Android Library ModuleSelect Android Library, Name it (We name it SharedPref and this will be your library name).

Now write code to your newly created library. You should see the library module right below the ‘app’.
Android Studio Library Module in Projects Tab

Writing SharedPrefs library – Example

Go to the newly created module (below the app – in this example it will be sharedpref). Now we create a new class called SharedPrefs inside sharedpref > java > anil.sardiwal.sharedpref.

Basically, this will make writing shared preferences easy for me. By using this library in my projects, I will just write SharedPrefs.saveInt(“intKey”, keyValue); instead of writing multiple lines of code for saving preference values. It will save and read not just int but string, boolean and long too.

Here’s the complete code for SharedPrefs.java

package anil.sardiwal.sharedpref;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * A utility tool to save and read from shared preferences.
 *
 * @author Anil Sardiwal
 * @since 09/10/2018
 */
public class SharedPrefs
{
    
    @SuppressLint("StaticFieldLeak")
    private static Context mContext;
    private SharedPrefs() {
    }

    public static SharedPrefs with(Context context)
    {
        mContext = context;
        return new SharedPrefs();
    }

    // Shared preferences
    private static String PREFS_NAME;
    public static void init(String globalPreferenceName)
    {
        PREFS_NAME = globalPreferenceName;
    }
    /**
     * For saving Strings.
     * @param keyName String for saving preference.
     * @param stringValue Default value
     */
    public void saveString( String keyName, String stringValue)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(keyName, stringValue);
        editor.apply();
    }

    public String getString( String keyName, String defaultValue)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(keyName, defaultValue);
    }

    /**
     * For saving Ints
     * @param keyName String for saving preference.
     * @param intValue Default value.
     */
    public void saveInt( String keyName, int intValue)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(keyName, intValue);
        editor.apply();
    }

    public int getInt( String keyName, int defaultValue)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(keyName, defaultValue);
    }

    /**
     * For saving Booleans
     * @param keyName String for saving preference.
     * @param booleanValue Default value.
     */
    public void saveBoolean( String keyName, Boolean booleanValue)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(keyName, booleanValue);
        editor.apply();
    }

    public boolean getBoolean( String keyName, Boolean defaultValue)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(keyName, defaultValue);
    }

    /**
     * For saving long
     * @param keyName String for saving preference.
     * @param longValue Default value.
     */
    public void saveLong( String keyName, long longValue)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(keyName, longValue);
        editor.apply();
    }

    public long getLong( String keyName, long defaultValue)
    {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getLong(keyName, defaultValue);
    }
}

How to Test your Library

Implement your library in the same project to test the library before publishing it. Go to Gradle Scripts > Open build.gradle (module: app) for your app module.

In dependencies, We will implement the SharedPrefs library like this:

implementation project(':sharedpref');

Now go to your app’s MainActivity and start testing by calling your library. In our example, we will initialize sharedPreferences by setting a global preference name to it.

SharedPrefs.init(MainActivity.class.getSimpleName());

Now test each and every method of your library module by using them in your app (MainActivity). Here’s how I did it.

// save string to shared prefs
    SharedPrefs.with(getApplicationContext())
        .saveString("editText1", stringEdit.getText().toString());

// save int to shared prefs
    SharedPrefs.with(getApplicationContext())
        .saveInt("editText2", Integer.parseInt(intEdit.getText().toString()));

// save boolean
    SharedPrefs.with(getApplicationContext()).saveBoolean("boolean", isChecked);

// Get values from Shared Preferences
    String string1 = SharedPrefs.with(this).getString("editText1", "Hello");
    stringEdit.setText(string1);

    int int1 = SharedPrefs.with(this).getInt("editText2", 101);
    intEdit.setText(String.valueOf(int1));

    boolean isChecked = SharedPrefs.with(this).getBoolean("boolean", false);
    booleanSwitch.setChecked(isChecked);

Now we will proceed to publish this library on Github + Jitpack to use it in our other projects.

How to Push your Library to Github

I will demonstrate this by pushing this library (complete project) on Github.

  1. First, create a repository on Github. Name it.
  2. Now go to Android Studio. VCS > Enable version control integration. Select Git. Press OK.
    Android Studio Enable Version Control Integration
  3. Now commit the project and select all unversioned files. Write the commit message and hover over to “Commit” button and select “Commit and Push”. Refer the screenshot below.
    Android Studio VCS Git Commit and Commit Message
  4. Go to Github repository page. It should be empty over there. Copy the HTTPS link and go back to Android Studio.Github new Repository HTTPS link
  5. In Push Commits window, It asks you to define remote. Click on Define remote. Paste the link in URL box and press OK.
    Android Studio Push Commit Define Remote
  6. It should now show the remote branch as master → origin → master. Now click on Push to push to Github.
  7. Go to Github repository page and refresh the page to check if your project is successfully pushed or not.
  8. On a successful push, it should look like this.
    Github example of new repository pushed from Android Studio

Create a Release on Github

Assuming that your library module is bug-free, we will now proceed to create a release.

  1. Go to releases > Create a new Release.
  2. Write Tag Version. I choose it to be 1.0
  3. All other fields are optional, you can choose to write more info.
  4. Click on Publish. Now our version 1.0 of SharedPrefs is ready.

How to Publish your Library using Jitpack

  1. Go to Jitpack.io
  2. Paste your Jitpack repository URL and click on Look Up. Note: You don’t need an account for Jitpack.
  3. You should see your first release tag name there. Click on Get It.
    Jitpack Android Library Look Up
  4. That’s it. You have successfully published a library.
  5. Follow the process of implementing your library from that page.
  6. Also, write a readme for your library for others to understand.

SharedPrefs Github linkhttps://github.com/theonlyanil/SharedPrefs

SharedPrefs Jitpack linkhttps://jitpack.io/#theonlyanil/SharedPrefs/1.0

Conclusion

I found this very handy to convert your utility classes into library modules so that you can easily implement in your future projects without copying the entire classes. So far, in five days, I have published three libraries on Github using Jitpack. Though I am yet to write the readme of two of them. If you are reading this post on a future date, you might find Readme of these projects updated.

My Other Libraries:

  1. ColorAnimation – soothing color animations on a viewGroup.
  2. FB Rebound Recycler – Spring mechanics to your Recyclerview.

Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *