2013.06.19OpenFL simple extension (1)

Until recently I used Adobe tools to deploy my applications on iOS and Android.

Since openFL is gaining momentum and seemed to reach a decent maturity level I wanted to use it for my next project.

Using high level abstracting libraries to get the job done is nice. But sometimes the better way is to just call this documented but not binded Obj-C function. That's what Adobe ANEs are for and they work quite well.

So how can we get the same freedom with openFL? Here's my first little extension attempt.

Create the extension using openfl template:

$ openfl create extension TestExtension

$ cd TestExtension
$ ls -1
TestExtension.hx   # the haxe binding to your extension
haxelib.json       # haxelib will be able to register the extension
include.xml        # openfl will use this to link your extension to applications
ndll/              # will contain native libraries for each platform
project/           # the source code of your extension

Now we have to compile the extension for each supported platform. This will feed the ndll/ folder.

cd project/
haxelib run hxcpp Build.xml
haxelib run hxcpp Build.xml -Dandroid
haxelib run hxcpp Build.xml -Diphoneos -DHXCPP_ARMV7
haxelib run hxcpp Build.xml -Diphonesim

Now let's register the extension with haxelib:

cd ..
haxelib dev TestExtension `pwd`

To ensure that my extension is created a quick test:

mkdir TestApp
cd TestApp

project.xml:

<?xml version="1.0" encoding="utf-8"?>
<project>
	<meta
        title="TestApp"
        package="me.labe.testapp"
        version="1.0.0"
        company="Laurent Bedubourg"
        />
	<app
        main="Main"
        path="Export"
        file="TestApp"
        />
	<source path="." />
	<haxelib name="openfl" />
  <haxelib name="TestExtension" />
</project>

Main.hx:

class Main {
    public static function main(){
        var t = new flash.text.TextField();
        t.text = Std.string(TestExtension.sampleMethod(16));
        flash.Lib.current.addChild(t);
    }
}

Compile and test on different platforms:

openfl test project.xml cpp
openfl test project.xml android
openfl test project.xml ios -simulator
openfl test project.xml ios

Worked like a charm on Mac and my Android tablet!

I still have to learn how to link external libraries and frameworks and how to organize custom code for each platform but it's definitely a good start: it's easier than creating ANE's for Adobe Air.

I had linkage errors with iOS, I'll update this post when I'll find what's going wrong and how to do it the right way.

And here's a GitHub repository for this simple extensionAnd here's the original post of Joshua about the subject

EDIT: It looks like my iOS problem is coming from an uppercase/lowercase inconsistency between platforms. Until this small bug is resolved you should be safe to experiment with lowercased extension names (as far as iOS is concerned).

EDIT: Definitely an uppercase/lowercase problem, here's the issue report so future readers will know when it is corrected.

EDIT: Joshua is cleaning up the extension template so everything will work out of the box really quickly :)