Wednesday, January 21, 2015

MyNotes: Creating the common library.

My first code change is to update the database.  The schema for the DB is okay, but the code could use a little work.  Since databases use a lot of common terms, this is a good opportunity to create some common classes that could be shared with other apps.  For now, I am just going to make this a separate module within my project.  Once I am happy with the module I may move it into its own repository in GitHub.

Step 1 : Create the Module

Creating a new module is pretty easy.  Just do File->New Module. In the dialog choose the library option:

On the next screen, just name your module and give it a package name:

In the next dialog select "No Activity" and press finish.  Once done it will construct the module, complete with the unit test directories for you.

If you haven't used a Library Module before, you will want to review the rules for library modules at the Android Development Blog.  In general, they are incredibly convenient and useful, but be sure to follow the rules for them.
I want my library project to use the same unit test framework as my main project so I am updating its gradle file to include Mockito and Hamcrest as well (See my previous blog entry for how to do that).

The library project comes with some extra stuff that I don't want so I am going to remove the following resources:

  • ic_launcher.png from the various drawable folders 
  • strings.xml from the values folder.
I also modified the CommonLibrary manifest.xml to remove the label and icon attributes from the application element.

The commit is at

Step 3: Add the Library Module Dependency

The last step is to add a dependency between the MyNotes Module and the CommonLibrary module.  The steps are:
  1. File->Project Structure...
  2. Select the module that will receive the dependency (MyNotes)
  3. Open the Dependency Tab
  4. Click the green "+" sign
  5. Select "Module Dependency" and select the module (CommonLibrary)
It will look like this afterward:

This will automatically update the MyNotes modules build.gradle filewith the module dependency.

The commit is at

No comments :

Post a Comment