32
Android Native Plugins Artem Kuzmenko Android Software Engineer Innovecs

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

Embed Size (px)

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

Page 1: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Android Native Plugins

Artem KuzmenkoAndroid Software Engineer

Innovecs

Page 2: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19
Page 3: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Plan for each plugin

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

- Personal experience- End users feedback

Page 4: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Common patternMain SDK

Bridge class

Helper class

End user’s code

Android

Cross-platform tool

Page 5: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19
Page 6: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Software used

- Eclipse- Sublime Text 2

Programming languages required

- Java- JavaScript

Page 7: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Activity

Cordova Plugin

General steps

MyPlugin.js

index.html

Android

PhoneGap

Page 8: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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 ….

Page 9: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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

Page 10: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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}

Page 11: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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>

Page 12: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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}

] ); };

Page 13: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Information source, community help

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

Page 14: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19
Page 15: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Software used

- Eclipse- Unity- MonoDevelop-Unity

Programming languages required

- Java- C# or JavaScript

Page 16: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Main SDK

Bridge class

General steps

Helper class (o)

MonoBehaviour

Android

Unity3d

Page 17: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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)

Page 18: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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");

Page 19: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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();

Page 20: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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" />

Page 21: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Pitfalls

- Resource ids- Hardware buttons: Back, Menu

Bridge class can have a boolean onBackPressed method

- Sensor, gyroscope when extending regular Activity

Page 22: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Information source, community help

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

Page 23: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

AIR (Adobe Integrated Runtime)

Page 24: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

Software used

- Eclipse- FlashDevelop- Adobe Flash Professional CS6

Programming languages required

- Java- ActionScript 3

Page 25: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

FREContext

FREExtension

General steps

AIR Mobile AS3 bridge

Android

AIR

FREFunction

Air Native Extension

Page 26: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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() { … }

Page 27: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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”);

Page 28: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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"); }

Page 29: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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

Page 30: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

AndroidManifest.xml

- application.xml in FlashDevelop

Page 31: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19

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

Page 32: Creating Android Plugins: Unity3d, Basic4Android, Phonegap, Adobe AIR (by Artem Kuzmenko) - Mobile Tech Hangout - 2014.07.19