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.

Similar Posts:

  1. Thanks man)
    I think about creating web service for it

  2. druidamix (1 comments)

    Can you add support for default value?

    Thanks

  3. Foxykeep (47 comments)

    @druidamix : just sent you an email to get more information about your request

  4. “It just works”

    PERFECT!!!!!!

    You are the BEST!

    Thank you!

  5. James (1 comments)

    should we use this or mdsd-android-content-provider ??

    I downloaded and installed the eclipse modeling tools. Looks very nice. The ContentProviderCodeGenerator seems a bit more primitive, but the code also looked very complete and I am wondering which is better.

  6. Foxykeep (47 comments)

    I don’t know the other one at all. So … try both ? :)

  7. mikeT (1 comments)

    Thank you for that nice piece of code, i am just wondering if one to many relationships are supported as well?

    Best regards
    Mike

Leave a Reply

Page optimized by WP Minify WordPress Plugin