Integrating greenDAO into your Android Application

Published Feb 03, 2017
Integrating greenDAO into your Android Application

greenDAO is an open source Android ORM that makes developing for SQLite databases fun again. It prevents developers from dealing with low-level database requirements and saves development time.

An ORM is a layer between the relational SQLite database and the object-oriented app code. ORM allows developers to use the database without transforming objects into compatible formats for the relational database.

At the end of this tutorial, you should be able to integrate greenDAO into your android application in less than 10 minutes.

Features of greenDao

  • Maximum performance (probably the fastest ORM for Android); our benchmarks are open sourced too
  • Easy to use powerful APIs covering relations and joins
  • Minimal memory consumption
  • Small library size (<100KB) to keep your build times low and to avoid the 65k method limit
  • Database encryption: greenDAO supports SQLCipher to keep your user’s data safe
  • Strong community: More than 5.000 GitHub stars show there is a strong and active community

Integrating greenDAO with Android Application

The following steps outline the procudure for integrating greenDAO with Android applications.

Step 1:

If you have an app already, you can skip this step. Otherwise, create an Android project from Android Studio:
File->New->New Project

Step 2:

Go to your build.gradle(Module:app) app level gradle file and add 'org.greenrobot:greendao:3.2.0' to your dependencies. Sync the gradle file

Add greenDao to your dependencies

Step 3

Go to your build.gradle(Prioject:greendaoapp) project level gradle file. Add 'org.greenrobot:greendao-gradle-plugin:3.2.0' as a class path to your dependencies and sync the gradle file

Add greenDao classpath to project level gradle file

Step 4

We'll create a greenDao generator here. This generator will be responsible for automatically creating entities and dao files. If you
want to read more about this, visit GreenDao Generator

  • Create a module by going to File->New->Module
  • Select "Java Library" and Click Next
  • Give the module a name. Here, we will use: greenDaoGenerator
  • Package name: com.greendao here
  • Java Class name: MyGenerator here
  • Click Finish

adding generator module

You will notice that your project folder structure now contains two modules. One app and the other greendaogenerator


Step 5

Open the gradle for the new module, add org.greenrobot:greendao-generator:3.2.0 to the dependencies, and sync.

adding dependency to new generator module

Step 6

Now we are going to modify our generator class so we can generate the dao files and entities (Tables). In this example we will be creating a table called users
with columns user_id, last_name, first_name, and email.
See the updated generator class below:

package com.greendao;

import org.greenrobot.greendao.generator.DaoGenerator;
import org.greenrobot.greendao.generator.Entity;
import org.greenrobot.greendao.generator.Schema;

public class MyGenerator {
    public static void main(String[] args) {
        Schema schema = new Schema(1, "com.appsng.greendaoapp.db"); // Your app package name and the (.db) is the folder where the DAO files will be generated into.


        try {
            new DaoGenerator().generateAll(schema,"./app/src/main/java");
        } catch (Exception e) {

    private static void addTables(final Schema schema) {
       // addPhonesEntities(schema);

    // This is use to describe the colums of your table
    private static Entity addUserEntities(final Schema schema) {
        Entity user = schema.addEntity("User");
        return user;
 //    private static Entity addPhonesEntities(final Schema schema) {
 //        Entity phone = schema.addEntity("Phone");
 //        phone.addIdProperty().primaryKey().autoincrement();
 //        phone.addIntProperty("user_id").notNull();
 //        phone.addStringProperty("number");
 //        return phone;
 //    }

After this, run the generator class. Right click on the generator class then Run MyGenerator.main().


This will run for a while; if properly done as explained, you will get a success response at the end of the execution.

After a success response goes back to your main app, you should see a new folder called db with DAO files generated and Table entities(models)


If you find these files, that means we are good to go.

Step 7

In your main app, create a class that extends the Application Object. In this case I created a class called AppController. Remember to make the name of your application in your
AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android=""
        package="com.appsng.greendaoapp" >
            android:theme="@style/AppTheme" >
            <activity android:name=".MainActivity" >
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />

Step 8

Go back to the AppController class and modify it. See updated AppController class below and see explanation in comments.

package com.appsng.greendaoapp;


import com.appsng.greendaoapp.db.DaoMaster;
import com.appsng.greendaoapp.db.DaoSession;

import org.greenrobot.greendao.database.Database;

 * Created by Akinsete on 1/14/16.

public class AppController extends Application {

    public static final boolean ENCRYPTED = true;
    private DaoSession daoSession;

    public void onCreate() {

        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"users-db"); //The users-db here is the name of our database. 
        Database db = helper.getWritableDb(); 
        daoSession = new DaoMaster(db).newSession();

        ///// Using the below lines of code we can toggle ENCRYPTED to true or false in other to use either an encrypted database or not.
//      DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "users-db-encrypted" : "users-db");
//      Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
//      daoSession = new DaoMaster(db).newSession();

    public DaoSession getDaoSession() {
        return daoSession;


That's it! We have successfully integrated greenDAO into our android application! If you would like to add other tables, go to the generator class, edit the entities, and run the
generator class. This will generate the DAO files and other entities needed.

Access full project here Github Repo

I hope this was useful for you. If you have any questions or issues, please feel free to drop a comment.

Cheers! Happy coding 🎉

Discover and read more posts from Sunday Akinsete
get started