Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Any help would be appreciated!

like image 277
TomCB Avatar asked Sep 17 '14 10:09

TomCB


People also ask

What is greenDAO?

greenDAO is a light & fast ORM for Android that maps objects to SQLite databases. Being highly optimized for Android, greenDAO offers great performance and consumes minimal memory. Home page, documentation, and support links: https://greenrobot.org/greendao/

What is Android ORM?

An ORM stands for Object Relational Mapping and will map database tables to entities. For a Persons table for example, a class Person will be created. You will then be able to edit the fields of the class and when you call a "save" method, the database table will be updated.

What is sugar ORM?

Sugar ORM is a database persistence library that provides a simple and concise way to integrate your application models into SQLite. In contrast to ActiveAndroid, which is mature, powerful, and flexible, Sugar ORM is: Less verbose. Quicker to set up.


1 Answers

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.

like image 164
nitesh goel Avatar answered Oct 13 '22 23:10

nitesh goel