Android Library Project

We can create a three type of project in android.
  • Android project
  • Library project
  • Test project
You can download source code here.

You can create a library project where we can design classes,resources.We can share this library project to another android project. Generation of APK file is not possible with library project.

For example,Facebook SDK is a library project.If we include this Facebook SDK we can use classes and resources of facebook.

We can create library project in two ways, Creating a new library project and another way is converting existing android project into a library project.

Creating a Library Project

We can create a library project in the same way as you would a new android application project.

Steps to create a new library project:
  • Select File > New > Android Application Project.
  • Enter the basic settings for the project, including Application Name as CreateLibraryProjectDemo, Project Name, Package Name as "com.etr.libraryprojectdemo", and SDK settings.
  • In the Configure Project page, select the Mark this project as a library option to flag the project as a library.
  • Set the other options as default and click Finish.
Refer to the below screenshot for creating a library project.


This is code for LibraryActivity and it have addition logic.

package com.etr.libraryprojectdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class LibraryActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.library, menu);
return true;
}

}

Converting Existing Android Project into a Library Project

Steps to converting existing android project into a library project:
  • In the Package Explorer, right-click the existing android project and select Properties.
  • In the Properties page, select the Android. 
  • Select the is Library check box in Library panel and click Apply and then click OK.

Refer to the below screenshot for converting existing android project into a library project.

   
Referencing a Library Project


Steps to add a reference to a library project:

  • Both existing android project and library project should be in same workspace.

  • In the Package Explorer, right-click the existing android project and select Properties. 

  • In the Properties page, select the Android.

  • Click Add button in Library panel  to open the Project Selection dialog.

  • From the list of available library projects, select a library project and click OK.

  • Click Apply and OK  to close the Properties window. 

  • Refer to the below screenshot for Referencing a Library Project.



    We can import library project addition logic into our android project. This is code for importing library project source. First of all we are importing a library project packages.

    import com.etr.libraryprojectdemo.LibraryActivity;

    And then create instance for LibraryActivity and then call addition(a,b);

    LibraryActivity act = new LibraryActivity();
    int c = act.addition(10, 20); 


    Comments

    Popular posts from this blog

    SQLiteDatabase With Multiple Tables

    Programmatically turn ON/OFF WiFi on Android device

    Android Service and IntentService