I had the same problem and searched the web and the documentation of GreenDAO but didn't find anything reliable.
So I wrote a code to run in the first run of the app. To do so I needed to check if it's the first time that my app is launched. For doing that I recommend this answer. You can see the code from that answer here:
public static void checkFirstRun(Context context) {
final String PREFS_NAME = "TickTockPrefs";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
// Get current version code
int currentVersionCode = 0;
try {
currentVersionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
// handle exception
e.printStackTrace();
return;
}
// Get saved version code
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
// Check for first run or upgrade
if (currentVersionCode == savedVersionCode) {
// This is just a normal run
return;
} else if (savedVersionCode == DOESNT_EXIST) {
// TODO This is a new install (or the user cleared the shared preferences)
seed(context);
} else if (currentVersionCode > savedVersionCode) {
// TODO This is an upgrade
}
// Update the shared preferences with the current version code
prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
And inside the seed method you can write whatever you want to insert. For example say I have a "Person" entity that I want to prepopulate with data:
public static void seed(Context context) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "your-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
Person person = new Person();
person.setName("Jason");
person.setFamily("Bourne");
PersonDao personDao = daoSession.getPersonDao();
personDao.insert(person);
}
Note that if you want to insert a List of entities use insertInTx() method instead of insert(). You can see the difference here.
I know this is different than ORM seed method but it seems there's no other alternatives except you manipulate greenDAO code yourself.