简体   繁体   English

用于ORMLite配置生成的Android Studio运行配置

[英]Android Studio run configuration for ORMLite config generation

I'm using Android Studio and want to use ORMLite framework. 我正在使用Android Studio,并且想使用ORMLite框架。 ORMLite for Android has a mechanism for making DAO creation through table config file . 适用于Android的ORMLite具有通过表配置文件进行DAO创建的机制。

How to setup additional Run Configuration in Android Studio for generating this config? 如何在Android Studio中设置其他运行配置以生成此配置?

I managed to do it, but it was a little bit tricky. 我设法做到了,但是有点棘手。

I have a class called DatabaseConfigUtil which extends OrmLiteConfigUtil, that I created just by following the ormlite official tutorial, so I'll just assume you did the same and also have that class. 我有一个名为DatabaseConfigUtil的类,该类扩展了OrmLiteConfigUtil,该类是按照ormlite官方教程创建的,因此我假设您也做同样的事情,并且也具有该类。 Please note that you have to pass the complete path to the configuration file, instead of just the file name. 请注意,您必须将完整路径传递给配置文件,而不仅仅是文件名。 Nonetheless, here it is: 尽管如此,这里是:

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);
  }
}

This is the class we want to execute in order to create the ormlite_config.txt. 这是我们要执行以创建ormlite_config.txt的类。

In the Android Studio project navigation panel, right-click on the DatabaseConfigUtil.java and select "Run" (the option with the green arrow). 在Android Studio项目导航面板中,右键单击DatabaseConfigUtil.java,然后选择“运行”(带有绿色箭头的选项)。 If you don't have a Run Configuration created, it will create one for you. 如果您没有创建运行配置,它将为您创建一个。

Now, just edit the configuration 现在,只需编辑配置

在此处输入图片说明

In the "Before launch" section, remove the Make. 在“启动前”部分中,删除品牌。 This is not problematic if in the raw folder you already have the file ormlite_config.txt, but if you don't, when you run the class, the project will compile which will fail because the ormlite_config.txt doesn't exist. 如果在原始文件夹中已经有文件ormlite_config.txt,这没有问题,但是如果没有,则在运行类时,项目将编译,因为ormlite_config.txt不存在,该项目将失败。

在此处输入图片说明

Now run the project again. 现在再次运行该项目。

Everything should go smoothly now. 现在一切应该顺利进行。

Cheers 干杯

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

UPDATE: 更新:

Recently I had to work with ORMLite again and decided that this solution could be automated with a gradle plugin. 最近,我不得不再次使用ORMLite,并决定可以使用gradle插件将该解决方案自动化。 Before creating my own, as the lazy developer that I am, I decided to check if anyone had attempted the same before. 在创建自己的自己之前,作为我的懒惰开发人员,我决定检查是否有人尝试过相同的尝试。 Thankfully, @snicolas did just that and you can find his plugin here . 幸运的是,@ snicolas做到了这一点,您可以在这里找到他的插件。 I've tried it, and it works reasonably well. 我已经尝试过了,并且效果很好。 It creates a task called createORMLiteConfigFile*Variant* that you can run to generate the file. 它创建一个名为createORMLiteConfigFile*Variant*的任务,您可以运行该任务来生成文件。

Collecting up all the comments under @Joao's answer gave me this working solution: 收集@Joao的回答下的所有评论给了我这个可行的解决方案:

1) Edit Configuration for your DB config file generator: 1)为您的数据库配置文件生成器编辑配置:

编辑配置

2) Configure the Working directory to be $MODULE_DIR$/src/main . 2)将Working directory配置为$MODULE_DIR$/src/main

3) In Before launch , replace Make with Make, no error check 3)在Before launch ,将Make替换为Make, no error check

工作目录

When you have these steps in place you can specify just the file name in your OrmLiteConfigUtil class: 完成这些步骤后,可以在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);
    }

I got the same issue OP got for the ClassNotFoundException. 我遇到了与ClassNotFoundException相同的问题。 The workaround for this is to temporarily to change the code to make the compiler to compile the project. 解决方法是临时更改代码以使编译器编译项目。

