Using java interop in your xamarin.android apps

1,810 views

Published on

Talk I did on Java interop on the Mono for Android platform. Shows what Mono Callable Wrappers are and what you can do with them. Also demonstrates the use of Java Binding Libraries.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,810
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
14
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Using java interop in your xamarin.android apps

  1. 1. Using Java interop in yourXamarin.Android apps @WMeints
  2. 2. Agenda Java interop… wait what!? Beyond basic interop Building your own interop components
  3. 3. JAVA INTEROP, WAIT WHAT?!
  4. 4. 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(); }
  5. 5. Java interop, what what?! Meanwhile in Java country…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_Handlern" + ""; 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, “ … Cross Platform Mobile Development - Android
  6. 6. Java interop, what what?! Mono callable wrappers Android .NET APIs MCW Android.* Java.* Bindings Mono (.NET Runtime) ACW Dalvik (Java Runtime) Linux Kernel Android callable wrappers
  7. 7. 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
  8. 8. 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
  9. 9. BEYOND BASIC INTEROP
  10. 10. Beyond basic interop The 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
  11. 11. Beyond basic interop - MCWs Android .NET APIs MCW Android.* Java.* Bindings Mono (.NET Runtime) ACW Dalvik (Java Runtime) Linux Kernel
  12. 12. Beyond basic interop – MCWs Talking 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
  13. 13. DemoMCW INTERNALS Cross Platform Mobile Development - Android
  14. 14. Beyond basic interop – MCWs Steps 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
  15. 15. Beyond basic interop – MCWs Steps to invoke a Java method: – Find the handle to the method – Marshal the arguments – Invoke the method Cross Platform Mobile Development - Android
  16. 16. Beyond basic interop – MCWs Notice 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
  17. 17. Beyond basic interop – MCWs Important 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
  18. 18. Beyond basic interop - ACWs Android .NET APIs MCW Android.* Java.* Bindings Mono (.NET Runtime) ACW Dalvik (Java Runtime) Linux Kernel
  19. 19. DemoACW INTERNALS Cross Platform Mobile Development - Android
  20. 20. Beyond basic interop - ACWs Android 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
  21. 21. Performance considerations A 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 Beware Bitmap users! Cross Platform Mobile Development - Android
  22. 22. BUILDING YOUR OWN INTEROP
  23. 23. Building your own interop Explicit 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
  24. 24. Adding Java sources Add 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
  25. 25. DemoADDING JAVA SOURCES Cross Platform Mobile Development - Android
  26. 26. Binding Java libraries This 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
  27. 27. Binding Java libraries The steps for binding a Java library: 1. Add the Java library to the binding project 2. Customize the transformations 3. Extend the library with your own goodies Cross Platform Mobile Development - Android
  28. 28. Binding Java libraries The steps for binding a Java library: 1. Add the Java library to the binding project 2. Customize the transformations 3. Extend the library with your own goodies Cross Platform Mobile Development - Android
  29. 29. DemoBINDING A JAVA LIBRARY Cross Platform Mobile Development - Android
  30. 30. Binding Java libraries The steps for binding a Java library: 1. Add the Java library to the binding project 2. Customize the transformations 3. Extend the library with your own goodies Cross Platform Mobile Development - Android
  31. 31. DemoCUSTOMIZING TRANSFORMATIONS Cross Platform Mobile Development - Android
  32. 32. Binding Java libraries The steps for binding a Java library: 1. Add the Java library to the binding project 2. Customize the transformations 3. Extend the library with your own goodies Cross Platform Mobile Development - Android
  33. 33. DemoEXTENDING BINDINGS Cross Platform Mobile Development - Android
  34. 34. Final thoughts Java 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

×