5 of the Best Android ORMs

Share this article

If you are developing an Android application, you will likely need to store data somewhere. You may choose a Cloud service (in which case, using a SyncAdapter would be a good idea), or to store your data in the embedded SQLite database. If you take the second option, you will have to decide between writing SQL queries, using a Content Provider (useful if you want to share your data with other apps), or using an ORM.

In this article, I will discuss some of the Android ORMs you may consider using in your application.

OrmLite

OrmLite is the first Android ORM that comes to my mind. However OrmLite is not an Android ORM, it’s a Java ORM with SQL databases support. It can be used anywhere Java is used, such as JDBC connections, Spring, and also Android.

It makes heavy usage of annotations, such as @DatabaseTable for each class that defines a table, or @DatabaseField for each field in the class.

A simple example of using OrmLite to define a table would be something like this:

@DatabaseTable(tableName = "users")
public class User {
    @DatabaseField(id = true)
    private String username;
    @DatabaseField
    private String password;

    public User() {
        // ORMLite needs a no-arg constructor
    }
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    // Implementing getter and setter methods
    public String getUserame() {
        return this.username;
    }
    public void setName(String username) {
        this.username = username;
    }
    public String getPassword() {
        return this.password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

OrmLite for Android is open source and you can find it on GitHub. For more information read its official documentation here.

SugarORM

SugarORM is an ORM built only for Android. It comes with an API which is both simple to learn and simple to remember. It creates necessary tables itself, gives you a simple methods of creating one-to-one and one-to-many relationships, and also simplifies CRUD by using only 3 functions, save(), delete() and find() (or findById()).

Configure your application to use SugarORM by adding these four meta-data tags to your apps AndroidManifest.xml:

<meta-data android:name="DATABASE" android:value="my_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.my-domain" />

Now you may use this ORM by extending it in the classes you need to make into tables, like this:

public class User extends SugarRecord<User> {
    String username;
    String password;
    int age;
    @Ignore
    String bio; //this will be ignored by SugarORM

    public User() { }

    public User(String username, String password,int age){
        this.username = username;
        this.password = password;
        this.age = age;
    }
}

So adding a new user would be:

User johndoe = new User(getContext(),"john.doe","secret",19);
johndoe.save(); //stores the new user into the database

Deleting all the users of age 19 would be:

List<User> nineteens = User.find(User.class,"age = ?",new int[]{19});
foreach(user in nineteens) {
    user.delete();
}

For more, read SugarORM’s online documentation.

GreenDAO

When it comes to performance, ‘fast’ and GreenDAO are synonymous. As stated on its website, “most entities can be inserted, updated and loaded at rates of several thousand entities per second;. If it wasn’t that good, these apps wouldn’t be using it. Compared to OrmLite, it is almost 4.5 times faster.

greenDAO vs OrmLite

Speaking of size, it is smaller than 100kb, so doesn’t affect APK size very much.

Follow this tutorial, which uses Android Studio to show the usage of GreenDAO in an Android application. You can view the GreenDAO source code on GitHub, and read the GreenDAO official documentation.

Active Android

Much like other ORMs, ActiveAndroid helps you store and retrieve records from SQLite without writing SQL queries.

Including ActiveAndroid in your project involves adding a jar file into the /libs folder of your Android project. As stated in the Getting started guide, you can clone the source code from GitHub and compile it using Maven. After including it, you should add these meta-data tags into your app’s AndroidManifest.xml:

<meta-data android:name="AA_DB_NAME" android:value="my_database.db" />
<meta-data android:name="AA_DB_VERSION" android:value="1" />

After adding these tags, you can call ActiveAndroid.initialize() in your activity like this:

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiveAndroid.initialize(this);

        //rest of the app
    }
}

Now that the application is configured to use ActiveAndroid, you may create Models as Java classes by using Annotations:

@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;
    }
}

This is a simple example of ActiveAndroid usage. The documentation will help you understand the usage of ActiveAndroid ORM further.

Realm

Finally Realm is a ‘yet-to-come’ ORM for Android which currently only exists. It is built on C++, and runs directly on your hardware (not interpreted) which makes it really fast. The code for iOS is open source, and you can find it on GitHub.

On the website you will find some use cases of Realm in both Objective-C and Swift, and also a Registration form to get the latest news for the Android version.

