34
Using Java interop in your Xamarin.Andr oid apps @WMeints

Using Java interop in your Xamarin.Android apps

  • Upload
    ardara

  • View
    95

  • Download
    0

Embed Size (px)

DESCRIPTION

Using Java interop in your Xamarin.Android apps. @ WMeints. Agenda. Java interop … wait what!? Beyond basic interop Building your own interop components. Java interop , wait what?!. Java interop , what what ?!. Building an Android app in C#. namespace TaskTracker.Client.Android - PowerPoint PPT Presentation

Citation preview

Page 1: Using Java  interop  in your  Xamarin.Android  apps

Using Java interop in your

Xamarin.Android apps

@WMeints

Page 2: Using Java  interop  in your  Xamarin.Android  apps

AgendaJava interop… wait what!?Beyond basic interopBuilding your own interop components

Page 3: Using Java  interop  in your  Xamarin.Android  apps

JAVA INTEROP, WAIT WHAT?!

Page 4: Using Java  interop  in your  Xamarin.Android  apps

Java interop, what what?!Building an Android app in C#

namespace TaskTracker.Client.Android{ [Activity(Label = "TaskTracker.Client.Android", MainLauncher = true, Icon = "@drawable/icon")] public class Activity1 : ListActivity { private TasksOfflineContext _context;

protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);

_context = new TasksOfflineContext("sync", new Uri("http://10.0.2.2:15314/taskssyncservice.svc/"));

_context.CacheController.ControllerBehavior.SerializationFormat = SerializationFormat.ODataJSON;

_context.CacheController.RefreshCompleted += OnRefreshCompleted; _context.CacheController.RefreshAsync(); }

Page 5: Using Java  interop  in your  Xamarin.Android  apps

Java interop, what what?!Meanwhile in Java country…

Cross Platform Mobile Development - Android

