dotnet / java-interop

Java.Interop provides open-source bindings of Java's Java Native Interface (JNI) for use with .NET managed languages such as C#
Other
189 stars 48 forks source link

[Hello-NativeAOTFromAndroid] Add NativeAOT+Android sample #1218

Closed jonpryor closed 2 months ago

jonpryor commented 2 months ago

Context: 2197579478152fbc815eb15195977f808cd6bde4

Commit 21975794 added samples/Hello-NativeAOTFromJNI, which demonstrated the use of NativeAOT to create a native library which could be loaded and used by a Java application.

What else supports loading native libraries for use by a "Java" environment? Android!

Take the core infrastructure from Hello-NativeAOTFromJNI, have the binary output target linux-bionic-arm64 -- the $(RuntimeIdentifier) for .NET using Android's "bionic" libc while not using .NET for Android -- and then use gradlew to package that native library into an Android application.

Add "degrees" of Android and Gradle Integration:

Update generator to support using Java.Base.dll as a referenced assembly for binding purposes, in particular by supporting the use of [JniTypeSignatureAttribute] on already bound types. No usable bindings of types in android.xml could be generated without this.

Update jcw-gen to appropriately support [JniTypeSignature(GenerateJavaPeer=false)] when determining the constructors that a Java Callable Wrapper should contain. Failure to do so meant that the JCW for MainActivity contained constructors that shouldn't be there, resulting in javac errors.

Update jcw-gen to and support JavaInterop1-style method overrides. Failure to do so meant that the JCW for MainActivity didn't declare an onCreate() method.