Simple Gradle build
I’m going to try to do the absolute bare minimum that can be considered a “Gradle build.” But even before that, I need to install Gradle.
Installation
Follow the instructions here to install Gradle. However, I decided to install Android Studio instead, because it’ll come in handy later on. (Android Studio comes with Gradle.)
Once you install either of those, add Gradle’s bin
folder to your system path, and run “gradle -v
” to make sure everything’s set up properly. Assuming OpenFL can compile for Android, you should have everything you need.
Sample project
I’m going to use DisplayingABitmap as my test project. I want to build it using OpenFL, but I don’t need to install it yet. The build process sets up an Android project, and that’s what I’m going to convert to Gradle.
> openfl create DisplayingABitmap
> cd DisplayingABitmap
> openfl build android
Once that finishes, the Android project will be located under DisplayingABitmap/Export/android/bin
.
You might wonder about the haxe
and obj
folders, but they aren’t part of the Android project; they’re used to create the Android project. If you look inside obj
, you’ll find C++ source files, plus one big C++ binary named libApplicationMain-v7.so
. This binary has already been copied into bin/libs/armeabi
, so you could delete it if you wanted. It might slow down your next build, but it wouldn’t stop this one.
Anyway, back to the bin
folder with the Android project. This is an Ant project, with an Ant build file: build.xml
. I could build it using Ant (which is what Lime does), but my goal right now is to build with Gradle.
The build
The quickest, easiest way to do this is to tell Gradle “import the existing Ant build file.” I’m going to make a file named build.gradle
containing one line of code:
ant.importBuild 'build.xml'
Now I check what tasks Gradle can perform:
> cd Export/android/bin
> gradle tasks
...
android_rules.clean - Removes output files created by other targets.
android_rules.debug - Builds the application and signs it with a debug key.
android_rules.emma
android_rules.help
android_rules.install - Installs the newly build package. Must be used in conjunction with a build target (debug/release/instrument). If the application was previously installed, the application is reinstalled if the signature matches.
...
First, I’m going to have it “clean” the project (this will only delete a few files; it won’t recompile all the C++ code like a clean OpenFL build does). Otherwise, Gradle will just reuse OpenFL’s build.
Next, I want to build and install a debug version. (No need to worry about release keys, which are a whole different can of worms.)
> gradle debug install
The output doesn’t include an “install success” message, but when I check my device…
Problems to work on
- No one wants to manually run the Gradle build every time.
- The
build.gradle
file is at risk of being deleted by openfl clean
.
- The Ant build is part of Lime’s build process, coming right after Lime finishes making the Android project. We need that Android project, but we’d prefer to skip the Ant build.