Using Java interop in your Xamarin.Android

Slides:



Advertisements
Similar presentations
Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
Advertisements

CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Android architecture overview
Introduction to.NET Technology Marcello Benati Software Engineer.NET Architect.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#
Android Tutorial Android Written in Java Utilizes Dalvik VM – Just in time (JIT) compilation since Android 2.2.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Protection of Agent Teamwork By Jeremy Hall. Agent Teamwork Overview ● Mobile agent framework  AgentTeamwork 2 is a mobile-agent based middleware system.
Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department.
Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004.
OOP Languages: Java vs C++
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Programming Languages and Paradigms Object-Oriented Programming.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
.NET Framework & C#.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
@2011 Mihail L. Sichitiu1 Android Introduction Platform Overview.
ANDROID Presented By Mastan Vali.SK. © artesis 2008 | 2 1. Introduction 2. Platform 3. Software development 4. Advantages Main topics.
DUE Hello World on the Android Platform.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Android for Java Developers Denver Java Users Group Jan 11, Mike
.NET Framework Danish Sami UG Lead.NetFoundry
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Software Engineering in Robotics Interfacing to external functions Henrik I. Christensen –
Effective C# 50 Specific Ways to Improve Your C# Item 46~ /09/25 1.
tom perkins1 XML Web Services -.NET FRAMEWORK – Part 1 CHAPTER 1.1 – 1.3.
Understanding Xamarin Development Matt
1 First BlueJ Day, Houston, Texas, 1st March 2006 Writing BlueJ Extensions Ian Utting University of Kent.
GTK Binding for Alice Robert Grabowski Eine GTK-Schnittstelle für Alice.
Programming in Java CSCI-2220 Object Oriented Programming.
AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”
Copyright Curt Hill Variables What are they? Why do we need them?
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Slide Advanced Programming 2004, based on LY Stefanus's slides Native Methods.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Modern Programming Language. Web Container & Web Applications Web applications are server side applications The most essential requirement.
Classes, Interfaces and Packages
.NET Mobile Application Development XML Web Services.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Introduction to Object-oriented Programming
Object Oriented Programming in
Server Side Programming
Android Runtime – Dalvik VM
Java Programming Language
CASE STUDY 1: Linux and Android
University of Central Florida COP 3330 Object Oriented Programming
CMPE419 Mobile Application Development
Java Programming Language
More Object-Oriented Programming
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
The Most Popular Android UI Automation Testing Tool Andrii Voitenko
Programming with inheritance Based on slides by Alyssa Harding
CMPE419 Mobile Application Development
C# COM Interoperability
Presentation transcript:

Using Java interop in your Xamarin.Android

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 { [Activity(Label = "TaskTracker.Client.Android", MainLauncher = true, Icon = public class Activity1 : ListActivity { private TasksOfflineContext _context; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); _context = new TasksOfflineContext("sync", new Uri(" _context.CacheController.ControllerBehavior.SerializationFormat = SerializationFormat.ODataJSON; _context.CacheController.RefreshCompleted += OnRefreshCompleted; _context.CacheController.RefreshAsync(); }

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= , 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= , Culture=neutral, “ …

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

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

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

BEYOND BASIC INTEROP

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

Beyond basic interop - MCWs Linux Kernel Mono (.NET Runtime) Dalvik (Java Runtime).NET APIs Android Bindings Android.* Java.* MCW ACW

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

MCW INTERNALS Demo Cross Platform Mobile Development - Android

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

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

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

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

Beyond basic interop - ACWs Linux Kernel Mono (.NET Runtime) Dalvik (Java Runtime).NET APIs Android Bindings Android.* Java.* MCW ACW

ACW INTERNALS Demo Cross Platform Mobile Development - Android

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

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 Cross Platform Mobile Development - Android Beware Bitmap users!

BUILDING YOUR OWN INTEROP

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

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

ADDING JAVA SOURCES Demo Cross Platform Mobile Development - Android

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

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

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

BINDING A JAVA LIBRARY Demo Cross Platform Mobile Development - Android

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

CUSTOMIZING TRANSFORMATIONS Demo Cross Platform Mobile Development - Android

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

EXTENDING BINDINGS Demo Cross Platform Mobile Development - Android

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