Final Words

These are not the only Android ORMs on the market. Other examples are Androrm and ORMDroid.

SQL knowledge is a skill that every developer should have, but writing SQL queries is boring, especially when there are so many ORMs out there. When they make your job simpler, why not use them in the first place?

How about you? What Android ORM do you use? Comment your choice below

Frequently Asked Questions (FAQs) about Android ORMs

What are the key factors to consider when choosing an Android ORM?

When choosing an Android ORM, you should consider several factors. First, consider the ORM’s performance. This includes how quickly it can read and write data, as well as how efficiently it uses memory. Second, consider the ORM’s ease of use. This includes how easy it is to set up, how well-documented it is, and how intuitive its API is. Third, consider the ORM’s flexibility. This includes how well it supports different types of databases, how well it handles complex queries, and how easily it can be customized to fit your specific needs. Lastly, consider the ORM’s community and support. This includes how active its community is, how quickly bugs are fixed, and how responsive its maintainers are to questions and feature requests.

How does an Android ORM improve app performance?

An Android ORM can significantly improve app performance in several ways. First, it can reduce the amount of code you need to write for database operations, which can make your app run faster. Second, it can automate common database tasks, such as creating tables and managing relationships between objects, which can save you time and reduce the risk of errors. Third, it can provide advanced features, such as caching and lazy loading, which can further optimize your app’s performance.

What are the drawbacks of using an Android ORM?

While Android ORMs offer many benefits, they also have some drawbacks. First, they can add complexity to your app, especially if you’re not familiar with ORMs or the specific ORM you’re using. Second, they can introduce performance overhead, especially if they’re not used correctly. Third, they can limit your control over the database, as they often abstract away the underlying SQL. Lastly, they can make it harder to debug database issues, as the ORM can obscure what’s actually happening at the database level.

How does an Android ORM handle relationships between objects?

An Android ORM handles relationships between objects by mapping them to corresponding relationships in the database. This includes one-to-one relationships, one-to-many relationships, and many-to-many relationships. The ORM provides methods for creating, retrieving, updating, and deleting related objects, and it automatically manages the underlying database operations.

Can I use an Android ORM with any type of database?

Most Android ORMs are designed to work with SQLite, which is the default database for Android apps. However, some ORMs also support other types of databases, such as MySQL or PostgreSQL. It’s important to check the ORM’s documentation to see what types of databases it supports.

How do I set up an Android ORM in my app?

Setting up an Android ORM in your app typically involves adding the ORM’s library to your project, configuring the ORM, and defining your data models. The exact steps can vary depending on the ORM, so it’s important to follow the ORM’s documentation.

How do I perform common database operations with an Android ORM?

With an Android ORM, you can perform common database operations using methods provided by the ORM. For example, you can create a new record by creating a new instance of your data model and calling the ORM’s save method. You can retrieve records by calling the ORM’s find or query methods. You can update a record by modifying the instance and calling the save method again. And you can delete a record by calling the ORM’s delete method.

How does an Android ORM handle data migration?

An Android ORM handles data migration by providing methods for creating, updating, and dropping tables. When you change your data models, the ORM can automatically update the corresponding tables in the database. Some ORMs also provide a versioning system that allows you to specify how the database should be updated for each new version of your app.

What is lazy loading and how does it work in an Android ORM?

Lazy loading is a technique where an object’s data is only loaded from the database when it’s actually needed. In an Android ORM, this can be achieved by defining a relationship as lazy. When you retrieve an object that has a lazy relationship, the related object’s data won’t be loaded until you actually access it. This can improve performance by reducing the amount of data that’s loaded at once.

How do I choose between different Android ORMs?

Choosing between different Android ORMs depends on your specific needs and preferences. You should consider factors such as the ORM’s performance, ease of use, flexibility, and community and support. You should also consider your own familiarity with ORMs and your comfort level with the ORM’s API. It can be helpful to try out a few different ORMs to see which one you prefer.

Aldo ZiflajAldo Ziflaj
View Author

Aldo is a Code-Lover and a student of Computer Engineering from Albania. His short-term goal is that of becoming a full-stack developer, focusing on Android, Ruby technologies and DevOps techniques.

chriswdatabasejavaormsql
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week