尝试为GreenDAO运行DaoGenerator时出现NoClassDefFoundError

时间:2017-03-16 19:17:55

标签: android sqlite android-studio greendao

我有一个Android项目,使用Android Studio 2.3,它使用GreenDAO生成与SQLite数据库交互的类。 DaoGenerator项目之前总是工作......但今天我只需要向实体添加2列/属性,每当我尝试运行生成器项目时,我都会收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/greenrobot/greendao/generator/Schema
    at com.company.daogenerator.ProjectDaoGenerator.main(ProjectDaoGenerator.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.greenrobot.greendao.generator.Schema
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

我在我的应用程序的Gradle文件中使用GreenDAO 3.2.0:

compile 'org.greenrobot:greendao:3.2.0'

另外,在DaoGenerator的Gradle文件中:

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'org.greenrobot:greendao-generator:3.2.0'
}

我的ProjectDaoGenerator.java文件:

package com.company.daogenerator;

import org.greenrobot.greendao.generator.DaoGenerator;
import org.greenrobot.greendao.generator.Entity;
import org.greenrobot.greendao.generator.Property;
import org.greenrobot.greendao.generator.Schema;

public class ProjectDaoGenerator {
    private static Entity primaryKeyEntity;
    private static Entity itemTypeEntity;

    public static void main(String args[]) throws Exception {
        Schema schema = new Schema(1, "com.company.project.datamodel");
        schema.enableKeepSectionsByDefault();

        // Define entities
        Entity primaryKey = schema.addEntity("CDPrimaryKey");
        Entity installation = schema.addEntity("CDInstallation");

        // Z_PRIMARYKEY
        primaryKeyEntity = primaryKey;
        primaryKey.setTableName("Z_PRIMARYKEY");
        primaryKey.addLongProperty("ENT").columnName("Z_ENT").primaryKey();
        primaryKey.addIntProperty("MAX").columnName("Z_MAX");
        primaryKey.addStringProperty("NAME").columnName("Z_NAME");
        primaryKey.addIntProperty("SUPER").columnName("Z_INT");

        // CDInstallation
        installation.setTableName("ZCDINSTALLATION");
        installation.addLongProperty("packageDate").columnName("ZPACKAGEDATE");

        (...) // Other Properties

        // **** Generate Schema ****
        new DaoGenerator().generateAll(schema, "app/src/main/java");
    }
}

它似乎无法找到org.greenrobot.greendao.generator.Schema

3 个答案:

答案 0 :(得分:14)

像这样为您的生成器设置build.gradle文件(特别注意mainClassName):

enter image description here

然后点击Android Studio右侧边栏中的“Gradle”标签,选择你的daogenerator的“运行”任务,如下所示:

enter image description here

它对我有用,更多细节检查链接:https://github.com/greenrobot/greenDAO/issues/619 http://greenrobot.org/greendao/documentation/generator/#Triggering_generation

答案 1 :(得分:2)

除了@Jesto Paul提到的内容之外,我在Generator类中更改了以下内容

  

new DaoGenerator()。generateAll(schema," ./ app / src / main / java"); - 显示路径   不存在。

  

new DaoGenerator()。generateAll(schema," ../ app / src / main / java");

(为路径添加了双点)。执行此操作后,生成器将表创建到文件夹。

答案 2 :(得分:1)

出于某种原因,我在更新android buildToolsVersion后遇到了同样的问题。

经过一段时间的搜索,我不小心检查了DaoGenerator-Application的“运行>编辑配置......”

在JRE列表中选择了“Android API 25 Platform”。所以我把它改回了我的计算机上运行的外部Java(例如“1.8”,在几天前做过)。这解决了我。

编辑:在这个项目中我使用GreenDAO 2.1.0

编辑2:

https://github.com/greenrobot/greenDAO/issues/619   - http://greenrobot.org/greendao/documentation/generator/#Triggering_generation