API Development

Integrating an external library in an Android module

In my previous blog post, I talked about the Module Development Guide reference modules available for iOS and Android and how you can use them to develop your own custom modules. That should provide you with a good foundation for your module development needs. But one of the primary motivations for developing a Titanium module is to expose the methods and properties of an external library. In this post we’re going to look at one way to incorporate an external library into your Android module.

Add External Library

For this discussion we’re going to look at integrating the Google AdMob library into our module. The complete source to the Admob module can be found in our public modules repository on GitHub. The first thing that must be done is to download the jar file for the external library. In this case, the external library file is GoogleAdMobAdsSdk-4.1.1.jar. Place this file into the ‘lib’ folder of your module project.

Add External Reference

Next we need to let the project know that we want this library packaged with the module. This requires that the .classpath file be updated with the path to the library. If you are using Eclipse for your development, then select project->properties from the menu, select “Java Build Path” in the left panel and then select the “Libraries” tab.

Now click “Add External JARs…” and locate the GoogleAdMobAdsSdk-4.1.1.jar file in your project folder.

Click “OK” to add the JAR file to your project. If you now look at your .classpath file in the project folder you should see an entry for your JAR file. If you are not using Eclipse then you can manually edit the .classpath file and add this entry.

Update timodule.xml

At this point, you have added the external library to your module project. If your module requires entries in the application’s manifest file, you will need to update the timodule.xml file. The entries in the timodule.xml file are merged into the androidmanifest.xml file when an application that includes your module is built. In our example, we do require an entry for the Admob activity and a couple of permission settings. The timodule.xml file should be updated.

Import Classes

Your external library is now ready to be used in your module. All that is left to do is import the classes and implement your module classes, methods, properties, and notifications. To wrap up our example, we can import the required AdMob classes and begin defining our classes.

Resources

Module Development Guide Reference Modules blog post
Module Developer Guide for Android