(Private) Libraries in Android: Part III

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 second one: to create the resources programmatically instead of using the XML files and export it to a JAR file. The steps are the following:

  1. Create a java project to add the code.
  2. Import the Android SDK into the java project.
  3. Create the resources programmatically.
  4. Export the java project to a .JAR file excluding the source code.
  5. Import the .JAR into the client project or add an intermediate Android Library Project.

Private library process

Let’s show a short example, the same example used in the part II. 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 and now we will keep that user interface in private too.

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. This class has the code of our authentication system.

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;

2. Import the Android Jar

Add the Android library to the project as you would do to add any external library .jar. You will find de android.jar file in the Android installation from your system, for example in a path like /androidSDK/platforms/android-18/android.jar.

android.jar in project

3. Create the resources

We are creating the resources programmatically, that’s why we added the android.jar in the previous step. Create a new final class named as “LayoutCreator”. In this class we add the method that will contain the code to create the login layout. We can add different methods for different views. Additionally, we will have to create the R.java class to store the IDs of the layout components.

Library structure

package belencruz;

public final class LayoutCreator {
    * Method to create the login view.
    * @param context Activity that needs the view
    * @return login View
   public static View createLoginLayout(final Activity context) {
      LinearLayout loginLayout = new LinearLayout(context);


      return loginLayout;

Now just the code to create the components inside the layout is left. To create the text inputs, add the following code:

// Edit Text User
EditText userInput = new EditText(context);

// Edit Text Password
EditText passwordInput = new EditText(context);
passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);


Lines 13 and 14 add the components to the login layout. To create the accept button add this code:

// Button Accept
Button loginButton = new Button(context);


And finally we need to handle the button event. 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.

loginButton.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
      new Thread(new Runnable() {
         public void run() {
            TextView user = (TextView) context.findViewById(R.id.userInput);
            TextView password = (TextView) context.findViewById(R.id.passwordInput);


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“.

4. 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.

Open the main activity and in the onCreate method we need to change the view that is loaded from this activity. Change the call to the setContentView method to this:


5. 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

1 Comment

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.