I had to remove the R.raw.ormlite_config value that I used in the DatabaseHelper class, which I am passing to the super(). 我必须删除在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);
    }

}

My DBHelper class that extends the extends OrmLiteSqliteOpenHelper needs to not use the raw folder yet. 扩展了OrmLiteSqliteOpenHelper扩展的DBHelper类不需要使用原始文件夹。 This helps in compiling the project successfully. 这有助于成功地编译项目。

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

在此处输入图片说明

  1. compiled the project. 编译了项目。
  2. change the working directory to the app/src/main folder. 将工作目录更改为app / src / main文件夹。
  3. Changed the JRE to JDK1.8 将JRE更改为JDK1.8
  4. Remove the Make from the Before launch section. 从启动前部分中删除品牌。
  5. Run

Ok I stumpled upon the same ClassNotFoundException like the OP. 好的,我遇到了与OP相同的ClassNotFoundException。 Here is how I solved it: 这是我解决的方法:

Short note: I have a library project and a main project and both are set up to with Gradle, that might be quite a difference as the previously called solution did not work for my setup. 简短说明:我有一个库项目和一个主项目,并且都通过Gradle进行了设置,这可能有很大的不同,因为先前调用的解决方案不适用于我的设置。

So my steps to do: 所以我要做的步骤:

  1. I created the DatabaseConfigUtil class 我创建了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. Note the class path I used in my documentation: It is basically based on the normal classpath you get when you try to run the above class (just copy it) 请注意我在文档中使用的类路径:它基本上基于您尝试运行上述类时获得的普通类路径(只需将其复制)

  3. Create a run configuration and add the classpath you just copied as VM options 创建运行配置并添加您刚刚复制的类路径作为VM options
  4. Make sure you have removed every entry that contains "build/classes/production/MODULENAME" 确保已删除所有包含“ build / classes / production / MODULENAME”的条目
  5. Add the complete path of your "build/classes/debug" to the classpath 将您的“ build / classes / debug”的完整路径添加到classpath
  6. Compile and run the configuration. 编译并运行配置。

You should see the output that the classes you defined in MODELS are created. 您应该看到创建在MODELS中定义的类的输出。

Side note: Joao Sousa said you should change the ORMlite source code. 旁注:Joao Sousa说您应该更改ORMlite源代码。 There is an easier method: The writeConfigFile(fileName) method seems to be broken in the new Gradle structure as it starts to look in the module root and goes up instead of going down to src/main/res/raw so I needed to use the other one where I could give a file object as parameter (see above code). 有一个更简单的方法:新的Gradle结构中的writeConfigFile(fileName)方法似乎已损坏,因为它开始在模块根目录中查找并向上而不是向下进入src/main/res/raw因此我需要使用另一个我可以给文件对象作为参数的地方(见上面的代码)。

Final note: As I try to make a lot of stuff on one press, I created a second run configuration that is called "PROJECTNAME FULL" which will do a "make" than runs the ormlite run configuration and finally a second "MAKE". 最后的注意事项:当我尝试一次按下很多东西时,我创建了第二个运行配置,称为“ PROJECTNAME FULL”,它将比运行ormlite运行配置执行“ make”操作,最后执行第二个“ MAKE”。
The first make compiles the sources so that the ormlite configuration can be created and the second "make" makes sure that the new created config file is added to the newly created build which will be installed then. 第一个make编译源代码,以便可以创建ormlite配置,第二个“ make”确保将新创建的配置文件添加到新创建的版本中,然后将其安装。 This "FULL" configuration doesn't need to run every time but at least once when you changed your model classes. 不必每次都运行“ FULL”配置,而是在更改模型类时至少运行一次。

I had trouble with this because my DB classes were defined in a Java project external to my android project. 我遇到了麻烦,因为我的数据库类是在我的android项目外部的Java项目中定义的。 But the OrmLiteConfigUtil is defined in the ormlite-android library, and must be built in the android project itself. 但是OrmLiteConfigUtil是在ormlite-android库中定义的,必须在android项目本身中构建。

It's OK, Gray plans ahead. 没关系,格雷会提前计划。 There's an overload of writeConfigFile that accepts File arguments specifying the search directory. 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);
  }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM