How to create a library for others to use Android

Hello everyone, Have you ever asked the android library in dependencies that you used someone else, they create them how? Today, I will guide you how to create and publish android library for others to use. And of course, you also have a minimum of a library for their many shared projects, support to rewrite or copy the resulting heterogeneous standard library project.

Create applications and libraries

First you create an Android Project as usual with Android Studio. Next to select Menu File -> New -> New Module

Here you select Android Library.

Next you enter the information of the library on (it's almost like 1 this project). Here I demo on a library displayed toast. The library you can create your own custom purposes

You click finish and wait a bit for the project load our new module. Once loaded you will see in the view Android, we have 1 peer app folder folder is toastlib. It is accompanied by library icon icon (library) and structured like our app folder.

Now you open the file build.gradle (app) module of the app, add the following line to our project load the library on. and remember to click Sync Now to sync again.

implementation project(':libstoast ')

Start writing library

in libstoas, we create 1 java file is displayed MToast to carry our a private Toast.

package com.nguyenvanquan7826.libstoast;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.Toast;

public class MToast {
    public static void show(Context context, String message) {
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        View view = toast.getView();
        view.setBackgroundColor(Color.BLUE);
        toast.show();
    }
}

So we're done with our own library. Also in the library, you absolutely can create Fragment, Activity,… attached interface to cater for various projects.

Now we're back to the original module app is our project to use the library just created.

In our MainActivity file instead of the default event by displaying MToast FAB

package com.nguyenvanquan7826.demolibs;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.nguyenvanquan7826.libstoast.MToast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MToast.show(MainActivity.this, "This is MToast");
            }
        });
    }
}

Result:

Publish content library for everyone

To perform take up global library for all users, firstly you need to install git on machine. Then create an account on github. These things you can find instructions on google a lot.

Next, you create 1 repository on pubic gihub Mode

Follow, github will guide you put the code up with some statements.

You open up a terminal of android studio (in the status bar below) and in turn run the commands like github guide (Your link will be different repository of yourselves).

git init

git remote add origin https://github.com/nguyenvanquan7826/androiddemolibs.git

git add .

git commit -m “init”

git push origin master

Orders last push, you may have to enter your email and password to your github account nhé. Once finished, push on github you will have code like this:

You click on the menu to create new releases and releases to “publish” for library.

Here considered as finished. Now you go https://jitpack.io/ , sign in with github repository and find their paths are to be found.

Down below you will see this is and people can use your library by 2 step guidance here: 1. more maven { url ‘https://jitpack.io’ } on the project and build.gradle 2 is putting his library in, rather tag with the version you created (1.0.0)

The general syntax is:

com.github.user:repository:version

Demo source code you can look, download here

Create multiple libraries in the same project

You may need to create multiple libraries, but do not want to use multiple projects to create each library, then you just write on a project such hế, create library modules later when used simply identifies each library only. Examples of projects AndroidLibs I have 2 library is appbase and textviewtwoline, to use each library I would write as follows:

implementation “com.github.nguyenvanquan7826.AndroidLibs:appbase:1.0.5”

implementation “com.github.nguyenvanquan7826.AndroidLibs:textviewtwoline:1.0.5”

The syntax would be:

com.github.user.repository:libsname:version

If you want to use all the library without writing the details out, you just write as with 1 library syntax

com.github.user:repository:version