Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS,...

92
Download class materials from university.xamarin.com Building a Java Bindings Library AND450

Transcript of Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS,...

Page 1: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Download class materials from

university.xamarin.com

Building a Java Bindings

Library

AND450

Page 2: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Information in this document is subject to change without notice. The example companies,

organizations, products, people, and events depicted herein are fictitious. No association with

any real company, organization, product, person or event is intended or should be inferred.

Complying with all applicable copyright laws is the responsibility of the user.

Microsoft or Xamarin may have patents, patent applications, trademarked, copyrights, or other

intellectual property rights covering subject matter in this document. Except as expressly

provided in any license agreement from Microsoft or Xamarin, the furnishing of this document

does not give you any license to these patents, trademarks, or other intellectual property.

© 2014-2017 Xamarin Inc., Microsoft. All rights reserved.

Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual

Studio are either registered trademarks or trademarks of Microsoft in the U.S.A. and/or other

countries.

Other product and company names herein may be the trademarks of their respective owners.

Page 3: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Determine which Java libraries can

be bound

2. Bind a .JAR

3. Bind an .AAR

4. Handle a private-use reference

.JAR

5. Handle a public-use reference .JAR

6. Examine how Java-language

constructs are mapped to C#

Objectives

Page 4: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Determine which Java libraries can be

bound

Page 5: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Define the term Bindings Library

2. Describe which libraries can be

bound

Tasks

Page 6: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

The Android community has many Java libraries you might want to use

in your app

Motivation

PayPal TritonPlayerArcGIS ...

Mapping Finance Music

Page 7: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

You can use JNI or a Bindings Library to incorporate Java libraries into

your Xamarin.Android app

Java integration options

Bindings

Library

JNI

.JAR or .AARYour app

Page 8: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

The Java Native Interface (JNI) enables interop between Java and other

languages

What is JNI?

IntPtr type = JNIEnv.FindClass("com/xamarin/mycode/MyClass");IntPtr constructor = JNIEnv.GetMethodID(type, "<init>", "()V");IntPtr instance = JNIEnv.NewObject(type, constructor);IntPtr method = JNIEnv.GetMethodID(type, "myMethod", "(I)Ljava/lang/String;");JValue argument = new JValue(17);IntPtr result = JNIEnv.CallObjectMethod(instance, method, argument);

JNI is verbose: this constructs

an object and calls a method

package com.xamarin.mycode;

public class MyClass{

public String myMethod(int i) { ... }}

Page 9: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

A Bindings Library is an assembly containing Managed Callable

Wrappers for Java types

What is a Bindings Library?

The Bindings Library

defines this class

and this method

var instance = new MyClass();

string result = instance.MyMethod(17);

package com.xamarin.mycode;

public class MyClass{

public String myMethod(int i) { ... }}

Page 10: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bindings Library

Bindings libraries use JNI internally to interoperate with Java

Bindings implementation

.JAR

or

.AAR

IntPtr m = JNIEnv.GetMethodID(...);JValue a = new Jvalue(...);IntPtr r = JNIEnv.CallObjectMethod(...);

string result = instance.MyMethod(17);Your code

Binding code

Page 11: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bindings are for Java libraries that were built for Android

Android libraries only

Bindings

Library

This Java library should

be for use with Android

.JAR or .AARYour app

Binding non-Android Java libraries may work but is not an officially-supported scenario

Page 12: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Flash Quiz

Page 13: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

① You can create Bindings Libraries for .JAR files and .AAR files

a) True

b) False

Flash Quiz

Page 14: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

① You can create Bindings Libraries for .JAR files and .AAR files

a) True

b) False

Flash Quiz

Page 15: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

② You can create a Bindings Library for any Java library

a) True

b) False

Flash Quiz

Page 16: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

② You can create a Bindings Library for any Java library

a) True

b) False

Flash Quiz

Page 17: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Define the term Bindings Library

2. Describe which libraries can be

bound

Summary

Page 18: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bind a .JAR

Page 19: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Create a Bindings Library for a .JAR

2. Use the Bindings Library types

Tasks

Page 20: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

.JAR

A Java Archive (.JAR) is the unit-of-deployment for Java apps and

libraries

What is a .JAR file?

Compiled Java code .class files

