Let’s Create Our Own Android Library



If you have got ever thought of writing your own custom library for others to use or extend.
I will even be showing you ways to distribute your library through JitPack. We will be making a straightforward statistics library, that can be used to compute basic, Arithmetic Mean, Median and Mode.

To achieve this, we will follow the steps below:
  • Create a new Android project that showcases the library
  • Create a new library module inside the app module
  • Write the code for your custom library
  • Add the library as a dependency to the project
  • Publish your library to Github
  • Setup to share your library with the other using Jitpack
1. Create a new android project

Create a new Android project on Android studio. This project are going to be want to check your library.
  
2. Create a new library module

Next is to add new android library module, using to create new library module from android studio in current project.

Right-click the project new -> module then choose Library -> Give your library name on the next screen(pop-up) and click finish.





When you click on finish button then project getting start building, after that the new created module in the project. You can see in your project structure(left side).
Our library for currently can have only one category.
  • Create Arithmetic.java. this file in the java folder under the library module(which we create).
3. Write the code for custom library

// contains functions to computer Arithmetic mean, median and mode
public class Arithmetic {
     /**
     * Computes the arithmetic mean of a set of numbers
     * @param numbers
     * @return
     */
     public static arithmeticMean(Collection<? extends Number> numbers) {
          //check type of values
          Iterator<? extends Number> iterator = numbers.iterator();
          Number firstNumberInCollection = iterator.next();
          if (firstNumberInCollection instanceof Integer) {
               return computeMeanInt(numbers);
     } else if (firstNumberInCollection instanceof Long) {
              return computeMeanLong(numbers);
     }
     return computeMeanDouble(numbers);
}
...
...
/**
* computes arithmetic mean for a set of Integer values
*
* @param numbers
* @return
*/
private static Number computeMeanInt(Collection<? extends Number> numbers) {
      double result = 0;
      Iterator<? extends Number> iterator = numbers.iterator();
      while (iterator.hasNext()) {
             result += iterator.next().intValue();
      }
      return result/numbers.size();
     }
}

Check this github repo for the complete code.

4. Add the library as a dependency to the project

Go to File -> Project Structure, click on the app module and choose the Dependencies tab. Use the (+) icon at all-time low of the window to feature a replacement module dependency. Select the newly created library.

Click the ok button when done. The dependency add the app module build.gradle file.

compile project(':Arithmetic')

Now testing library:- In the onCreate() method of our MainActivity. We add the code to test this library.

ArrayList<Integer> numbers = new ArrayList<>();
Random r = new Random();
for (int i = 0; i < 15; i++) {
       numbers.add(r.nextInt(7)+1);
}
double mean = CentralTendency.arithmeticMean(numbers).doubleValue();
double median = CentralTendency.median(numbers);
ArrayList<Integer> mode = CentralTendency.mode(numbers);
Collections.sort(numbers);
String res = String.format("Numbers:\n\n%s\nMean: %.1f\nMedian: %.1f\nMode: %s\n",
                          numbers, mean, median, mode);
textView.setText(res);

5). Publish your library to Github

Now that our app is prepared, we need to upload to Github. Push all committed codes to the remote repo.

6). Setup and Share your library through Jitpack

Now we setup our Jitpack for Git repository. This is use for share our library in globe just adding there dependency in our gradle files.

Add the android-maven plugin. In the root build.grade file, add:

buildscript {
    dependencies {
         classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

add the following below line In the library module build.gradle file:

apply 'com.github.dcendents.android-maven'
Group 'com.github.YourUsername'

Create a Github release or add a TAG In the project root in github, run the command below to create v1.0 tag for newly created library:

git tag - a v1.0 -m “Artihmatic version 1.0”
git push origin v1.0

Ensure that you have Gradle Wrapper in your repository. If you don’t then create it using the command gradle wrapper and commit it. When this is done, run this command in the project root;

./gradlew install

This will install library in the local maven repository($HOME/.m2/repository).
If the build is successful and the project has also been uploaded to Github, go to jitpack url:
On the Github, select the release you would like to share by clicking the Get It button next to the list of releases of your repository.

Add a README File


Automatically, Github looks for a file names README.md in the root of your project and displays it below. The README file should contains the following:
  • A badge with the status of your library repository.
  • A line describing how your library can be added to an android project.
  • Sample code showing how to use the library.
  • Finally, the library license usage. Check out this link for sample open source licensing.

Comments

Popular posts from this blog

Android Pie Wifi RTT

Multipeer Connectivity Framework in IOS

Which Programming Language is better for iOS Mobile Application Development? Swift or Objective C