articles

Home / DeveloperSection / Articles / Creating libraries for Android applications

Creating libraries for Android applications

Manoj Pandey2909 12-Mar-2015

Android library projects provide reusable code and resources that can save you time and effort when developing apps — you don’t have to keep “reinventing the wheel.”

Android project can use code contained in JAR files (Java libraries). It is also possible to create libraries modules which can be used as dependencies in Android projects. These modules allow you to store source code and Android resources which can be shared between several other Android projects.

Here I am creating sample of libraries with access it in another application.

1.     Create an android project for library 
Creating libraries for Android applications
2.     Click Next and Mark this project as library

  Creating libraries for Android applications

3.     Add class in Library

public class MyDemo {
     public String getMyResult() {
           return "Android Library Application......!";
     }
 
}


4.      Create a new Android Project

5.      Add library in this project , use some following steps-:

·     Right click in your project and choose Properties  as given below

Creating libraries for Android applications

·         Now open a window choose Android as given below

Creating libraries for Android applications

·      Press add button and choose your library then click ok

Creating libraries for Android applications

After click ok press Apply button.

 Creating libraries for Android applications

 

Now you have imported library in your application project. 

6.      Add following code in MainActivity.class

 

package com.example.androiddemolibrary; 
import com.example.mylibrary.MyDemo; 
import android.os.Bundle;
import android.app.Activity;
 
import android.view.Menu;
import android.widget.TextView;
 
public class MainActivity extends Activity {     TextView textView;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           textView = (TextView) findViewById(R.id.mytextview);
          
           // Call constructor of library class
           MyDemo demo = new MyDemo();
 
           // call method of library class  String text = demo.getMyResult();
           textView.setText(text);
 
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
           // Inflate the menu; this adds items to the action bar if  it is present.
           getMenuInflater().inflate(R.menu.main, menu);
           return true;
     }
 
}

 

Now run your application 

Creating libraries for Android applications


Updated 07-Sep-2019

Leave Comment

Comments

Liked By