Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio run configuration for ORMLite config generation

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?

like image 873
Stanislav Borzenko Avatar asked Jun 25 '13 13:06

Stanislav Borzenko


People also ask

How to Open configuration in Android Studio?

Note: An easy way to locate your configuration directory is to navigate to Help > Edit Custom VM Options in Android Studio. This will open a configuration file.


2 Answers

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.

like image 141
Joao Sousa Avatar answered Oct 08 '22 17:10

Joao Sousa


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);     } 
like image 38
Dan J Avatar answered Oct 08 '22 18:10

Dan J