【问题标题】:Android Studio run configuration for ORMLite config generation用于 ORMLite 配置生成的 Android Studio 运行配置
【发布时间】:2013-06-25 13:32:40
【问题描述】:

我正在使用 Android Studio 并想使用 ORMLite 框架。 ORMLite for Android 有一个通过table config file 创建DAO 的机制。

如何在 Android Studio 中设置额外的运行配置以生成此配置?

【问题讨论】:

标签: android android-studio ormlite


【解决方案1】:

我设法做到了,但有点棘手。

我有一个名为 DatabaseConfigUtil 的类,它扩展了 OrmLiteConfigUtil,我只是按照 ormlite 官方教程创建的,所以我假设你也做了同样的事情并且也有这个类。 请注意,您必须传递配置文件的完整路径,而不仅仅是文件名。 不过,这里是:

public class DatabaseConfigUtil extends OrmLiteConfigUtil {
  private static final Class<?>[] classes = new Class[] {
    Class1.class, Class2.class, Class3.class, Class4.class
  };

  public static void main(String[] args) throws SQLException, IOException {
    writeConfigFile(new File("PATH/TO/ANDROID/PROJECT/src/main/res/raw/ormlite_config.txt"), classes);
  }
}

这是我们要执行的类以创建 ormlite_config.txt。

在 Android Studio 项目导航面板中,右键单击 DatabaseConfigUtil.java 并选择“运行”(带有绿色箭头的选项)。如果您没有创建运行配置,它会为您创建一个。

现在,只需编辑配置

在“发布前”部分中,删除 Make。如果在原始文件夹中你已经有文件 ormlite_config.txt 这没有问题,但如果你没有,当你运行这个类时,项目将编译失败,因为 ormlite_config.txt 不存在。

现在再次运行项目。

现在应该一切顺利。

干杯

-------------- ## ----- ------------

更新:

最近我不得不再次使用 ORMLite,并决定可以使用 gradle 插件自动化这个解决方案。在创建自己的开发人员之前,作为懒惰的开发人员,我决定检查是否有人之前尝试过相同的操作。值得庆幸的是,@snicolas 就是这样做的,您可以找到他的插件 here。我已经尝试过了,而且效果还不错。它会创建一个名为 createORMLiteConfigFile*Variant* 的任务,您可以运行该任务来生成文件。

【讨论】:

  • 感谢您的回答,但我还有一个问题。当我运行 DatabaseConfigUtil 配置 ClassNotFoundException 发生:Exception in thread "main" java.lang.ClassNotFoundException: my.package.DatabaseConfigUtil
  • 在运行之前,请尝试编译。也许它与类路径有关。您可以检查是否在运行配置中显示“使用模块的类路径”,选择了正确的项目,这应该是您的 android 项目。不过,如果您已经有一个运行配置,我建议您删除现有的运行配置并再次运行。
  • 请注意,如果你只想使用writeConfigFile("ormlite_config.txt");,你应该将Edit Configurations中的Working Directory设置为&lt;YOUR-WORKING-DIR&gt;/src/main。 ORMLite 可以自动找到原始目录。
  • 我做了以下调整以使其工作:1)用“Make,没有错误检查”替换“Make”和2)指定正确的WORKDIR(即android项目的根目录)谢谢João!
  • 要扩展@ZerayRice 的建议,您可以使用Working Directory 字段中的$MODULE_DIR$ 变量来实现此跨平台并将此运行配置放入源代码控制:$MODULE_DIR$/src/main
【解决方案2】:

在@Joao 的回答下收集所有 cmets 给了我这个可行的解决方案:

1) 编辑数据库配置文件生成器的配置:

2) 将Working directory 配置为$MODULE_DIR$/src/main

3) 在Before launch 中,将Make 替换为Make, no error check

当您完成这些步骤后,您可以只在 OrmLiteConfigUtil 类中指定文件名:

public class DBConfigUtil extends OrmLiteConfigUtil {

    /**
     * To make this work in Android Studio, you may need to update your
     * Run Configuration as explained here:
     *   http://stackoverflow.com/a/17332546
     */
    public static void main(String[] args) throws Exception {
        writeConfigFile("ormlite_config.txt", sClasses);
    }

【讨论】:

  • 感谢这对我帮助很大。对于任何想知道的人,sClasses 是您已注释的类,例如:private static final Class>[] classes = new Class[] {Workout.class};。在运行配置之前,您还必须在 res 文件夹下创建 raw 文件夹,并且一旦成功,您可以在此处添加配置文件: RoboSpiceDatabaseHelper databaseHelper = new RoboSpiceDatabaseHelper( application, "sample_database.db", null, 3 , R.raw.ormlite_config);
  • 这很到位,简单,符合 ORMLite 的官方文档,但更友好。
【解决方案3】:

我遇到了与 ClassNotFoundException 相同的问题。解决方法是临时更改代码以使编译器编译项目。

我必须删除我在 DatabaseHelper 类中使用的 R.raw.ormlite_config 值,我将其传递给 super()。

public class DBConfigUtil extends OrmLiteConfigUtil {
private static final Class<?>[] classes = new Class[] {Workout.class};