Java manifest containing

metadata like the identity

of the main class (Note:

not an Android manifest)

Java Resources (Note:

not Android resources)Image and

text files

Manifest

Uses .ZIP file format

Page 21: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

A Managed Callable Wrapper is a C# type that wraps a Java type,

operations on the wrapper class are forwarded to the original Java type

What is a Managed Callable Wrapper?

public class Contact : Java.Lang.Object{ ...public virtual string GetPhoneNumber(string p0){

...}

}

C# wrapper class

public class Contact{ ...

public String getPhoneNumber(String category){

...}

}

Original Java type

Page 22: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bindings Library

Xamarin tools generate a Bindings Library from an input .JAR

Automatic generation

Xamarin

tools

Managed

Callable

Wrappers

.JAR

.JAR

Page 23: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bindings Library

A single Bindings Library can contain bindings for many .JAR files

Multiple input files

Xamarin

tools

.JAR

.JAR

.JAR

.JAR

Managed

Callable

Wrappers

Page 24: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Build Action determines if the .JAR will be embedded in the Bindings

Library

Build Action

Bindings Library

.JAR

Bindings Library

.JAR

EmbeddedJar InputJar

Managed

Callable

Wrappers

Managed

Callable

Wrappers

Embedding is preferred in practice because deployment is easy: the necessary .JARs will be

compiled to Dalvik bytecodes, packaged in the .APK, and available to the app at runtime.

Page 25: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

There are several steps to create a Bindings Library

How to create a .JAR Bindings Library

1. Create a

new project

Bindings

Library.JAR

2. Add your

.JAR(s)

5. Build3. Set the

Build Action

4. Choose the

target framework

Page 26: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

How to create a .JAR Bindings Library [1]

Create a Bindings Library project in Visual Studio

The template is

in the Android

Library category

Page 27: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Add your .JAR files to the project

How to create a .JAR Bindings Library [2]

Place your .JAR files

in the "Jars" folder

Page 28: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Set the Build Action for each .JAR file

How to create a .JAR Bindings Library [3]

Set the Build Action

to EmbeddedJaror InputJar

Page 29: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Set the Target framework version for your Bindings Library

How to create a .JAR Bindings Library [4]

E.g. usable with 4.4

and newer apps

Page 30: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Build the project, a Bindings Library project generates an assembly

How to create a .JAR Bindings Library [5]

Output is a .DLL containing the

Managed Callable Wrappers

Xamarin

tools

Page 31: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bindings Libraries are .DLLs, you reference them in your Android project

How to use a .JAR Bindings Library

1. Add a

reference var c = new Com.Mycompany.Mynamespace.MyClass();c.MyMethod();

2. Use the types

Page 32: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bind a .JAR

Individual Exercise

Page 33: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Create a Bindings Library for a .JAR

2. Use the Bindings Library types

Summary

Page 34: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bind an .AAR

Page 35: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Create a Bindings Library for an

.AAR

2. Use the Bindings Library types

3. Use the Bindings Library resources

Tasks

Page 36: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Uses .ZIP file format

An Android Archive (.AAR) file is the file format for Android libraries

What is an .AAR file?

.AAR

R.txt

classes.jar

res folder

AndroidManifest.xml

Compiled Java code

Meta-data e.g. Activity

declarations, permissions

Resources

Resource Ids

Page 37: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Binding an .AAR file is the same as a .JAR except for the Build Action

How to create an .AAR Bindings Library

3. Set the Build Action toLibraryProjectZip

1. Create an Android Java

Bindings Library project

2. Add your .AAR files to

the "Jars" folder

The .AAR will be embedded in the .DLL, this is the only option for .AAR files

Page 38: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Access to the types in an .AAR Bindings Libraries is the same as for .JARs

How to access .AAR types

1. Add a

reference var c = new Com.Mycompany.Mynamespace.MyClass();c.MyMethod();

2. Use the types

Page 39: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

You can access the resources in a bound .AAR file, the Xamarin tooling

merges the R data from the .AAR into your App’s Resource class

How to access .AAR resources

Bindings Library App’s .APK

Resourceclass

.AAR

R.txt

res folder

Xamarin

tools

Page 40: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Use the Resource.Drawable name for images packaged in the .AAR

