GreenDao的GitHub地址:
https://github.com/greenrobot/greenDAO
本文参考大神所写,增加了一些配图,让初学者更容易理解
http://blog.csdn.net/h3c4lenovo/article/details/43566169
新建gradle空白项目,项目结构如下:
-
新建”lib-greendao-generator”module。此library主要是生成Dao、Bean文件,所以主项目不必引用。并且它是一个纯java module。
-
新建Module
-
选择Java Library
-
起个名字,然后finish
-
目录结构如下
-
lib-greendao-generator/build.gradle配置
apply plugin: 'application'
apply plugin: 'java'
//设置主class,否则编译不通过
mainClassName = "com.example.GeneratorClass"
//设置自动生成类的目录,这个目录不是本lib的目录,而是另一个需要被主app引用的目录
def outputDir = "../lib-greendao/src/main/java-gen"
dependencies {
//添加generator工具引用
compile 'org.greenrobot:greendao-generator:2.2.0'
}
//自动创建文件夹
task createDocs {
def docs = file(outputDir)
docs.mkdirs()
}
//执行
run {
args outputDir
} -
MyClass.java相关代码
package com.example;
import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Schema;
/**
* 创建db类
* 生成操作数据库所需的bean和dao
*/
public class MyClass {
public static void main(String[] args) throws Exception {
//参数1是版本号
//参数2是生成文件的包
Schema schema = new Schema(1, "com.test.greendao");
addNewsDetail(schema);
try {
//后面的目录是在gradle中配置的路径
new DaoGenerator().generateAll(schema, "lib-greendao/src/main/java-gen");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建表
* 生成dao和bean
*
* @param schema
*/
private static void addNewsDetail(Schema schema) {
Entity newsDetail = schema.addEntity("TestBean");
newsDetail.setHasKeepSections(true);
newsDetail.addStringProperty("id").primaryKey().index();
newsDetail.addStringProperty("title");
newsDetail.addStringProperty("content");
}
}
-
-
新建”lib-greendao”Module
-
新建module
-
选择Android Library
-
输入名称,finish
-
-
目录结构如下
-
在app/build.gradle中配置如下
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.green.test"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
//引用被生成dao的library
compile project(':lib-greendao')
} -
在lib-greendao/build.gradle中配置如下
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets{
main {
//配置java代码路径
java.srcDirs = ['src/main/java', 'src/main/java-gen']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//引用greendao主包
compile 'org.greenrobot:greendao:2.2.0'
} -
运行MyClass就可以得到自动生成的类啦~~~
-
混淆
-dontwarn org.bouncycastle.crypto.**
-dontwarn freemarker.**
##自动生成类的包地址
-keep class com.test.greendao.**{*;}
-keep class de.greenrobot.daogenerator.** {*;}
-keepclassmembers class * extends de.greenrobot.dao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties