The environment is not described in detail here.
1. Write the Android local library
1. Extract the required jar package from the Flash Air SDK
The jar package required for Android is FlashRuntimeExtensions. jar in the Flash Air SDK/lib/android directory.
2. Create an Android project and add the jar package extracted in step 1
3. Write Extension, Context, and Function
Customize the Extension class and implement the FREExtension Interface
Custom Context class, inheriting FREContext class
Customize the Function class to implement the FREFunction Interface
Multiple Function classes can be defined, and are registered in the getFunctions method of the custom Context class.
Sample:
[Java]
Public class TestExtension implements FREExtension {
@ Override
Public FREContext createContext (String arg0 ){
// TODO Auto-generated method stub
Return new TestContext ();
}
@ Override
Public void dispose (){
// TODO Auto-generated method stub
}
@ Override
Public void initialize (){
// TODO Auto-generated method stub
}
}
Public class TestExtension implements FREExtension {
@ Override
Public FREContext createContext (String arg0 ){
// TODO Auto-generated method stub
Return new TestContext ();
}
@ Override
Public void dispose (){
// TODO Auto-generated method stub
}
@ Override
Public void initialize (){
// TODO Auto-generated method stub
}
}
[Java]
Public class TestContext extends FREContext {
@ Override
Public void dispose (){
// TODO Auto-generated method stub
}
@ Override
Public Map <String, FREFunction> getFunctions (){
HashMap <String, FREFunction> map = new HashMap <String, FREFunction> ();
Map. put ("testToast", new TestToastFunction ());
// You can continue to put other functions.
Return map;
}
}
Public class TestContext extends FREContext {
@ Override
Public void dispose (){
// TODO Auto-generated method stub
}
@ Override
Public Map <String, FREFunction> getFunctions (){
HashMap <String, FREFunction> map = new HashMap <String, FREFunction> ();
Map. put ("testToast", new TestToastFunction ());
// You can continue to put other functions.
Return map;
}
} [Java]
Public class TestToastFunction implements FREFunction {
@ Override
Public FREObject call (FREContext arg0, FREObject [] arg1 ){
FREObject msg _ = arg1 [0];
FREObject result = null;
Try {
Toast. makeText (arg0.getActivity (), "test successful: android local call" + msg _. getAsString (), Toast. LENGTH_LONG). show ();
Result = FREObject. newObject ("this is a value that can be returned ");
} Catch (Exception e ){
E. printStackTrace ();
}
Return result;
}
}
Public class TestToastFunction implements FREFunction {
@ Override
Public FREObject call (FREContext arg0, FREObject [] arg1 ){
FREObject msg _ = arg1 [0];
FREObject result = null;
Try {
Toast. makeText (arg0.getActivity (), "test successful: android local call" + msg _. getAsString (), Toast. LENGTH_LONG). show ();
Result = FREObject. newObject ("this is a value that can be returned ");
} Catch (Exception e ){
E. printStackTrace ();
}
Return result;
}
}
4. Export the project you just compiled in the form of a jar package and name the jar package (for example, HelloANENative. jar)
II. AS end
1. Create a Flex library project and select the Mobile Library Configuration
2. Create the extension. xml file under the src directory of the project. The basic content of the file is as follows (view more attributes ):
[Plain]
<Extension xmlns = "http://ns.adobe.com/air/extension/3.3">
<Id> com. adobe. ane. test </id>
<VersionNumber> 1 </versionNumber>
<Platforms>
<Platform name = "Android-ARM">
<ApplicationDeployment>
<NativeLibrary> HelloANENative. jar </nativeLibrary> <! -- Is the previously exported jar package -->
<! -- Full path of local FREExtension for Android -->
<SPAN style = "WHITE-SPACE: pre"> </SPAN> <initializer> com.cn. example. android. TestExtension </initializer>
<SPAN style = "WHITE-SPACE: pre"> </SPAN> <finalizer> com.cn. example. android. TestExtension </finalizer>
</ApplicationDeployment>
</Platform>
</Platforms>
</Extension>
<Extension xmlns = "http://ns.adobe.com/air/extension/3.3">
<Id> com. adobe. ane. test </id>
<VersionNumber> 1 </versionNumber>
<Platforms>
<Platform name = "Android-ARM">
<ApplicationDeployment>
<NativeLibrary> HelloANENative. jar </nativeLibrary> <! -- Is the previously exported jar package -->
<! -- Full path of local FREExtension for Android -->
<Initializer> com.cn. example. android. TestExtension </initializer>
<Finalizer> com.cn. example. android. TestExtension </finalizer>
</ApplicationDeployment>
</Platform>
</Platforms>
</Extension>
3. Compile an AS Extension class. The main function is to create ExtensionContext and interact with Native code of Android (we recommend that you use a singleton)
[Plain]
Package com. adobe. nativeExtensions. test
{
Import flash. external. ExtensionContext;
Public class HelloWorldExtension
{
Public static const KEY: String = "testToast"; // consistent with the key in Map in java
Public static const EXTENSION_ID: String = "com. adobe. ane. test"; // consistent with the id tag in extension. xml
Private var extContext: ExtensionContext;
Public function HelloWorldExtension ()
{
// The second parameter is the createContext method of FREExtension in java code.
ExtContext = ExtensionContext. createExtensionContext (EXTENSION_ID ,"");
}
Public function hello (name: String): String {
If (extContext ){
Return extContext. call (KEY, name) as String;
}
Return "call failed ";
}
}
}
Package com. adobe. nativeExtensions. test
{
Import flash. external. ExtensionContext;
Public class HelloWorldExtension
{
Public static const KEY: String = "testToast"; // consistent with the key in Map in java
Public static const EXTENSION_ID: String = "com. adobe. ane. test"; // consistent with the id tag in extension. xml
Private var extContext: ExtensionContext;
Public function HelloWorldExtension ()
{
// The second parameter is the createContext method of FREExtension in java code.
ExtContext = ExtensionContext. createExtensionContext (EXTENSION_ID ,"");
}
Public function hello (name: String): String {
If (extContext ){
Return extContext. call (KEY, name) as String;
}
Return "call failed ";
}
}
}
Compile the. SWF file (for example, HelloANELibrary. swc)
In this step, all the files required for packaging ane are available.
3. Package ane
In steps 1 and 2, we can obtain three files.
HelloANENative. jar, HelloANELibrary. swc, extension. xml
Change the extension of helloanelibrary.swcto .rarto decompress the library.swf file. Then, we will get four files.
Put the four files in the following directory:
Run the following command to enter the directory:
Adt-package-storetype PKCS12-keystore 123. p12-storepass 123-target ane helloworld. ane extension. xml-swc HelloANELibrary. swc-platform Android-ARM-C Android-ARM.
You can see helloworld. ane in the current directory.
Certificate generation command:
Adt-certificate-cn SelfSign-ou QE-o "Example, Co"-c US 2048-RSA newcert. p12 39 # wnetx3tl
Adt-certificate-cn ADigitalID 1024-RSA SigningCert. p12 39 # wnetx3tl IV. Test ane
Create a Flex mobile project and select android as the target platform.
Right-click the project-properties, click Flex build path on the left side of the property panel, select local extension, and add ANE.
Click Flex build package on the left side of attribute bread, select Google Android, and click Local extension. Check the ANE package you want to introduce and click OK.
At this time, the ANE packages you just checked will appear under the referenced library of the project. You can import the ANE class in the flash project.