1.配置项目下的build
buildscript { repositories { mavenCentral() } dependencies { classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0") classpath ("org.greenrobot:greendao-gradle-plugin:3.3.1") // 使用最新版本 } }
2.配置app下的build
id("org.jetbrains.kotlin.kapt") id("org.greenrobot.greendao")
kapt { arguments { arg("daoPackage", "你的ApplicatinId") arg("databaseName", "你的数据库名称-db") arg("targetGenDir", "/src/main/java") arg("schemaVersion", "1") } }
implementation("org.greenrobot:greendao:3.2.2") kapt("org.greenrobot:greendao-generator:3.2.2")
3.初始化数据库
class App : Application() { private var myOpenHelper: MyOpenHelper? = null private var db: SQLiteDatabase? = null private var mDaoMaster: DaoMaster? = null private var mDaoSession: DaoSession? = null override fun onCreate() { super.onCreate() setDatabase() } private fun setDatabase() { myOpenHelper = MyOpenHelper(this, "你的数据库名称-db", null) db = myOpenHelper!!.writableDatabase mDaoMaster = DaoMaster(db) mDaoSession = mDaoMaster!!.newSession() } fun getmDaoSession(): DaoSession? { mDaoSession = mDaoMaster!!.newSession() return mDaoSession } inner class MyOpenHelper(context: Context?, name: String?, factory: CursorFactory?) : DaoMaster.OpenHelper(context, name, factory) }
4.实体类
@Entity public class TypeEntity { @Id(autoincrement = true) private Long id; private int color; private String type; }
5.实例类写好之后锤子🔨锤一下就自动生成后续代码了 【快捷键忘了,如果有记得的同学,欢迎留言Thanks♪(・ω・)ノ 】。