How to access an .AAR image

public final class R{ ...

public static final class drawable { public static int logo=0x7f020000; }}

public partial class Resource{ ...

public partial class Drawable { public const int logo=2130837505; } }

<ImageView android:src="@drawable/logo" ... />

Java values are merged

into the C# Resource class

Page 41: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Use the Resource.Layout name for layouts packaged in the .AAR

How to access an .AAR layout

public final class R{ ...

public static final class layout { public static int row_layout=0x7f030001; }}

public partial class Resource{ ...

public partial class Layout { public const int row_layout=2130903042; } }

var a = new ArrayAdapter<string>(this, Resource.Layout.row_layout, ...);

Java values are merged

into the C# Resource class

Page 42: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Use the Resource.String name for strings packaged in the .AAR

How to access an .AAR string

public final class R{ ...

public static final class string { public static int greeting=0x7f040000; }}

public partial class Resource{ ...

public partial class String { public const int greeting=2130968576; } }

<TextView android:text="@string/greeting" ... />

Java values are merged

into the C# Resource class

Page 43: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Bind an .AAR

Individual Exercise

Page 44: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Create a Bindings Library for an

.AAR

2. Use the Bindings Library types

3. Use the Bindings Library resources

Summary

Page 45: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Handle a private-use reference .JAR

Page 46: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Include a private-use reference

.JAR in your Bindings Library

Tasks

Page 47: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

A reference .JAR is a .JAR file used by one of your bound .JAR or .AAR

files

What is a reference .JAR?

This is the library you need

to create bindings for...

.JAR or .AAR .JAR...however, it uses types defined

here, this is the reference .JAR

public class Contact{ ...}

Only .JAR files can be references, .AAR files cannot

Page 48: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

A reference .JAR is private-use if it only uses the types from the

reference .JAR behind-the-scenes and does not expose them to the

client

What is a private-use reference .JAR?

You use the types only as private fields,

package fields, local variables, etc.

.JAR or .AAR .JAR

public class Contact{ ...}

public class Mail{ ...private Contact[] friends;

Contact[] family; }

Note: the term private-use is used in this course, but is not an official phrase

Page 49: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

You do not need to create Managed Callable Wrappers for the types in

a private-use reference .JAR, the types are not exposed to the client

No bindings needed

If this is private-use only...

.JAR or .AAR .JAR

public class Contact{ ...}

public class Mail{ ...private Contact[] friends;

Contact[] family; }

... then no wrappers are needed for this class

Page 50: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

For private-use reference .JARs, there are no compile-time requirements

Compile-time requirements

When you create a Bindings

Library for this .JAR/.AAR...

.JAR or .AAR .JAR

public class Contact{ ...}

public class Mail{ ...private Contact[] friends;

Contact[] family; }

...you do not have to include this

.JAR in your project in any way

Page 51: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

A private-use reference .JARs must be available at runtime

Runtime requirements

.JAR or .AAR .JAR

public class Contact{ ...}

public class Mail{ ...private Contact[] friends;

Contact[] family; }

This library needs to be compiled from

Java bytecodes to Dalvik bytecodes and

be available on the device at runtime

Page 52: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

You can embed your private-use reference .JAR in your Bindings Library

.DLL using the EmbeddedReferenceJar Build Action

Reference .JAR embedding

EmbeddedReferenceJarwill not generate bindings

but will include it in the .DLL

Bindings Library

.JAR

.JAR

or

.AAR

Managed

Callable

Wrappers

Page 53: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Flash Quiz

Page 54: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

① You are required to include private-use reference .JARs in your Bindings

Library project

a) True

b) False

Flash Quiz

Page 55: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

① You are required include private-use reference .JARs in your Bindings

Library project

a) True

b) False

Flash Quiz

Page 56: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

② What does the EmbeddedReferenceJar Build Action do?

a) Embeds the .JAR and generates bindings

b) Embeds the .JAR but does not generate bindings

c) Does not embed the .JAR but does generate bindings

d) Does not embed the .JAR and does not generate bindings

Flash Quiz

Page 57: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

② What does the EmbeddedReferenceJar Build Action do?

a) Embeds the .JAR and generates bindings

b) Embeds the .JAR but does not generate bindings

