Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile...

Preview:

DESCRIPTION

On Saturday, 19 of July, regular quarterly meeting of Tech Hangout Community took place in Creative Space 12, the cultural and educational center based in Kiev! The event was held under the motto “One day of inspiring talks on Mobile Software Development!”. This time enthusiastic and proactive people gathered to share their tips & tricks in mobile software development. *TECH HANGOUT COMMUNITY was found in 2012 by the developers for the developers for knowledge and experience sharing. Such meetings are the part of Innovecs Educational Project that actively develops sphere of internal trainings and knowledge exchange program among professionals. This Initiative was born within the walls of Innovecs and has proved to be extremely popular and high-demand. In a short period of time it gained its own Facebook group with more than 90 members, blog with more than 40 posts and constant quarterly external meeting of Tech hangout community with more than 80 participants. The concept of the event proposes a 30-minute report on the topic previously defined, and the discussion in a roundtable session format. Join to discuss - https://www.facebook.com/groups/techhangout/

Citation preview

Android Native Plugins

Artem KuzmenkoAndroid Software Engineer

Innovecs

Plan for each plugin

- Software used- Programming languages required- General steps- Information source, community help

- Personal experience- End users feedback

Common patternMain SDK

Bridge class

Helper class

End user’s code

Android

Cross-platform tool

Software used

- Eclipse- Sublime Text 2

Programming languages required

- Java- JavaScript

Activity

Cordova Plugin

General steps

MyPlugin.js

index.html

Android

PhoneGap

Cordova Plugin

- Java class extends CordovaPlugin (cordova.jar)- Add consts, implement all needed interfaces- “execute()” method

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

if (NAME_TO_POSITION_ACTION.equals(action)) {

JSONObject arg_object = args.getJSONObject(0);

String name = arg_object.getString("name");

int position = arg_object.getInt("position");

cordova.getActivity().runOnUiThread( … );

callbackContext.success();return true;

} else if ….

Return result to js

- From “execute()” method

boolean isReady = MainSdk.isReady();PluginResult result = new PluginResult(PluginResult.Status.OK, isReady);result.setKeepCallback(false);callbackContext.sendPluginResult(result);

- Primitives, JSONObject, JSONArray, byte array

PluginResult.Statuspublic enum Status { NO_RESULT, OK, CLASS_NOT_FOUND_EXCEPTION, ILLEGAL_ACCESS_EXCEPTION, INSTANTIATION_EXCEPTION, MALFORMED_URL_EXCEPTION, IO_EXCEPTION, INVALID_ACTION, JSON_EXCEPTION, ERROR}

Config.xml

<feature name="MyPlugin"> <param name="android-package" value="com.myphonegap.MyPlugin" /></feature>

index.html

<script type="text/javascript" src="js/myplugin.js"></script>

MyPlugin.jsMyPlugin.prototype.init = function(successCallback, errorCallback, arg1, arg2) { cordova.exec ( successCallback, // success callback function errorCallback, // error callback function 'MyPlugin', // mapped to our native Java class called "MyPlugin" 'init', // with this action name [ // and this array of custom arguments

{ "arg1": arg1,

"arg2": arg2}

] ); };

Information source, community help

- http://docs.phonegap.com/- https://github.com/phonegap/phonegap/wiki

Software used

- Eclipse- Unity- MonoDevelop-Unity

Programming languages required

- Java- C# or JavaScript

Main SDK

Bridge class

General steps

Helper class (o)

MonoBehaviour

Android

Unity3d

Bridge class

- Java, android project- Add external jar - Unity classes.jar

Unity\Editor\Data\PlaybackEngines\androiddevelopmentplayer\bin\classes.jar”

- Extend UnityPlayerActivity, Activity(to place in subview) - optional- Implement all needed interfaces, add public API- UnityPlayer (com.unity3d.player.UnityPlayer)

UnityPlayer.currentActivityUnityPlayer.UnitySendMessage(String gameObjectName, String method, String message)

- Resource id (string, array, bool, layout etc.)int id = getResources().getIdentifier(String name, String defType, String defPackage)

Helper class

- C# or JS- Add consts- Add interfaces for MonoBehaviours to implement- AndroidJavaObject for the bridge class (from constructor for ex.)

AndroidJavaObject mBridge = new AndroidJavaObject("com.bridge.package");mBridge .Call ("setGameObjectForCallback", gameObject.name);

- Add public functions which calls bridge APImBridge.Call ("setNameToPosition", name, position);mBridge.Call<bool>("isReady");

MonoBehaviour

- C# or JS- Implement interfaces- Initialize Helper class from onStart()

mHelper = new PluginHelper (gameObject);

- Call Helper functions from onStart(), onGUI() etc.mHelper.setNameToPosition(name, position);bool isReady = mHelper.isReady();

AndroidManifest.xml

- Default Unity manifest\Unity\Editor\Data\PlaybackEngines\androidplayer\AndroidManifest.xml

- Custom plugin manifest, add your permissions, services, activities etc.\Assets\Plugins\Android\AndroidManifest.xml

- Result manifestProject_Folder\Temp\StagingArea\AndroidManifest.xml

- Custom Main Activity<meta-data

android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />

Pitfalls

- Resource ids- Hardware buttons: Back, Menu

Bridge class can have a boolean onBackPressed method

- Sensor, gyroscope when extending regular Activity

Information source, community help

- http://docs.unity3d.com/- http://answers.unity3d.com/- http://forum.unity3d.com/

AIR (Adobe Integrated Runtime)

Software used

- Eclipse- FlashDevelop- Adobe Flash Professional CS6

Programming languages required

- Java- ActionScript 3

FREContext

FREExtension

General steps

AIR Mobile AS3 bridge

Android

AIR

FREFunction

Air Native Extension

Java part

- FlashRuntimeExtensions.jar- MyExtension implements FREExtension (initialize, dispose)

@Overridepublic FREContext createContext(String contextType) {

return new MyContext();}

- MyContext extends FREContext@Override

public Map<String, FREFunction> getFunctions() { … }

FREFunction interface@Overridepublic FREObject call(FREContext context, FREObject[] args)

- argsgetAsString(), getAsInt(), getAsBool(), getAsDouble()

- return null or FREObject.newObject(...)String, int, boolean, double

- catch FREExceptionsFRETypeMismatchException, FREInvalidObjectException, FREWrongThreadException

etc.

- send async event to AS from FREFunctioncontext.dispatchStatusEventAsync(MyExtension.READY_EVENT, “ready”);

ActionScript bridge

- extends EventDispatcher- add consts- implement calls to Java FREContext

private static var context:ExtensionContext;

public static function setNameToPosition(name:String, position:int):void {context.call("setNameToPosition", name, position);

}

public static function isReady():Boolean {return context.call("isReady"); }

Extension.xml

- id - com.my.air.plugin.MyExtension

- versionNumber - 0.9

- nativeLibrary - MyAirPlugin.jar

- initializer - com.my.air.plugin.extensions.MyExtension

- finalizer - com.my.air.plugin.extensions.MyExtension

AndroidManifest.xml

- application.xml in FlashDevelop

Information source, community help

Native extensions for Adobe AIRhttp://www.adobe.com/devnet/air/native-extensions-for-air.html

Using native extensions for Adobe AIRhttp://help.adobe.com/en_US/air/build/WS597e5dadb9cc1e0253f7d2fc1311b491071-8000.html

Building a native extension for iOS and Androidhttp://www.adobe.com/devnet/air/articles/building-ane-ios-android-pt1.html

Recommended