NME is a great solution for writing applications for the Android platform, but sometimes it’s nice to escape the confines of GameActivity.java. This is fairly easy to do, but unfortunately until now it meant writing Java.
Which is fine for some people, but given the option I’d avoid it. (For one thing, it’s lead me to using two separate editors simultaneously: FlashDevelop and Notepad++.)
The convenience of using NME, along with the power and freedom that comes from using Java for Android, without ever having to leave HaXe, is one of the benefits of HaXe’s upcoming Java target. And for me, it’s a pretty major one.
There are three main steps. The first is writing the code, and the other two are the two-stage compilation process. I imagine most of the stuff I discuss will end up being automated nicely, but for now this is how I went about it.
I recently (~7 hours ago) got the opportunity to play with the new and upcoming java target for haxe (c/o @cwaneck), and it was okay.
Okay.. I was trying to down play it, but I can’t do it. It’s really cool!
I’m going to write down exactly what I had to do to go from haxe to java, from java to bytecode, and from bytecode to running. (Once I’d checked out the repository)
I made a simple SMS relay for my android (Andrew) last night. In case you want to do something similar, good news I’m about to explain its code!
Note: I used NME and notepad++ and flashdevelop and java, instead of NME and haXe, or Eclipse and Java. This might strike you as strange. It probably is strange, but it’s easier and more comfortable for me. The code shown will mostly be java and XML, which should match up nicely if you’re using Eclipse and Java, and you can take a look at Programming for android in Java but using NME if you’re using NME.
Let’s start with AndroidManifest.xml:
< ?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="::ANDROID_INSTALL_LOCATION::" android:versionCode="1" android:versionName="1.0" package="::APP_PACKAGE::">
<application android:label="::APP_TITLE::" android:debuggable="true"::if (HAS_ICON):: android:icon="@drawable/icon"::end::>
<receiver android:name=".TextMessageReceiver" >
<intent -filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<service android:name=".SMSService" />
<uses -sdk android:minSdkVersion="7"/>
<uses -permission android:name="android.permission.RECEIVE_SMS" />
<uses -permission android:name="android.permission.SEND_SMS" />
<uses -permission android:name="android.permission.WRITE_SMS" />
<uses -permission android:name="android.permission.READ_CONTACTS" />
I really like Haxe and NME, but someday I might want to try something that it’s default templates may not be well suited for.
NME’s main activity is defined by GameActivity.java, which is cool, but in some cases you may want to specify your own activity rather than creating an NME extension. This is something that you can totally do!
Depending on how you go about it, many of the haxe and nme libraries may no longer be safe to use. If your activity extends org.haxe.nme.GameActivity, I assume everything should be fine. If it skips it entirely on its way to android.app.Activity, I’d probably write the rest of my code in Java — just in case. I’m not really sure how the compiled c++/ndk stuff works and is tied in, with the exception of some JNI calls.