Toggle Menu

Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Core Code

OS and Tools



Android Libraries

We frequently find ourselves reusing the same code over and in our applications. Over time, we define a set of routines that we want to reuse. We can do several things with these routines:

  • Put in them in a jar file and use that external jar file in our application.
  • Create a service
  • Create a library

In this text we are going to focus on the third technique. This technique links the code from the library into your main project. This means the code for your library could be copied to the target machine multiple times, once for each program that uses the library. A library cannot use another library. Still, I like the idea of libraries in some cases.

Create the library as you would any standard Android Project. In Proprieties | Android check the IsLibrary box.


Figure 01: Click to enlarge this image.

In the project that uses the library, click the Add button and Add in the library.

Adding a Library to a

Figure 02: Click to enlarge


Here is the code for a simple library:

package com.elvenware.simplelibrary;

public class SimpleLibrary extends Object 
 pu  public int GetNine()
      return 9;
} 9;
} 9;

Here is the code for a program that consumers the library:

package com.elvenware.simplelibraryuser;

import com.elvenware.simplelibrary.SimpleLibrary;
import android.os.Bundle;
import android.widget.TextView;

public class SimpleLibraryUserActivity extends Activity 

  public void onCreate(Bundle savedInstanceState) 

    SimpleLibrary library = new SimpleLibrary();
    int nine = library.GetNine();

    TextView textViewMain = (TextView)this.findViewById(;
    textViewMain.setText(String.format("The answer is %s", nine)); 

Copyright © Charlie Calvert | Elvenware Home | Writing Code | Delphi | CSharp | My Books