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?
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?
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
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.
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.
Collecting up all the comments under @Joao's answer gave me this working solution:
1) Edit Configuration for your DB config file generator:
2) Configure the Working directory
to be $MODULE_DIR$/src/main
.
3) In Before launch
, replace Make
with Make, no error check
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);
}
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
}
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:
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);
}
}
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)
VM options
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.
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);
}
}
change the "working directory" to /src/main of the project in "Edit Configurations"