Top 5 Android ORMs

What are ORM Frameworks?

ORM is yet another nerd-acronym, it is short for Object Relational Mapping. In a nutshell, an ORM framework is written in an object oriented language (like Java, C#, PHP, etc…) and it is designed to virtually wrap around a relational database.

db

Why do we need ORM’s on Android?

Developing Android Applications often involves storing, reading and updating data. Sometimes we may choose to store data into the cloud, so we might use a SyncAdapter. But sometimes the best idea is to store the data into a SQLite Database. Of course, SQL is a skill every developer must have, but writing SQL Queries is boring. ORM’s are making our life easier, so why not use them ? Here is a list of most popular ORM’s available out there for Android:

1. greenDAO

According to its website, “greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases. Being highly optimised for Android, greenDAO offers great performance and consumes minimal memory”. It is used by a lot of popular apps, including Pinterest, Path, Glide and many others. Being smaller than 100KB, greedDAO might be the perfect fit for your next app.

greenDAO-performance

2. Sugar ORM

Another simple to use and robust ORM, built especially for Android. Giving us 3 simple methods, save(), delete(), find() or findById(), SugarORM makes CRUD very simple. It also creates tables automatically and provide simple methods for making one-to-one and one-to-many relationships. Include this ORM is your app by adding this line in your app.gradle file

compile 'com.github.satyan:sugar:1.3'

then add these 4 meta-tags in your AndroidManifest.xml file:

<meta-data android:name="DATABASE" android:value="database.db" />
<meta-data android:name="VERSION" android:value="1" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />

Now we can use this ORM like this:

public class Book extends SugarRecord {
    String title;
    String author;
    public Book() { }
    public Book(String title, String author){
        this.title = title;
        this.author = author;
    }
}

So, saving a new book will be:

Book book = new Book(getContext(), "The world of Ice and Fire", "George R.R. Martin");
book.save(); 

3. ORMLite

ORMLite is an open-source framework that provide lightweight object relational mapping between Java classes and SQL Databases. It’s simple and easy to use and understand. You can use it not only with Android, but with other Java apps, for example: web-apps or desktop apps and it does have a lot of useful features like using annotations, compiles SQL statements for repetitive query tasks and supports database transactions. This ORM framework is very popular in Java World. You can get familiar with it here.

4. ActiveDroid

ActiveDroid is another ORM that saves your time and resources, allowing you to save and retrieve SQLite Database records easily. It’s annotation based, very simple to use and to get familiar with it. You can include it in your project using maven or gradle. Let’ s see a quick example of how we can use it:

First of all, we need to copy the .jar file into our /libs folder. Next, we should add these tags to our AndroidManifest.xml file:

<meta-data android:name="DB_NAME" android:value="database.db" />
<meta-data android:name="DB_VERSION" android:value="1" /> 

After we’re done with that, we need to add this line in our onCreate() method of our activity.

ActiveAndroid.initialize(this);

That’s all, now we can can create our models just as Java classes using Annotations, like this:

@Table(name = "User")
public class User extends Model {
    @Column(name = "username")
    public String username;
    @Column(name = "password")
    public String password;
    public User() {
        super();
    }
    public User(String username,String password) {
        super();
        this.username = username;
        this.password = password;
    }
} 

5. ORMDroid

Last ORM we will talk about will be ORMDroid, that is another simple ORM persistence framework for Android applications. It is small, no more than 30KB, simple, flexible and automatic. It works only with Android and it is actively maintained, so you might consider this it when choosing ORM for your next app.

Conclusion

Android databases are much more implementation-heavy than their iOS counterparts, so choosing an ORM is a very important step. At Atomate, for our next app — a medium-complexity Android client, we are going to use SugarORM because of its simplicity and speed. It’s actively maintained and provides good documentation.

Of course, choosing an ORM for your next app should involve a fair amount of investigation and planning on how “big” your app might grow. It’s hard to say that one ORM is better than another, so knowing the exact requirements of your next project should help you to choose correctly.

Good luck and happy coding!

Leave a comment

4 Comments

  1. Alex says:

    Very interesting!
    Thank you!

  2. Raed says:

    So helpful, Thanks

  3. imany_apk says:

    can we have some complex query like JOIN or LIKE or multi JOIN or anyother in SugarORM ?

  4. Nikita says:

    Thank you!