logo

How to publish an Android Library to Maven Central using Kotlin DSL

A day comes in every Android dev's life where they have to fight one of the toughest battle in their career "Publishing a library". Don't worry soldier I got your back, today I will teach you one of the simplest way to defeat our common enemy, just follow my steps.

1. Get you code ready

To publish a library first you will likely want to have it as a seprate project or seprate module in exisiting project. Check you min-sdk level, kotlin version etc. so that it can be compatible with as many projects as possible.

2. Register for SonarType

First of all you need to register for Sonatype Jira.

git revert <hash>

2. git reset

If there are no commit on top of the commit which you wanna remove(or you wanna remove all commit upto that point) and you haven’t pushed the code yet then git reset can save your day.

You can think of it as a “rollback” — it points your local environment back to a previous commit

git reset HEAD~N

Here N is the number of commits you wanna remove.

There are 3 options in git reset

  1. git reset — — hard

  2. git reset — — mixed

  3. git reset — — soft

Hard option will completely remove the commit (dangerous), Mixed is default option and will move the content of commit to unstaged area, **Soft **will move the content of commit to staged area.

If this code is already pushed you need to use *git push -f *to force push it to the upstream.

3. git rebase

It is one of the most helpful commands out there, it allows to modify older commits, you can also use git rebase to combine commits into a new commit. git rebase allows you to rewrite history. Since you will be modifying the history it’s important that you’ve not pushed upstream or no one is working on that branch.

git rebase -i HEAD~N

Where N is the commit-number of commit you wanna remove.The -i flag shows that its an interactive rebase

You will se something like this

    pick s3f03mw commit 2
    pick 734x3fe i want to remove this commit
    pick 45ff26c commit 4

    # Rebase e851499..45ff26c onto e851499 (1 command)
    #
    # Commands:
    # p, pick <commit> = use commit
    # r, reword <commit> = use commit, but edit the commit message
    # e, edit <commit> = use commit, but stop for amending
    # s, squash <commit> = use commit, but meld into previous commit
    # f, fixup <commit> = like “squash”, but discard this commit’s log message
    # x, exec <command> = run command (the rest of the line) using shell
    # b, break = stop here (continue rebase later with ‘git rebase — continue’)
    # d, drop <commit> = remove commit
    # l, label <label> = label current HEAD with a name
    # t, reset <label> = reset HEAD to a label
    # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
    # . create a merge commit using the original merge commit’s
    # . message (or the oneline, if no original merge commit was
    # . specified). Use -c <commit> to reword the commit message.
    #
    # These lines can be re-ordered; they are executed from top to bottom.
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    #
    # Note that empty commits are commented out

We can replace the word pick by any of the given commands, but we are going to learn about how to remove or drop commit.

We will simply replace pick with d it will look something like this

d 734x3fe i want to remove this commit

Now save and exit the file

Psst.. press esc then type :wq to exit. There can be conflicts, solve them then continue rebase if there are commits remaining.

git rebase --continue

Congratulations 🥳, you’ve successfully removed the commit.

4. git cherry-pick

When all hell breaks loose and none of the options works then its time to use git cherry-pick

Using this command we can pick the commits we want and add them to a new branch and continue the work form that new branch.

git checkout -b <new branch name>

git cherry-pick <hash1-older>^..<hash2-newer>

And if you don’t want an inclusive cherry picking do

git cherry-pick <hash1-older>..<hash2-newer>

Also cherry picking a single commit can be done as

git cherry-pick <hash>

Voila! you’ve successfully removed the commit and the git history shows that nothing ever happened.

All done you’ve mastered the art of covering up your mistakes in git, Stay cool 😎