How to create custom java library and call its function inside Android App

Viewed 1419

I am new to java and android development and I want to create my own java library (JAR files) and I want to call this jar lib function inside android app code base.

Although there are numerous online tutorials available which demonstrate how to do this I couldnt find one for a newbee person like me.

The resources available on the internet, assume that a person should be hands on java and android platform. This is not the case with me.

I have just started learning it and I want to create a java library and call its function into an Android App.

So to achieve this, below are the steps I have performed.

Step 1 : Created a sample program "Addition of 2 numbers" so this is what I have written.

 package MyPackage;
 import java.io.*;
 import java.net.*;

 public class MyAddition {      
    public static int addition(int a, int b){
       return (a+b);
    }
 }

Saved it as MyAddition.java .Compiled and created its class file MyAddition.class inside folder [folder_path]..../mylib/MyPackage/

Steps 2 : Created its JAR files using following command

   jar -cf mylib.jar [folder_path]../mylib

Steps 3 :

Now I have installed Android Studio in order to create my android app code base where I want to call this JAR lib function.

In Android Studio, I have created a new project using its predefined "bottom Navigation activity" as "My Application" so the whole project is set up by the Android Studio. Here is a screenshot for the whole setup.

enter image description here

enter image description here

enter image description here

Step 4 : : I have my complete application set up which looks like this.

enter image description here

Step 5 : : Now I want to integrate my JAR file which I've created earlier into this app. So to achieve this I've added the jar lib "mylib.jar" inside apps/libs folder like this.

enter image description here

enter image description here

Step 6 : : After doing all this, I am adding following line into the build.gradle file compile files 'libs/mylib.jar'

and then sync.

After Adding the Jar, I am calling its function inside Android App Function but in this case its showing me error "can not resolve the MyPackage", where as it shows me this inside the application scope

enter image description here

enter image description here

Now I could not proceed further in order to include and call jar files function.

Any help is appreciated .

3 Answers
Related