domingo, 1 de fevereiro de 2015

Porting the StackerJavaFX game to Android

For too long I want to port a JavaFX application of mine to Android using JavaFX Ports. The day has come! In this post I will share with you my experiences porting the Stacker Game to Android. Actually at the time I write this sentence I just created a new project on Netbeans and it might even not work, I can even abandon this post, because at the moment I have 0% of the port done! UPDATE: It worked and was really easy!

From Java 8 to Java 7


Android doesn't support Java 8. We have projects to use Lambdas in Java 7, but I wanted to make it simple as possible to avoid any incompatibility, hence I decided to keep it only with Java 7 and JavaFX 2.2.
The saddest thing was to remove my beautiful lambdas to use the old anonymous classes from the code. Also, I noticed that JavaFX 8 is easier than JavaFX 2.2. The reason is that we have more constants and constructors in JavaFX 8 that we won't find in the 2.2 version. There were simple details to change, but Netbeans helped me, what I did was:

* Create a new JavaFX prject in Netbeans, but this time using Java 7;
* Copied the source from the JavaFX 8 project and pasted into the Java 7 project;
* Corrected the errors pointed by Netbeans.

I finished this in about 25 minutes. The main problem is that I ran into this Netbeans bug, but quickly solved by running it with Java 8, but compiling with Java 7.
I could continue programming in Netbeans, but as this was a first test, I deleted netbeans related files and created a gradle project.

First Port to Android


I carefully followed the instruction from the JavaFX ports site using the new build system, but I faced an issue with "Where Should I put my JAR?". This thread answered my question. The I noticed that I had to configure my project structure as a Gradle project. It worked very well!



After you configure your gradle application and correctly install Android tools, you just need to run too commands to have your application running in your phone:

$ gradle build
$ gradle installDebug

The harder part was to configure my phone to run in developer mode and then use the adb tool to configure the USB access.

Conclusion

I was really easy to port the JavaFX application to my Android phone and I am still impressed with with the performance while the app was running on the phone. Of course I would have to make a few adjustments if I want the app to be widely available, such as the size when the screen rotates...

* The source of the application ready to run on Android can be found on my github;
* A direct link to the APK can be found here.

Nenhum comentário:

Postar um comentário