搭建環境這裡就不詳細說明了
一、Android本地庫編寫
1.從Flash Air SDK中提取需要的jar包
Android下需要的jar包在Flash Air SDK/lib/android目錄下的FlashRuntimeExtensions.jar
2.建立Android工程,加入第一步提取的jar包
3.編寫與as互動的主要類Extension,Context,Function
自訂Extension類,實現FREExtension介面
自訂Context類,繼承FREContext類
自訂Function類,實現FREFunction介面
Function類可以定義多個,定義之後都註冊在自訂Context類的getFunctions方法中
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());
//可繼續put其他function
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());
//可繼續put其他function
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(), "測試成功:android本地被調用" + msg_.getAsString(), Toast.LENGTH_LONG).show();
result = FREObject.newObject("這是可返回的值");
} 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(), "測試成功:android本地被調用" + msg_.getAsString(), Toast.LENGTH_LONG).show();
result = FREObject.newObject("這是可返回的值");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
4.把剛剛編寫的工程以jar包的形式匯出,命名jar包(如:HelloANENative.jar)
二、AS端
1.建立Flex庫項目,選擇配置為手機庫
2.在項目的src目錄下建立extension.xml檔案,檔案基本內容如下(查看更多屬性):
[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><!--是之前置出的jar包-->
<!--即為Android本地FREExtension全路徑-->
<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><!--是之前置出的jar包-->
<!--即為Android本地FREExtension全路徑-->
<initializer>com.cn.example.android.TestExtension</initializer>
<finalizer>com.cn.example.android.TestExtension</finalizer>
</applicationDeployment>
</platform>
</platforms>
</extension>
3.編寫一個AS的Extension類,主要作用是建立ExtensionContext,與Android的Native代碼互動(建議使用單例)
[plain]
package com.adobe.nativeExtensions.test
{
import flash.external.ExtensionContext;
public class HelloWorldExtension
{
public static const KEY:String = "testToast";//與java端中Map裡的key一致
public static const EXTENSION_ID:String = "com.adobe.ane.test";//與extension.xml中的id標籤一致
private var extContext:ExtensionContext;
public function HelloWorldExtension()
{
//第二個為參數,會傳入java代碼中的FREExtension的createContext方法
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";//與java端中Map裡的key一致
public static const EXTENSION_ID:String = "com.adobe.ane.test";//與extension.xml中的id標籤一致
private var extContext:ExtensionContext;
public function HelloWorldExtension()
{
//第二個為參數,會傳入java代碼中的FREExtension的createContext方法
extContext = ExtensionContext.createExtensionContext(EXTENSION_ID,"");
}
public function hello(name:String):String{
if(extContext){
return extContext.call(KEY,name) as String;
}
return "call failed";
}
}
}
編譯得到.swf檔案(如:HelloANELibrary.swc)
到這一步,打包ane需要的檔案都有了
三、打包ane
通過一、二步我們可以獲得三個檔案
HelloANENative.jar、HelloANELibrary.swc 、extension.xml
把HelloANELibrary.swc的尾碼名改為.rar,解壓獲得library.swf檔案,此時我們將得到四個檔案
以一下目錄把4個檔案放好:
通過命令進入這個目錄,執行
adt -package -storetype PKCS12 -keystore 123.p12 -storepass 123 -target ane helloworld.ane extension.xml -swc HelloANELibrary.swc -platform Android-ARM -C Android-ARM .
在目前的目錄下就可以看到helloworld.ane
認證產生命令:
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四、測試ane
建立Flex手機項目,選擇android為目標平台。
對著工程點擊右鍵-屬性,在屬性面板的左邊點擊Flex構建路徑,在選擇本地擴充,添加ANE。
在點擊屬性麵包左邊的Flex構建打包,選擇Google Android後點擊本地擴充,對你需要引入的ANE包打勾,在點擊確定
此時項目的引用的庫下面就會出現你剛剛打勾的那些ANE包了。在flash項目中可以匯入ANE中的類使用。