30 Jun 2015

GreenDao ORM in Android Studio Part1


Every android developer knows that boilerplate you are facing while working with SQLite Database. A lot of code that is repeated so often.

To avoid this boilerplate you can use different ORM. I'm going to show how to use greendao. But also below I will give brief description of the best 5 ORMs.


It's not android but java ORM. If you got used to annotations probably you would like it.


This ORM was created for Android. It's has easy to use and memorizable API and other sweet features


This ORM is doing the same things as the other ones, also it has different annotations that make code more beautiful


It's written on C++ and has great performance


If you need great performance you should definitely look on GreenDao. It's working in 3.5-4 times faster than ORMLite

Using GreenDao in Android Studio

Adding dependencies

You are going to need 3 jars that you can get here:



greendao generator

Or you can just use gradleplease from my former post

And add dependencies to gradle without downloading any jars

Note: there are some issues with freemaker if you are using maven-central dependencies.

Adding greendao generator to your project

GreenDao generator is wrapping your database, creating data model. It generates classes that you usually write by yourself. Generator is doing this annoying work for you.

By the way, generated classes look nice and are easily editable. So if you want some specific additional functionality you are free to edit them.

First, create new module on your project

smth went wrong

Then select Java Library

smth went wrong

smth went wrong

Modify build.gradle in your module following way:

apply plugin: 'application'
apply plugin: 'java'

mainClassName = "com.somepackage.greendao.GreenDaoGeneratorM"
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/greendao-generator-1.3.1.jar')
    compile files('libs/freemarker-2.3.22.jar')

Creating the data model

Open the class GreenDaoGeneratorM and add following:

public static void main(String args[]) throws Exception {
        Schema schema = new Schema(1, "com.guthub.jackwarthongithub.dbmodel");

Then fill the main method with following content:

        Schema schema = new Schema(1, "com.guthub.jackwarthongithub.dbmodel");

        Entity person = schema.addEntity("Person");

        Entity lease = schema.addEntity("Lease");

        Property personId = lease.addLongProperty("personId").getProperty();
        lease.addToOne(person, personId);

        ToMany personToLease = person.addToMany(lease, personId);

        new DaoGenerator().generateAll(schema, "../app/src/main/java");

As you can see, you are able to create relations without any sql code

To actually generate the model make "run" command in gradle on the right side of Android Studio

smth went wrong

That's it. Now if everything is fine. The generated classes will be created in package "dbmodel"

smth went wrong

smth went wrong

Using greendao is simple. You can find different information in official documetation

Next post I will show how to use greendao on some real example. I promise to show you OneToMany and OneToOne relations

blog comments powered by Disqus