Category Archives: ContentProviderCodeGenerator

ContentProviderCodeGenerator or how to create your ContentProviders easily

There are multiple ways to store data persistently in Android, like for example the SharedPreferences. One of them is to use a ContentProvider.

A ContentProvider is one of primary building blocks of Android applications, providing content to applications. You can for example use it to open some of your data to other applications.
You can also use it to just store your data and not export it to other applications (by setting the flag android:exported="false" in your Android Manifest).

Write your first ContentProvider can be a challenging task as there are a good number of methods to implement and it’s not always clear how to do it.
That was one of the reason why I released with DataDroid (my local and remote data management library) some skeletons explaining how to do it by just following a bunch of TODOs to follow.

However you still have a lot of code to write, most of it just being copy/paste all over the classes. And when you start having multiple versions of your ContentProvider with new columns or tables, it begins to be really annoying to have to maintain that…

Which brings us to ContentProviderCodeGenerator !!

Like every lazy software engineer, it was bothering me and so I took/found some time and wrote a code generator ! :)

You can find all the information on its GitHub page, but here is a short explanation :

  • You clone the repository and import the Java project in Eclipse (or your favorite IDE)
  • You write a JSON file defining your tables and their columns in the input folder. Examples are provided to learn the syntax
  • You run the project as a Java Project
  • All the needed files are “magically” generated in the output folder ready for you to copy them in your project

It’s really easy to use (and if needed I can help you)
It makes adding a column or a table so easy it’s not even funny.
It just works. (yes I know I just used an Apple slogan on an Android blog) (and if it doesn’t work, I’ll fix it)

Last important thing : the project license. As all my other projects, it is released under the Beerware license :

You can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return.

Page optimized by WP Minify WordPress Plugin