c) Does not embed the .JAR but does generate bindings

d) Does not embed the .JAR and does not generate bindings

Flash Quiz

Page 58: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Include a private-use reference

.JAR in your Bindings Library

Summary

Page 59: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Handle a public-use reference .JAR

Page 60: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Make a public-use reference .JAR

available at compile time

Tasks

Page 61: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

A reference .JAR is public-use if it exposes the types from the reference

.JAR in public or protected members

What is a public-use reference .JAR?

.JAR or .AAR .JAR

public class Contact{ ...}

public class Mail{ ...public Contact Find(String n){ ...}

protected Contact spouse;}

You return the type from a public

method, have a protected field, etc.This reference .JAR is public-use

Note: the term public-use is used in this course, but is not an official phrase

Page 62: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

You need Managed Callable Wrappers for the types in a public-use

reference .JAR

Bindings required

.JAR or .AAR .JAR

public class Contact{ ...}

public class Mail{ ...public Contact Find(String n){ ...}

protected Contact spouse;}

The Contact class is

exposed to the client...

...so bindings are

needed for Contact

Page 63: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

.JAR or .AAR .JAR

public class Contact{ ...}

public class Mail{ ...public Contact Find(String n){ ...}

}

Reference .JARs must be available at compile time, this is needed

because the types are loaded into a JVM during binding generation

Compile-time requirements

To load the Mail class, the JVM needs

the Contact class to be available

Page 64: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

There are three options for compile-time availability, you decide based

on where/when you want to generate the bindings for the reference

.JAR

Compile-time options [overview]

Re-use: You already

have a Bindings

Library for the

reference .JAR

Create: You will make

a Bindings Library for

the reference .JAR

but have not done so

Merge: You want to

create the bindings

for both libraries in

this Bindings Library

Page 65: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

An assembly reference for the .DLL

containing the bindings for the

reference .JAR satisfies the compile-

time requirements, the Xamarin

tooling reads the .JAR inside the

.DLL

Compile-time options [Re-use]

.DLL containing

bindings for the

reference .JAR, it

has the reference

.JAR inside

.JAR you are

currently binding,

it exposes types

from the reference

.JAR

Re-use: You already

have a Bindings

Library for the

reference .JAR

Page 66: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Create: You will make

a Bindings Library for

the reference .JAR

but have not done so

Including the reference .JAR in your

Bindings Library project with the

ReferenceJar Build Action

satisfies the compile-time

requirements

Compile-time options [Create]

Do not create

bindings, do

not embed the

reference .JAR

in this .DLL

Page 67: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Merge: You want to

create the bindings for

both libraries in this

Bindings Library

Including the reference .JAR in your

Bindings Library project with the

EmbeddedJar Build Action satisfies

the compile-time requirements

Compile-time options [Merge]

Create bindings

and embed the

reference .JAR

in this .DLL

Page 68: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Flash Quiz

Page 69: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

① You must include public-use reference .JARs in your Bindings Library

project

a) True

b) False

Flash Quiz

Page 70: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

① You must include public-use reference .JARs in your Bindings Library

project

a) True

b) False

Flash Quiz

Page 71: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

② What does the ReferenceJar Build Action do?

a) Embeds the .JAR and generates bindings

b) Embeds the .JAR but does not generate bindings

c) Does not embed the .JAR but does generate bindings

d) Does not embed the .JAR and does not generate bindings

Flash Quiz

Page 72: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

② What does the ReferenceJar Build Action do?

a) Embeds the .JAR and generates bindings

b) Embeds the .JAR but does not generate bindings

c) Does not embed the .JAR but does generate bindings

d) Does not embed the .JAR and does not generate bindings

Flash Quiz

Page 73: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Make a public-use reference .JAR

available at compile time

Summary

Page 74: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Examine how Java-language

constructs are mapped to C#

Page 75: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Examine the C# Bindings for Java-

language constructs

Tasks

Page 76: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

public interface Exportable{ void export(int format);

public static final int PDF = 1;public static final int XPS = 2;

}

Some Java language constructs do not have a direct C# mapping

Motivation [language features]

E.g. Java interfaces can contain

constants which is not allowed in C#

Page 77: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Some Java patterns are mapped to C# language features

Motivation [patterns]

