TomCB TomCB - 1 year ago 76
Android Question

GreenDao Android Studio

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 now! Check it out! :-)

Any help would be appreciated!

Answer Source

Tested on Android Studio 1.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'])
  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");
            new DaoGenerator().generateAll(schema, "../DaoExample/src-gen");
        private static void addNote(Schema schema) {
            Entity note = schema.addEntity("Note");

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.