Create an Android library project
Developing Android Applications does not differ from developing any other applications. After building few projects you realize you re-use some piece of code, and you do not want to duplicate code.
In Android, we commonly call this Android Library Project
, or also known as the famous .JAR file. This lets you create a self-contained library or library project that contains all the common files you need for all your projects.
Unfortunately, (as of today – Jan 3rd 2015) Android Studio does not really support the creating of .JAR file. Android Studio supports .AAR file.
It uses the same concept using binary file with a different extension name.
To create the .ARR file you update your build.gradle and use this tag: apply plugin: 'android-library'. This will tell Android Studio that this is a library project and save the .AAR file in the build/outputs/aar/
folder.
Because I do not use Eclipse, we will use the command line (which is honestly way better and faster).
Create the .JAR file.
First, we need to create the structure (folder, basic components) of the library project. You need to specify the name of your project, the target, the path and the package name.
android create lib-project -n MyLibrary -t android-15 -p /path/to/MyLibrary -k com.mylibrary
And create the placeholder for the Java code.
mkdir -p /path/to/MyLibrary/src/com/mylibrary/
Now that the basic structure is completed, let's put some code!
In the /path/to/MyLibrary/src/com/mylibrary/
you need to place all your Java file. For this example, let's use this code:
package com.mylibrary; public class LibraryDemo { private String name; public LibraryDemo() { this.name = "Book of Zeus"; } public String getSiteName() { return this.name; } }
That's it!
Create the .JAR file
cd /path/to/MyLibrary/
By default, ANT will create the .JAR file in the “bin/” folder with a common name (classes.jar). You can change this by updating the “build.xml” and adding (or updating) this line:
<property name="out.library.jar.file" location="bin/${ant.project.name}.jar" />
Finally, build it:
ant release
Test your .JAR
It is important to create unit test in order to validate your library. You do not want to crash your application because you forgot something important.
Option 1: jUnit
Visit http://junit.org/ to get the latest jUnit Jar file.
Option 2: Custom Unit Test
... or, you can create your own set of test cases. I like to separate my test cases from my library. This way your .JAR file will smaller, and you probably do not need all the test cases in your application.
To create your own test cases you will need to create a test case folder:
mkdir -p /path/to/MyLibraryTest/
Copy your .JAR file.
cd /path/to/MyLibraryTest/ cp /path/to/MyLibrary/bin/LibraryDemo.jar .
Now, let's create a simple LibraryDemoTest
class (LibraryDemoTest.java).
import com.mylibrary.*; public class LibraryDemoTest { public static void main(String[] args) { LibraryDemo lib = new LibraryDemo(); String name = lib.getSiteName(); // test if true System.out.println("Test if TRUE: " + LibraryDemoTest.myAssertTrue(name)); // test if false System.out.println("Test if FALSE: " + LibraryDemoTest.myAssertFalse(name)); } // my Assert methods private static boolean myAssertTrue(String name) { return name.equals("Book of Zeus"); } private static boolean myAssertFalse(String name) { return !name.equals("Luke Skywalker"); } }
Now, here's the trick. Because you need to import your library file, you need to where to specify the classpath to make sure Java will include the library properly.
javac -cp .:./MyLibrary.jar LibraryDemoTest.java
Then run it
java -cp .:./MyLibrary.jar LibraryDemoTest
That's it! Now it's time for you to code and add all your test cases!