So here's the deal: I'm using ORMLite for Android, which uses annotations for it's mapping in Android. As you know, annotations are slow in Android, and the makers of ORMLite have realized this, so they added the ability to run a java executable to generate a resource file that bypasses the need to check annotations at runtime in the android app. It looks something like this:
public class DatabaseConfigUtil extends OrmLiteConfigUtil {
private static final Class<?>[] classes = new Class[] {
SimpleData.class,
};
public static void main(String[] args) throws Exception {
writeConfigFile("ormlite_config.txt", classes);
}
}
I need a way to run this java executable every once in a while. To sum this up: I need a way to run a java executable in Android Studio. It can be via Gradle, another run configuration, part of a JUnit test, I don't really care. I just need the ability to run this from AndroidStudio.
This is my current Gradle Script:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 18
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':AndroidLibrary')
compile 'com.j256.ormlite:ormlite-android:4.47'
}
Run the Java Application The gradle build uses the application plugin so we can run the application from the command line. The gradlew run command will be used to run the application from the command line. First, use the task command to display the added tasks by the plugin: gradlew tasks.
To run a java file in Android ensure your class has the main method. In Android Studio 3.5 just right click inside the file and select "Run 'Filename.
Gradle plug-ins One example is the Java plug-in. This plug-in adds tasks to your project which allow compiling Java source code, run unit tests and to create a JAR file. A plug-in is included in a build. gradle file with the apply plugin: 'pluginname' statement.
I use IDE configuration for this. Here is how to achieve it:
OrmLite DB config
, Main class: com.yourclasspath.DatabaseConfigUtil
, Use classpath of module: main
Before launch
press plus icon -> Run another configuration and select OrmLite DB configNow everytime you build your main configuration it also executes DatabaseConfigUtil.
If you dont want to run DatabaseConfigUtil before every build just skip step 4 and run it from configuration selection next to the Run icon in the toolbar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With