    public static void main(String[] args) throws IOException, SQLException {
        writeConfigFile("ormlite_config.txt",classes);
    }

}

我的扩展了扩展 OrmLiteSqliteOpenHelper 的 DBHelper 类还需要不使用原始文件夹。这有助于成功编译项目。

public DBHelper(Context context){
    super(context,DATABASE_NAME,null,DATABASE_VERSION,1);// R.raw.ormlite_config
}

  1. 编译项目。
  2. 将工作目录更改为 app/src/main 文件夹。
  3. 将 JRE 更改为 JDK1.8
  4. 从“启动前”部分中删除 Make。
  5. 运行

【讨论】:

    【解决方案4】:

    好吧,我偶然发现了和 OP 一样的 ClassNotFoundException。以下是我的解决方法:

    简短说明:我有一个库项目和一个主项目,并且都使用 Gradle 进行设置,这可能会有很大的不同,因为之前调用的解决方案不适用于我的设置。

    所以我的步骤是:

    1. 我创建了 DatabaseConfigUtil 类

      public class DatabaseConfigUtil extends OrmLiteConfigUtil {
          public static final Class<?>[] MODELS = {Character.class, Party.class, Clazz.class};
      
          /**
           * This must be called as a stand alone app by a JRE instance and NOT by android.
           * It will create an ormlite config file that will make the reflection for annotation and more easier and faster.
           * <p/>
           * Make sure you have pathOfProject/build/classes/debug in your class path when running!
           * <p/>
           * Working class path:
           * <code>-classpath /usr/lib/jvm/java-7-oracle/lib/jconsole.jar:/usr/lib/jvm/java-7-oracle/lib/dt.jar:/usr/lib/jvm/java-7-oracle/lib/sa-jdi.jar:/usr/lib/jvm/java-7-oracle/lib/tools.jar:/usr/lib/jvm/java-7-oracle/lib/javafx-doclet.jar:/usr/lib/jvm/java-7-oracle/lib/ant-javafx.jar:/usr/lib/jvm/java-7-oracle/lib/javafx-mx.jar:/home/martin/workspace/idea/Project/MainProject/libs/ormlite-android-4.45.jar:/home/martin/workspace/idea/Project/MainProject/libs/ormlite-core-4.45.jar:/opt/android-studio/lib/idea_rt.jar:/home/martin/workspace/idea/Project/MainProject/build/classes/debug:/opt/android/platforms/android-16</code>
           *
           * @param args none will be used.
           * @throws Exception
           */
          public static void main(String[] args) throws Exception {
              writeConfigFile(new File("MODULENAME/src/main/res/raw/ormlite_config.txt"), MODELS);
          }
      }
      
    2. 请注意我在文档中使用的类路径:它基本上基于您尝试运行上述类时获得的正常类路径(只需复制它)

    3. 创建一个运行配置并将刚才复制的类路径添加为VM options
    4. 确保您已删除所有包含“build/classes/production/MODULENAME”的条目
    5. 将“build/classes/debug”的完整路径添加到类路径中
    6. 编译并运行配置。

    您应该会看到您在MODELS 中定义的类已创建的输出。

    旁注:Joao Sousa 说您应该更改 ORMlite 源代码。有一个更简单的方法:writeConfigFile(fileName) 方法似乎在新的 Gradle 结构中被破坏,因为它开始在模块根目录中查找并上升而不是下降到 src/main/res/raw 所以我需要使用另一个我可以给一个文件对象作为参数(见上面的代码)。

    最后说明:当我尝试在一次印刷机上制作很多东西时,我创建了称为“PROJECTNAME FULL”的第二个运行配置,它将执行“make”而不是运行 ormlite 运行配置,最后是第二个“制作”。
    第一个 make 编译源以便可以创建 ormlite 配置,第二个“make”确保将新创建的配置文件添加到将安装的新创建的构建中。这种“完整”配置不需要每次都运行,但至少在您更改模型类时运行一次。

    【讨论】:

    • 关于使用替代方法 writeConfigFile(File configFile, Class>[] classes) 傻我:D 这个解决方案更“性感”
    • 嗯,每个人都会不时发生这种情况:D
    【解决方案5】:

    我遇到了麻烦,因为我的 DB 类是在我的 android 项目外部的 Java 项目中定义的。但是 OrmLiteConfigUtil 是在 ormlite-android 库中定义的,必须在 android 项目本身中构建。

    没关系,格雷提前计划。有一个 writeConfigFile 重载,它接受指定搜索目录的 File 参数。

    public class DatabaseConfigUtil extends OrmLiteConfigUtil {
      public static void main(String[] args) throws Exception {
          File conffile = new File("app/src/main/res/raw/ormlite_config.txt");
          File searchdir = new File("../jclip/tdb/src/main/java/");
          writeConfigFile(conffile, searchdir);
      }
    }
    

    【讨论】:

      【解决方案6】:

      在“编辑配置”中将“工作目录”更改为项目的/src/main

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-07
        • 2016-11-22
        • 2021-08-26
        • 2019-10-12
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        相关资源
        最近更新 更多