GreenDao3.2的配置

直接进入主题,看看GreenDao在AndroidStudio中的配置以及简单的使用
在工程build.gradle中的配置,代码如下

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
注意:

在工程build.gradle中只是添加了classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'

app下的build.gradle中的配置,代码如下

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.tbwy.greendao"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

//greendao配置
greendao{
    //版本号,升级时可配置,对应当前数据库版本
    schemaVersion 1
    //由GreenDao自动生成代码所在的包名,默认的是在项目包下面新建一个gen
    daoPackage 'com.tbwy.greendao.greendao.gen'
    //设置自动生成代码的目录
    targetGenDir 'src/main/java'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'org.greenrobot:greendao:3.2.2'
}
注意:

app下的build.gradle中添加了三处代码:
1,apply plugin: 'org.greenrobot.greendao'
2,增加greendao配置

//greendao配置
greendao{
    //版本号,升级时可配置,对应当前数据库版本
    schemaVersion 1
    //由GreenDao自动生成代码所在的包名,默认的是在项目包下面新建一个gen
    daoPackage 'com.tbwy.greendao.greendao.gen'
    //设置自动生成代码的目录
    targetGenDir 'src/main/java'
}

3,添加greendao依赖compile 'org.greenrobot:greendao:3.2.2'

项目更新完成后,我们就需要来写一个实体类了,就是要往数据库中存储的数据类。它是建立在包名下的,也就是com.tbwy.greendao下,以“书”为例

@Entity
public class Book {
    @Id
    private Long bookId;
    private String bookName;
    private String bookAutho;
}

写成这样就可以了,然后一次点击菜单Build-->Make Project,运行完成后;在此打开我们建立的实体类Book

@Entity
public class Book {
    @Id
    private Long bookId;
    private String bookName;
    private String bookAutho;
    @Generated(hash = 619701788)
    public Book(Long bookId, String bookName, String bookAutho) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookAutho = bookAutho;
    }
    @Generated(hash = 1839243756)
    public Book() {
    }
    public Long getBookId() {
        return this.bookId;
    }
    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }
    public String getBookName() {
        return this.bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public String getBookAutho() {
        return this.bookAutho;
    }
    public void setBookAutho(String bookAutho) {
        this.bookAutho = bookAutho;
    }
}

可以看出系统给自动生成了一些代码,用于获取和设置值
而且在com.tbwy.greendao.greendao.gen目录下,自动生成了三个类BookDao,DaoMaster,DaoSession, 示例图为:

GreenDao3.2的配置_第1张图片
Paste_Image.png

这样GreenDao的适配工作就完成了。

配置过程中遇到的问题:

Error:Unable to find method 'org.gradle.api.tasks.TaskInputs.file(Ljava/lang/Object;)Lorg/gradle/api/tasks/TaskInputFilePropertyBuilder;'.
Possible causes for this unexpected error include:In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

图为:

GreenDao3.2的配置_第2张图片
Paste_Image.png

我造成这个问题的原因是因为gradle的版本低了,我换了一个gradle-3.5的版本后,问题得以解决。

你可能感兴趣的:(GreenDao3.2的配置)