美文网首页
GreenDao3.2的配置

GreenDao3.2的配置

作者: 小和尚恋红尘 | 来源:发表于2017-08-17 17:35 被阅读0次

    直接进入主题,看看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, 示例图为:

    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:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
    <a href="syncProject">Re-download dependencies and sync project (requires network)</a></li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
    <a href="stopGradleDaemons">Stop Gradle build processes (requires restart)</a></li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
    

    图为:

    Paste_Image.png

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

    相关文章

      网友评论

          本文标题:GreenDao3.2的配置

          本文链接:https://www.haomeiwen.com/subject/idlnrxtx.html