Flash还是很好的,不过国内没人用. Whatever了. 以下是ANE制作中的一点笔记 分享下 也留作备份

Step1 编写As3 Library

package com.eran {
    import flash.external.ExtensionContext;
    import flash.system.Capabilities;

    public class WhateverNameAS {

        private static var mInstance:WhateverNameAS;

        public static function get instance():WhateverNameAS {
            if (mInstance == null) {
                mInstance = new WhateverNameAS();
            }
            return mInstance;
        }

        private var mExtContext:ExtensionContext;
        private var mIsMobile:Boolean;

        public function WhateverNameAS() {
            initialize();
        }

        private function initialize():void {
            mIsMobile = Capabilities.manufacturer.indexOf("iOS") != -1 || Capabilities.manufacturer.indexOf("Android") != -1;
            if (mIsMobile) {
                mExtContext = ExtensionContext.createExtensionContext("HereISExtensionID", "HereISContextType");
                if (mExtContext != null) {
                    mExtContext.call("initMe")
                } else {
                    trace("Error: can't create context");
                }
            }
        }

        public function callFun1():void {
            if (mIsMobile) {
                if (mExtContext != null) {
                    mExtContext.call("fun1");
                } else {
                    trace("Error: context is null");
                }
            }
        }
    }
}

编写后生成*.swc ,这里我命名为ANESWC.swc, 先放在某个目录留到后续步骤用

Step2 写Java 导出Jar包

主入口

package com.eran;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREExtension;

public class HereIsMainEntry implements FREExtension {

    @Override
    public void initialize() {
    }

    @Override
    public FREContext createContext(String s) {
        if (s.equals("HereISContextType")) {
            return new WhateverNameContext();
        }
        return null;
    }

    @Override
    public void dispose() {
    }
}

Context类

package com.eran;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.eran.fun.WhateverFun1;
import com.eran.fun.WhateverInitFun;

import java.util.HashMap;
import java.util.Map;

public class WhateverNameContext extends FREContext {

    @Override
    public Map<String, FREFunction> getFunctions() {
        Map<String, FREFunction> functionMap = new HashMap<String, FREFunction>();
        functionMap.put("initMe", new WhateverInitFun());
        functionMap.put("fun1", new WhateverFun1());
        return functionMap;
    }

    @Override
    public void dispose() {
    }
}

Fun1

package com.eran.fun;

import android.widget.Toast;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;

public class WhateverFun1 implements FREFunction {
    @Override
    public FREObject call(FREContext freContext, FREObject[] freObjects) {
        Toast.makeText(freContext.getActivity(), "WhateverFun1 called", Toast.LENGTH_SHORT).show();
        return null;
    }
}

Init Fun

package com.eran.fun;

import android.widget.Toast;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;

public class WhateverInitFun implements FREFunction {
    @Override
    public FREObject call(FREContext freContext, FREObject[] freObjects) {
        Toast.makeText(freContext.getActivity(), "WhateverInitFun called", Toast.LENGTH_SHORT).show();
        return null;
    }
}

打包Jar流程, Intellij Idea 13~15 流程都一样

Artifacts 选择 From modules

ANE 从入门到精通 --- 简单的Whatever-LMLPHP

选择刚写的Java工程

ANE 从入门到精通 --- 简单的Whatever-LMLPHP

改掉导出的Jar名称,并且去除被Import进来的 FlashRuntimeExtensions.jar

这个文件我是直接从AIR SDK 目录里面直接复制到工程的lib目录 同时设置了引用

ANE 从入门到精通 --- 简单的Whatever-LMLPHP

最终的效果图

ANE 从入门到精通 --- 简单的Whatever-LMLPHP

运行打包后应该生成 *.jar文件, 这里我的Jar文件叫WhateverJAR.jar

Step3 创建打包用的目录

ANE 从入门到精通 --- 简单的Whatever-LMLPHP

在任意位置创建一个文件夹,内部结构如上图. 其中

adt.jar 文件是从AIR SDK中直接复制过来,

ANESWC.swc 是Step1中生成

将ANESWC.swc 用解压工具解压,将里面的library.swf 分别放入 Android-ARM目录和default 目录

将Step2 生成的Jar包WhateverJAR.jar 放入 Android-ARM目录

extension.xml 文件 内容如下

<extension xmlns="http://ns.adobe.com/air/extension/20.0">
  <id>HereISExtensionID</id>
  <versionNumber>1</versionNumber>
  <platforms>
    <platform name="Android-ARM">
      <applicationDeployment>
        <nativeLibrary>WhateverJAR.jar</nativeLibrary>
        <initializer>com.eran.HereIsMainEntry</initializer>
        <finalizer>com.eran.HereIsMainEntry</finalizer>
      </applicationDeployment>
    </platform>
    <platform name="default">
        <applicationDeployment/>
    </platform>
  </platforms>
</extension>

其中 http://ns.adobe.com/air/extension/20.0 是我用到的AIR SDK 版本号(我用的是AIR SDK20 所有版本是20.0)

id 字段就是在Step1中创建Context所填的第一个参数

mExtContext = ExtensionContext.createExtensionContext("HereISExtensionID", "HereISContextType");

同时注意该字段也是测试工程的 *-app.xml中 引用当前ANE所使用的名字

    <extensions>
        <extensionID>HereISExtensionID</extensionID>
    </extensions>

而函数的第二个字段 "HereISContextType" 是Step2中 传入Java的入口文件HereIsMainEntry.java 的参数

    @Override
    public FREContext createContext(String s) {
        if (s.equals("HereISContextType")) {
            return new WhateverNameContext();
        }
        return null;
    }

编辑build.bat

@echo off

cd /d %~dp0
set adt=java -jar adt.jar
set ane=AneT1.ane
set target=ane %ane% .\extension.xml
set swc=./ANESWC.swc
set platform-android=-platform Android-ARM -C ./Android-ARM .
set platform-default=-platform default -C ./default .

echo Packaging...
if exist %ane% del %ane% > nul
%adt% -package -target %target% -swc %swc% %platform-ios-arm% %platform-ios-x86% %platform-android% %platform-default%

echo Finish!
pause

运行build.bat 生成最终AneT1.ane, 导出测试工程

最终生成的Ane结构如下:

ANE 从入门到精通 --- 简单的Whatever-LMLPHP

总结:

生成了default平台,这样在PC端进行调试时候 不会出现该ANE只适合Android 从而无法再PC模拟器运行的情况

同时ANE的swc内部 通过

 mIsMobile = Capabilities.manufacturer.indexOf("iOS") != -1 || Capabilities.manufacturer.indexOf("Android") != -1;

确保其ANE一定运行在Mobile环境中, PC环境仅仅是个空实现

04-25 14:41