5 Best Android ORM Libraries to Use

When your app requires lots of database activities, using an ORM is the best way to reduce your workload so you can focus on other aspect of the app.

I hope you can choose a suitable Android ORM after reading this post.

OrmLite

Object Relational Mapping Lite (ORM Lite) is a simple and lightweight Java ORM. The ORM is supported in many Java platforms such as Spring.

ORMLite supports native calls to Android SQLite database APIs and its main website has separated documents for Android only.

@DatabaseTable(tableName = "account")
public class Account {
    @DatabaseField(id = true)
    private String username;
    
    @DatabaseField(canBeNull = false)
    private String password;
    
    Account() {
    	// a must defined no-arg constructor
    }
    public Account(String username, String password) {
        this.username = username;
        this.password = password;
    }
    //...getter and setter methods can go below
}

OrmLite is open source and can be found on GitHub.

Website: http://ormlite.com.

ActiveAndroid

I use ActiveAndroid for most of my projects. ActiveAndroid is an active record style ORM which allows you to save and retrieve SQLite database records without ever writing a single SQL statement. The library takes care of all the settings and SQLite stuff, and all with just some simple code lines and few simple steps of configuration.

You can either download the JAR file or compile the library folder using ant command.

@Table(name = "Books")
public class Item extends Model {
    @Column(name = "Name")
    public String name;

    @Column(name = "Price")
    public float price;

    @Column(name = "Publisher")
    public String publisher;
}

Website: http://www.activeandroid.com/

greenDAO

I haven’t tried greenDAO yet but I assume it is better to use the library when your project contains lots of data tables. greenDAO is considered to be the fastest ORM for Android. There are many top Android apps such as Pinterest, Path are relying on greenDAO. Many of them have over 10 million installs.

greenDAO-orm

Example of creating an object and adding properties to the object:

Entity account = schema.addEntity("Account");
account.addIdProperty();
account.addStringProperty("username");
account.addStringProperty("password");

Website: http://greenrobot.org/greendao

SugarORM

SugarORM is an ORM developed only for Android in order to simplify the interaction with SQLite database.

public class Game extends SugarRecord {
  String title;
  String developer;

  public Game(){
  }

  public Game(String title, String developer){
    this.title = title;
    this.developer = developer;
  }
}
//Add new 
Game game = new Game("Uncharted 4", "Naughty Dog");
game.save();
//find and delete a game 
Game game = Game.findById(Game.class, 1);
game.delete();
//list all games
List<Game> games = Game.listAll(Game.class);
Game.deleteAll(Game.class);

Website: http://satyan.github.io/sugar/index.html

DBFlow

DBFlow is a simple and powerful android database library with annotation processing. It is designed to be fast, high performance, and easy to approach. It reduces codes for dealing with databases with its powerful and simple API. DBFlow is boasted as a more efficient and powerful alternative to the other Android ORMs. According to the developer’s own benchmark, DBFlow is the fastest Android ORM.

//define a database
@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase {
    public static final String NAME = "App";
    public static final int VERSION = 1;
}
//define a table
@Table(databaseName = AppDatabase.NAME)
public class TestModel1 extends BaseModel {
    @Column(columnType = Column.PRIMARY_KEY)
    public
    String name;
}

Website: https://github.com/Raizlabs/DBFlow

Other good Android ORM libraries

These are also good ORM libraries which I haven’t tried yet.

Requery. It is a modern SQL based query & persistence library for Android. Requery provides a modern set of interfaces to perform and persist queries.

Ollie. Ollie is a compile-time active record ORM for Android. It supports RxJava.

RushOrm. Similar to other libraries, RushOrm replaces the need for SQL by mapping java classes to SQL tables. It can be used in projects with complex data structures and projects that communicated with a REST API.

  • Jamling

    A alternative android orm library: https://github.com/Jamling/Android-ORM/ with eclipse plugin helpers

  • Check out JDXA ORM for Android. Based on [KISS Principles](http://softwaretree.com/v1/KISSPrinciples.html), JDXA is a simple yet powerful, non-intrusive, and flexible ORM.

    JDXA easily supports complex object modeling including inheritance, one-to-one, one-to-many, and many-to-many relationships and offers a POJO (Plain Old Java Objects) friendly non-intrusive programming model, which
    does not require you to change your Java classes in any way:

    – No need to subclass your domain classes from any base class
    – No need to clutter your source code with annotations
    – No need for DAO classes

    JDXA also offers convenient persistence for JSON objects.

    Here are some JDXA ORM code snippets: http://softwaretree.com/v1/products/jdxa/code-snippets.html