Java Native Extension Fundamentals for AIR

Hello dear habrachitateli. My story, as the name of the topic shows, will go about the basis and features of creating an adobe native extension (hereinafter ANE) for Adobe AIR, in my case I will write extensions in java, to expand the functionality of AIR, on the Android platform. And so with your permission we begin.

Introduction


What is ANE? This is a tool that has appeared since Adobe AIR 3.0 and higher, it allows you to extract and execute pieces of native code from the AIR environment, conditionally we can execute any native code. The extension looks like a compiled library file with the .ane extension.


What you need for work


To get started, we need:
1. As an Eclipse IDE.
2. Flash Builder 4.6 plugin
3. ADT plugin.
4. Adobe AIR SDK.

I won’t explain how to run all this and write code because this topic goes beyond the scope of this article, by default, the article assumes that you own all of these subtleties ..

About JAVA extension programming.


To begin with, let's create an Android project, after marking it as a library and most importantly, do not forget the name of the package, this name will be described in the extension description file a bit later.
The main extension class essentially implements the FREExtension interface ... this interface, like many other classes, comes with a separate library from adobe, where can we find this library? It can be found inside the AIR SDK archive at lib / android /. Libraries are called FlashRuntimeExtensions with extensions jar and so. We write in java, therefore we need a jar, take our library and connect it to our project. And so let's create the main class of our extension, it should have the following structure:

@Override
public FREContext createContext(String arg0) {
	return null;
}
@Override
public void initialize() {
}
@Override
public void dispose() {
}


As you can see, the structure of our class is pretty simple, the three required methods, with the dispose and initialize methods, I think and so everything is clearly called when the extension is initialized and destroyed. The createContext method is interesting. Conditionally, an extension has its own context that will describe the functionality of our extension, as we understand the class of our future context, it should extend the FREContext class. Let's create a class in our context, the class should have the following structure:

@Override
public Map getFunctions() {
	return null;
}	
@Override
public void dispose() {
}


Here you should pay attention to the getFunctions () method. At its core, this method returns maps, the key of which is the name of the method that we use when calling from as3, and the value of the function that implements the FREFunction interface, which contains all the functionality of the called method of our extension. The function class must have the following structure:

@Override
public FREObject call(FREContext context, FREObject[] args) {		
	return null;
}


As we see here, we have only one mandatory method. Access to the activity of the current application can be obtained through an instance of the FREContext class, which we get as a parameter in our function, by the quite logical getActivity () method and already having a native Androyd activity, you can do whatever you like with it. Also, an instance of the FREContext class has a dispatchStatusEventAsync method that allows you to make a callback in as3, the results of which can be heard with the StatusEvent.STATUS event. The second parameter of the method is an array of FREObject classes, these are all the parameters that will be passed to our context from as3. An instance of the FREObject class has a set of methods for converting to the corresponding data type.
This ends with the java part and move on to as3.

About extension programming on AS3.


To create a library, let's create a Flex library project, at the same time we will not forget to import AIR classes, and also the class package should be identical to the same as in java.
The main class on as3 can extend anything, just create a class and create an instance of the ExtensionContext class in it, it is created as follows:

var extContext:ExtensionContext = ExtensionContext.createExtensionContext("имя пакета расширения", "тип контекста");


now we have an instance of the context of our extension, you can listen to it as we wrote above, as follows
extContext.addEventListener(StatusEvent.STATUS, onStatus);


You can also call the method that we described in context in java as follows:
extContext.call("имя вызываемого метода", ..."массив параметров передаваемый функции расширения)


It's pretty simple, let's now try to collect our ane.

Build ANE.


And so, after all our efforts, we should have swc from our flex project and jar from our android project.
Let's create a separate directory, and organize the following hierarchy inside it:



Inside the android folder, our jar library should lie, in case you are developing ane for ios or something like that, the directory should correspond to its name. Another important thing before compiling is to extract the library.swf file from the swc library (open your swc file with the help of the winrar archiver for example) and drop it into each directory. Approximately, the android directory will have two files and look like this:



And the default directory will contain only one library.swf file.
Now we need to describe our extension in the descriptor is the extension.xml file, in our case it will look something like this:
пакет расширенияверсия расширенияtest.jarполный путь вместе с пакетом к java классу расширения


Well, now everything is almost ready for us, it remains just to assemble our library, for this we execute the following program from the console (do not forget to specify the path to the Adobe AIR binaries in the environment variables):

adt -package -target ane extension.ane extension.xml -swc test.swc -platform Android-ARM -C android . -platform default -C default .


Where extension.ane is our ane to be created, and test.swc is our flex library. As a result, you will get ane file that you can use.

On this on this, probably I will end with the basics. Thank you for your time and patience, and if anyone is interested, wait for the continuation.

Also popular now: