Skip to content

Commit 9b911de

Browse files
committed
ff
1 parent 6d56386 commit 9b911de

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
id:
3+
title: "Binding Java Source"
4+
dateupdated: 2021-05-21
5+
---
6+
7+
# Overview
8+
9+
Bindings can be done on not only .jar and .aar files but also raw
10+
.java code. This will allow developers of bindings or applications
11+
to write custom API's for underlying bindings and expose them
12+
easily to the developer.
13+
14+
Applications already had the ability to include `AndroidJavaSource`
15+
items in them, these would then be compiled into the final `classes.dex`.
16+
However this custom java source code was not "bound" and if users wanted
17+
to call these classes they would need to do that manually.
18+
19+
The `AndroidJavaSource` Item Group now supports a `Bind` attribute. This
20+
will instruct the build system to not only compile the code but also
21+
produce a C# API for it.
22+
23+
```xml
24+
<ItemGroup>
25+
<AndroidJavaSource Include="MyClass.java" Bind="True" />
26+
</ItemGroup>
27+
```
28+
29+
The `Bind` attribute will default to `true` when in binding projects.
30+
But for applications it will need to be set manually.
31+
32+
# Example
33+
34+
Consider the following Java code.
35+
36+
```java
37+
package com.xamarin.test;
38+
39+
class MyClass {
40+
public bool IsDave (String name)
41+
{
42+
return name.equals ("Dave");
43+
}
44+
}
45+
```
46+
47+
We want to bind this into a C# API in our app project.
48+
So we can add a new `AndroidJavaSource` items with the `Bind`
49+
attribute set to `true`.
50+
51+
```xml
52+
<ItemGroup>
53+
<AndroidJavaSource Include="MyClass.java" Bind="True" />
54+
</ItemGroup>
55+
```
56+
57+
When we build the app, the java code will be compiled into a
58+
.jar file which matches the application project name.
59+
This .jar will then be bound and the generated C# API will end
60+
up being something like this.
61+
62+
```csharp
63+
using System;
64+
65+
namespace Com.Xamarin.Test {
66+
public class MyClass : Java.Lang.Object {
67+
public bool IsDave (string name){
68+
// binding code
69+
}
70+
}
71+
}
72+
```
73+
74+
All this will happen before the main C# code is compiled. The binding
75+
code will be included in the C# compile process, so you can use the
76+
code directly in your app.
77+
78+
```csharp
79+
public class MainActivity : Activity {
80+
public override void OnCreate()
81+
{
82+
var c = new MyClass ();
83+
c.IsDave ("Bob");
84+
}
85+
}
86+
```
87+
88+
89+
# Limitations
90+
91+
You will be limited to standard java types and any types that
92+
are available in a .jar or .aar which you reference.
93+
94+
You CANNOT use Java Generics. The Binding process currently does not
95+
really support Java Generic very well. So stick to primitive types
96+
or normal classes as much as possible. This is so that you don't need
97+
to use the metadata.xml to alter the API of the Java class.

0 commit comments

Comments
 (0)