Android ane written in C language for flash (local extension)

Source: Internet
Author: User

Use the flex SDK 4.6, Android-ndk, and running environment fedora 17 (in Windows ).

I will not talk about the installation and configuration of flex and Android-ndk. There are many articles on the Internet.

Original Works. retain original author information during reprinting.


1. Create a JNI folder in the working directory, and copy the android/flashruntimeextensions. So file in the lib directory of the Flex SDK, and flashruntimeextensions. h In the include directory to the JNI directory.


2. Create a makefile in the JNI Directory: Android. mk

Include $ (clear_vars) local_module: = flashlocal_src_files: = flashruntimeextensions. soinclude $ (prebuilt_shared_library) include $ (clear_vars) local_module: = mylocal_shared_libraries: = flashlocal_src_files: = test. C # Code include $ (build_shared_library) whose source code is 3rd)

3. Write the C code test. C:

#include<FlashRuntimeExtensions.h>#include <stdlib.h>#include <string.h>FREObject test(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[]){               const uint8_t* msg = (const uint8_t*)"Hello world!!!!";        FREObject retObj;        FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);        return retObj;}void ContextInitializer(void* extData, const uint8_t* ctxType,FREContext ctx,uint32_t* numFunctionsToTest,const FRENamedFunction** functionsToSet){        *numFunctionsToTest= 1;        FRENamedFunction*func = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*1);        func[0].name= (const uint8_t*)"test";        func[0].functionData= NULL;        func[0].function= &test;        *functionsToSet= func;}void ContextFinalizer(FREContext ctx) {        return;}void ExtInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet,FREContextFinalizer* ctxFinalizerToSet){        *extDataToSet= NULL;        *ctxInitializerToSet= ContextInitializer;        *ctxFinalizerToSet= ContextFinalizer;}void ExtFinalizer(void* extData){        return;}

 



4. Generate the libmy. So file:

Run ndk-build in the JNI directory

You can find libmy. So in OBJ/local/armeabi/, indicating that the compilation is successful,



5. The configuration file of ANE (New myane. XML in the current working directory ):

<extension xmlns="http://ns.adobe.com/air/extension/3.1"><id>libiao.MyExtension</id><versionNumber>1.0.0</versionNumber><platforms><platform name="Android-ARM"><applicationDeployment><nativeLibrary>libmy.so</nativeLibrary><initializer>ExtInitializer</initializer><finalizer>ExtFinalizer</finalizer></applicationDeployment></platform></platforms></extension>

 


6. Flash interface file (New myextension. As in the current working directory ):

package{        import flash.events.EventDispatcher;        import flash.external.ExtensionContext;        public class MyExtension extends EventDispatcher        {                private static var context:ExtensionContext;                public function MyExtension()                {                        super();                        try                        {                                context= ExtensionContext.createExtensionContext("libiao.MyExtension","");                        }catch(e:Error){}                }                public function say():String                {                        var str:String = context.call("test") as String;                        return str;                }        }}

 


7. Generate the SWC file (run in the current directory ):

Acompc-source-path.-include-classes myextension-SWF-version = 14-output myextension. SWC


8.use unzips to decompress library.swfin myextension.swc, create a new file folder extension, and put the libmy. So files under library.swf and OBJ/local/armeabi/into the extension folder.



9. Generate the ANE file (helloworld. ane) and run it in the same level directory of extendsion:

ADT-package-target ane helloworld. Ane myane. XML-SWC *. SWC-platform Android-arm-C extension.


10. Use ANE in Flash as3:

Main.:

package{        import flash.display.Sprite;        import flash.text.TextField;        /**         * ...         * @author db0@qq.com         */        public class Main extends Sprite        {                private var ext:MyExtension = new MyExtension();                private var ta:TextField = new TextField();                public function Main():void                {                        addChild(ta);                        ta.text = ext.say();                }        )     }

 


11. Generate the SWF file:

Amxmlc main. As-library-path + = myextension. SWC-DEBUG = true-output test.swf


12. Air configuration file my-app.xml:

<application xmlns="http://ns.adobe.com/air/application/3.1"><supportedProfiles>mobileDevice</supportedProfiles><extensions><extensionID>libiao.MyExtension</extensionID></extensions><id>ExtensionTest</id><filename>ExtensionTest</filename><name>ExtensionTest</name><versionNumber>1.0.0</versionNumber><initialWindow><content>Test.swf</content><visible>true</visible><systemChrome>none</systemChrome><transparent>false</transparent><minimizable>false</minimizable><maximizable>false</maximizable><resizable>false</resizable><aspectRatio>landscape</aspectRatio><autoOrients>false</autoOrients><renderMode>auto</renderMode></initialWindow><android><manifestAdditions><![CDATA[<manifest><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_PHONE_STATE"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.RECORD_AUDIO"/><uses-permission android:name="android.permission.DISABLE_KEYGUARD"/><uses-permission android:name="android.permission.WAKE_LOCK"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/></manifest>]></manifestAdditions></android></application>

 


13.package into apk(main.apk ):

ADT-package-target APK-storetype PKCS12-key store my. p12-storepass FD main.apk my-app.xml-extdir. test.swf


14.run main.apk on your mobile phone. You can see helloword !!!, Congratulations! You're done.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.