Android Studio run configuration for ORMLite config generation

14,384

Solution 1

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. 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.

In the Android Studio project navigation panel, right-click on the DatabaseConfigUtil.java and select "Run" (the option with the green arrow). If you don't have a Run Configuration created, it will create one for you.

Now, just edit the configuration

enter image description here

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.

enter image description here

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. 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. I've tried it, and it works reasonably well. It creates a task called createORMLiteConfigFile*Variant* that you can run to generate the file.

Solution 2

Collecting up all the comments under @Joao's answer gave me this working solution:

1) Edit Configuration for your DB config file generator:

edit configuration

2) Configure the Working directory to be $MODULE_DIR$/src/main.

3) In Before launch, replace Make with Make, no error check

working directory

When you have these steps in place you can specify just the file name in your OrmLiteConfigUtil class:

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

Solution 3

I got the same issue OP got for the 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().

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. This helps in compiling the project successfully.

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

enter image description here

  1. compiled the project.
  2. change the working directory to the app/src/main folder.
  3. Changed the JRE to JDK1.8
  4. Remove the Make from the Before launch section.
  5. Run

Solution 4

Ok I stumpled upon the same ClassNotFoundException like the OP. 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.

So my steps to do:

  1. I created the DatabaseConfigUtil class

    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
  4. Make sure you have removed every entry that contains "build/classes/production/MODULENAME"
  5. Add the complete path of your "build/classes/debug" to the classpath
  6. Compile and run the configuration.

You should see the output that the classes you defined in MODELS are created.

Side note: Joao Sousa said you should change the ORMlite source code. 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).

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".
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. This "FULL" configuration doesn't need to run every time but at least once when you changed your model classes.

Solution 5

I had trouble with this because my DB classes were defined in a Java project external to my android project. But the OrmLiteConfigUtil is defined in the ormlite-android library, and must be built in the android project itself.

It's OK, Gray plans ahead. There's an overload of writeConfigFile that accepts File arguments specifying the search directory.

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);
  }
}
Share:
14,384
Stanislav Borzenko
Author by

Stanislav Borzenko

Updated on June 15, 2022

Comments

  • Stanislav Borzenko
    Stanislav Borzenko about 2 years

    I'm using Android Studio and want to use ORMLite framework. ORMLite for Android has a mechanism for making DAO creation through table config file.

    How to setup additional Run Configuration in Android Studio for generating this config?