public class Contact{ ...public String getName() { ... }public void setName(String n) { ... }

}

E.g. C# would use properties, not get/set methods

Page 78: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

public class Contact{ ...public String send(String message){...

}}

Java and C# use different conventions for capitalization and naming

Motivation [conventions]

E.g. C# would use an uppercase first letter

Page 79: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Examine mappings for various Java constructs

Individual Exercise

Page 80: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java packages map to C# namespaces with uppercase first letters

Binding namespaces

namespace Com.Mycompany.Mypackage{...

}

package com.mycompany.mypackage;

Package Namespace

Page 81: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java classes map to C# classes

Binding classes

public class Contact : Java.Lang.Object{...

}

public class Contact{...

}

Class Base type is Java's Object class

Page 82: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java interfaces map to C# interfaces with an uppercase 'I' added to the

name and a class for constants (if needed)

Binding interfaces

public interface IExportable{

void Export(int p0);}public interface Exportable

{ void export(int format);

public static final int PDF = 1;public static final int XPS = 2;

}

Interface with methods and constants Interface for methods, class for constants

public abstract class Exportable{

public const int Pdf = 1;public const int Xps = 2;

}

Page 83: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java instance methods map to C# virtual methods since Java

methods use virtual dispatch by default

Binding instance methods

public class Contact : Java.Lang.Object{ ...

public virtual string Send(string p0){

...}

}

Virtual instance method,

parameter names are not preserved

public class Contact{ ...

public String send(String message){

...}

}

Instance method

Page 84: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java static methods map to C# static methods

Binding static methods

public class Contact : Java.Lang.Object{ ...

public static string Format(string p0);{

...}

}

Static method

public class Contact{ ...

public static String format(String value){

...}

}

Static method

Page 85: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java get/set methods map to C# properties

Binding get/set methods

public class Contact : Java.Lang.Object{ ...

public virtual string Name { get; set; }

public virtual int Age { get; }

public virtual void SetRate(double p0);}

Properties for Name and

Age, method for Rate

public class Contact{ ...

public String getName() { ... }public void setName(String n) { ... }

public int getAge() { ... }

public void setRate(double r) { ... }}

Properties created for get/set

and get-only, but not set-only

Page 86: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java fields map to C# properties

Binding fields

public class Contact{ ...public int address;

}

Public field

public class Contact : Java.Lang.Object{ ...public int Address { get; set; }

}

Read/write property

Page 87: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java set/add methods with a specific signature map to C# events

Binding listeners

public class Employee{ ...public void addSalaryListener(SalaryListener subscriber) { ... }public void setSalaryListener(SalaryListener subscriber) { ... }

}

Must have add or setprefix and Listener suffix

Must have

void returnMust have a single parameter

of an interface type

public event EventHandler<SalaryEventArgs> SalaryEvent;

Binding generates a delegate, an event-args class, and an event

Page 88: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java static nested classes map to C# nested classes

Binding static nested classes

public class List{ ...public static class Node{}

}

public class List : Java.Lang.Object{ ...public class Node : Java.Lang.Object{}

}

Nested classStatic nested class

Page 89: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java inner classes map to C# nested classes with a constructor that

requires an instance of the outer class

Binding inner classes

public class List{ ...public class Iterator{}

}

Must pass instance of outer class

public class List : Java.Lang.Object{ ...public class Iterator : Java.Lang.Object{public Iterator(List __self) { ... }

}}

Inner class

Page 90: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Java public and protected members get C# bindings by default

Bound access levels

public class Contact{ ...

public int myFieldPublic;protected int myFieldProtected;

private int myFieldPrivate;int myFieldPackage;

}

private and packagemembers not bound

public class Contact : Java.Lang.Object{ ...

public int MyFieldPublic { get; set; }protected int MyFieldProtected { get; set; }

}

public and protected member

are bound, access level is preserved

Page 91: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

1. Examine the C# Bindings for Java-

language constructs

Summary

Page 92: Building a Java Bindings Library - GitHub Pages · Xamarin, MonoTouch, MonoDroid, Xamarin.iOS, Xamarin.Android, Xamarin Studio, and Visual ... The Java Native Interface (JNI) enables

Thank You!

Please complete the class survey in your profile:

university.xamarin.com/profile