GreenDao Android Studio

TomCB picture TomCB · Sep 17, 2014 · Viewed 17.1k times · Source

I'm looking for a clear step-by-step explanation on how to import GreenDao in Android Studio.

I've used it before in AS, but failed to get it to work again. There are some tutorials out there, but they don't seem to apply to the latest version of AS.

When I clone from github, I get a example project stuff etc. Is there a way to install GreenDaoGenerator without these extras?

Just looking for an up-to-date step-by-step explanation.

Update: I suggest using Realm.io now! Check it out! :-)

Any help would be appreciated!

Answer

nitesh goel picture nitesh goel · Dec 8, 2014

Tested on Android Studio 2.0

With Android Studio 0.6.1+ (and possibly earlier) you can easily add non android project to your android project as a module.

Using below method you can have Java modules(greenDaoGenerator) and Android modules in the same project and also have the ability to compile and run Java modules as stand alone Java projects.

  1. Open your Android project in Android Studio. If you do not have one, create one.
  2. Click File > New Module. Select Java Library and click Next.
  3. Fill in the package name, etc and click Finish. You should now see a Java module inside your Android project.
  4. Open the build.gradle file of the java project and add the following dependency

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile('de.greenrobot:DaoGenerator:1.3.0')
    }
    
  5. Copy your DaoGenerator classes or create if you don't have one to your java module.For e.g. I have created ExampleDaoGenerator class in my java module.

    public class ExampleDaoGenerator {
    
        public static void main(String[] args) throws Exception {
            Schema schema = new Schema(1000, "de.greenrobot.daoexample");
            addNote(schema);
            new DaoGenerator().generateAll(schema, "../DaoExample/src-gen");
        }
    
        private static void addNote(Schema schema) {
            Entity note = schema.addEntity("Note");
            note.addIdProperty();
            note.addStringProperty("text").notNull();
            note.addStringProperty("comment");
            note.addDateProperty("date");
       }
    
    }
    

Now, to generate the classes that you can use in android project follow below steps.

  1. Click on the run menu in the top bar. Click Edit Configurations...
  2. In the new window, click on the plus sign at the top left of the window and select Application
  3. A new application configuration should appear, fill the following information.

    1. Give it a name e.g. greenDao.
    2. In main class click … button and select your generator class which have the main method.for e.g. in this case it is com.greendao.generator.ExampleDaoGenerator
    3. In working directory select path of your java project.
    4. In use class of module select you java project. click ok.
    5. Again go to run menu and now you can see e.g. run greendao. click on it.It should compile successfully.

Its done !!! you can check your generated classes in the folder that you have specified.For e.g. in this case it is /DaoExample/src-gen

NOTE: You can run your android project again by clicking on run menu -> Edit Configuration . select your project and click ok.