public class Activity1 extends android.app.ListActivity implements mono.android.IGCUserPeer{

static final String __md_methods;static {

__md_methods = "n_onCreate:(Landroid/os/Bundle;)V:GetOnCreate_Landroid_os_Bundle_Handler\n" +"";

mono.android.Runtime.register ("TaskTracker.Client.Android.Activity1, “ + “TaskTracker.Client.Android, Version=1.0.0.0, Culture=neutral,

PublicKeyToken=null", Activity1.class, __md_methods);

}

public Activity1 (){

super ();if (getClass () == Activity1.class)

mono.android.TypeManager.Activate ("TaskTracker.Client.Android.Activity1, “ + “TaskTracker.Client.Android, Version=1.0.0.0,

Culture=neutral, “ …

Page 6: Using Java  interop  in your  Xamarin.Android  apps

Java interop, what what?!

Linux Kernel

Mono (.NET Runtime) Dalvik (Java Runtime)

.NET APIs Android Bindings

Android.* Java.*MCW

ACW

Android callable wrappers

Mono callable wrappers

Page 7: Using Java  interop  in your  Xamarin.Android  apps

Java interop, wait what?!What you are essentially doing is building apps that rely heavily upon interop.

In fact: Everything you build on Android will at some point talk to a piece Java code.

Cross Platform Mobile Development - Android

Page 8: Using Java  interop  in your  Xamarin.Android  apps

Java interop, wait what?!So why not take advantage of the situation?

– You can extend your app with Java code– Found a cool Android library? Bind it!

Cross Platform Mobile Development - Android

Page 9: Using Java  interop  in your  Xamarin.Android  apps

BEYOND BASIC INTEROP

Page 10: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interopThe two-way traffic between Android and Mono is done through two endpoints:

– Mono Java Mono Callable Wrappers– Java Mono Android Callable Wrappers

Cross Platform Mobile Development - Android

Page 11: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interop - MCWs

Linux Kernel

Mono (.NET Runtime) Dalvik (Java Runtime)

.NET APIs Android Bindings

Android.* Java.*MCW

ACW

Page 12: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interop – MCWsTalking to Java from .NET is done through the Java Native Interface

– The next bit is going to get pointy or pointerific depending on what you like…

Cross Platform Mobile Development - Android

Page 13: Using Java  interop  in your  Xamarin.Android  apps

MCW INTERNALSDemo

Cross Platform Mobile Development - Android

Page 14: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interop – MCWsSteps to create an instance of a Java class– Find the handle to the type– Marshal constructor arguments– Invoke the constructor– Safe the instance pointer!

Cross Platform Mobile Development - Android

Page 15: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interop – MCWsSteps to invoke a Java method:– Find the handle to the method– Marshal the arguments– Invoke the method

Cross Platform Mobile Development - Android

Page 16: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interop – MCWsNotice the TransferOwnership settings

– Important, who is the owner of the handle?– Care must be taken when transferring handles– If two objects own a handle, the app will be

unstable!

Cross Platform Mobile Development - Android

Page 17: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interop – MCWsImportant to know:

– When invoking JNI, native handles are used– Has effect on garbage collection, so clean it up!– Please, Reduce the amount of memory copy

actions, it will improve the performance.

Cross Platform Mobile Development - Android

Page 18: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interop - ACWs

Linux Kernel

Mono (.NET Runtime) Dalvik (Java Runtime)

.NET APIs Android Bindings

Android.* Java.*MCW

ACW

Page 19: Using Java  interop  in your  Xamarin.Android  apps

ACW INTERNALSDemo

Cross Platform Mobile Development - Android

Page 20: Using Java  interop  in your  Xamarin.Android  apps

Beyond basic interop - ACWsAndroid callable wrappers are the least of your problems.

– Generated by the Mono compiler– Don’t touch or you will break them!

Cross Platform Mobile Development - Android

Page 21: Using Java  interop  in your  Xamarin.Android  apps

Performance considerationsA few things you need to know:

– Value types are copied between Java and Mono– For reference types pointers are exchanged– 4 bytes in .NET != 4 bytes in Java, sometimes

Cross Platform Mobile Development - Android

Beware Bitmap users!

Page 22: Using Java  interop  in your  Xamarin.Android  apps

BUILDING YOUR OWN INTEROP

Page 23: Using Java  interop  in your  Xamarin.Android  apps

Building your own interopExplicit use of interop is possible from your own app through these methods:

– Add Java source files to your project– Create bindings for an existing library

Cross Platform Mobile Development - Android

Page 24: Using Java  interop  in your  Xamarin.Android  apps

Adding Java sourcesAdd a .java file to your project for

– You found an activity or service that you don’t want to translate to .NET code

– You have a single component, but not enough to create a library (Please keep it to one file).

Cross Platform Mobile Development - Android

Page 25: Using Java  interop  in your  Xamarin.Android  apps

ADDING JAVA SOURCESDemo

Cross Platform Mobile Development - Android

Page 26: Using Java  interop  in your  Xamarin.Android  apps

Binding Java librariesThis is the real stuff, the big one, the goodest.

– Allows you to use existing libraries– Automatically generates wrappers for Java classes

based on the settings you provide.

Cross Platform Mobile Development - Android

Page 27: Using Java  interop  in your  Xamarin.Android  apps

Binding Java librariesThe steps for binding a Java library:

1. Add the Java library to the binding project2. Customize the transformations 3. Extend the library with your own goodies

Cross Platform Mobile Development - Android

Page 28: Using Java  interop  in your  Xamarin.Android  apps

Binding Java librariesThe steps for binding a Java library:

1. Add the Java library to the binding project2. Customize the transformations 3. Extend the library with your own goodies

Cross Platform Mobile Development - Android

Page 29: Using Java  interop  in your  Xamarin.Android  apps

BINDING A JAVA LIBRARYDemo

Cross Platform Mobile Development - Android

Page 30: Using Java  interop  in your  Xamarin.Android  apps

Binding Java librariesThe steps for binding a Java library:

1. Add the Java library to the binding project2. Customize the transformations 3. Extend the library with your own goodies

Cross Platform Mobile Development - Android

Page 31: Using Java  interop  in your  Xamarin.Android  apps

CUSTOMIZING TRANSFORMATIONSDemo

Cross Platform Mobile Development - Android

Page 32: Using Java  interop  in your  Xamarin.Android  apps

Binding Java librariesThe steps for binding a Java library:

1. Add the Java library to the binding project2. Customize the transformations 3. Extend the library with your own goodies

Cross Platform Mobile Development - Android

Page 33: Using Java  interop  in your  Xamarin.Android  apps

EXTENDING BINDINGSDemo

Cross Platform Mobile Development - Android

Page 34: Using Java  interop  in your  Xamarin.Android  apps

Final thoughtsJava interop is all around you in Xamarin.Android

– Don’t worry too much about it in your day-to-day Android development.

– Use it when you need it, to get more power!

Cross Platform Mobile Development - Android