(Private) Libraries in Android. Part II

(Private) Libraries in Android. Part II

In the first post about libraries in Android, I proposed two different solutions to share a project with resources but without sharing the source code:

In this post, I will show in detail the first one: creating a Library Project to include the resources, but including the source code using a JAR file. The steps are the following:

  1. Create a java project to add just the code, not resources.
  2. Export the java project to a JAR file excluding the source code.
  3. Create a Library Project to include the resources.
  4. Add to the Library Project the previous created JAR.
  5. The client project will import the Library Project which contains the resources and the JAR file.

Private library process

Let’s show a short example. Suppose we want to create a library to provide a log-in screen to authenticate users in our system. We want to keep the code that connects to our servers and authenticates the users in private. We also want to provide the user interface in order to maintain our own corporative design.

1. Create the Java project

Create a Java project and create a package and a class in it. Let’s name the class “Authenticator”, a final class with one static method.

Java project structure

Add the method code:

package belencruz;

public final class Authenticator {
    * Method to authenticate users.
    * @param user User name
    * @param pass User password
    * @return true if login is correct
   public static boolean authenticate (String user, String pass) {
      // Connect to my server to authenticate

      // For this example:
      if(user.equals("Belen") && pass.equals("belen"))
         return true;
      return false;

Export the project to a .JAR file excluding the source code. If you are using eclipse, then in the export dialog, just check the “Export generated class files and resources” option, but don’t check the option “Export Java source files and resources“.

2. Create an Android Library Project

Create a new Android project and check it as a library project.

Library project check

Import our library .JAR from the previous step. The project structure now looks like this:

Library project structure

Open the main layout and create a simple login layout. To avoid conflicts between names, it is a good idea to change the name of this layout file, for example, to “library_activity_main.xml”.

Login layout

Now open the main activity and add the method that will be run when the user clicks on the Accept button. Add the code to call the method of our .JAR:

import belencruz.Authenticator;

* When user clicks the Accept button. Login.
* @param v
public void onAcceptClick(View v) {
   final EditText et_user = (EditText) findViewById(R.id.editText_user);
   final EditText et_pass = (EditText) findViewById(R.id.editText_pass);

   new Thread(new Runnable() {
      public void run() {

In this method, we get the user credentials from the layout and start the authentication process. Since the login is a costly process, we execute it in a new thread. To simplify the example, I used a Runnable object, a better way would have been the use of an AsyncTask.

The project library is now ready to be imported in other Android projects.

3. Create the client Android project

Create a new Android project, not library this time. Add the library project to this new one.

Add the library project

In the main layout of this project, add a login button. When the user clicks the button, the user interface will switch to the login layout from our library. So, in the main activity, add the following code in the handler method of the login button:

public void onLoginClick(View v){
   Intent i = new Intent(this,

Finally, don’t forget to declare the library activity in the manifest file of the client project. Open the AndroidManifest.xml from the client project and inside the application tag, add the following declaration:

Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.