安卓绑定库

This commit is contained in:
denggaofeng 2026-01-23 10:13:10 +08:00
parent e26ad1b8c6
commit 6979bf6211
6 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,48 @@
Additions allow you to add arbitrary C# to the generated classes
before they are compiled. This can be helpful for providing convenience
methods or adding pure C# classes.
== Adding Methods to Generated Classes ==
Let's say the library being bound has a Rectangle class with a constructor
that takes an x and y position, and a width and length size. It will look like
this:
public partial class Rectangle
{
public Rectangle (int x, int y, int width, int height)
{
// JNI bindings
}
}
Imagine we want to add a constructor to this class that takes a Point and
Size structure instead of 4 ints. We can add a new file called Rectangle.cs
with a partial class containing our new method:
public partial class Rectangle
{
public Rectangle (Point location, Size size) :
this (location.X, location.Y, size.Width, size.Height)
{
}
}
At compile time, the additions class will be added to the generated class
and the final assembly will a Rectangle class with both constructors.
== Adding C# Classes ==
Another thing that can be done is adding fully C# managed classes to the
generated library. In the above example, let's assume that there isn't a
Point class available in Java or our library. The one we create doesn't need
to interact with Java, so we'll create it like a normal class in C#.
By adding a Point.cs file with this class, it will end up in the binding library:
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}

View File

@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0-android</TargetFramework>
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!--
Enable trim analyzers for Android class libraries.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
-->
<IsTrimmable>true</IsTrimmable>
<!--
NOTE: you can simply add .aar or .jar files in this directory to be included in the project.
To learn more, see: https://learn.microsoft.com/dotnet/maui/migration/android-binding-projects
-->
</PropertyGroup>
<!-- 添加必要的 NuGet 包引用以解决缺失的类型 -->
<ItemGroup>
<!-- Kotlin 支持 -->
<PackageReference Include="Xamarin.Kotlin.StdLib" Version="2.0.21.3" />
<PackageReference Include="Xamarin.KotlinX.Coroutines.Core" Version="1.9.0.3" />
<PackageReference Include="Xamarin.KotlinX.Serialization.Core" Version="1.7.3.4" />
<!-- AndroidX 支持 -->
<PackageReference Include="Xamarin.AndroidX.Activity" Version="1.10.1.1" />
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.7.0.6" />
<PackageReference Include="Xamarin.AndroidX.Core" Version="1.16.0.1" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.Runtime" Version="2.8.7.3" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.ViewModel" Version="2.8.7.3" />
<!-- 其他可能需要的依赖 -->
<PackageReference Include="Xamarin.AndroidX.Collection" Version="1.5.0.1" />
<PackageReference Include="Xamarin.AndroidX.Annotation" Version="1.9.1.3" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,14 @@
<enum-field-mappings>
<!--
This example converts the constants Fragment_id, Fragment_name,
and Fragment_tag from android.support.v4.app.FragmentActivity.FragmentTag
to an enum called Android.Support.V4.App.FragmentTagType with values
Id, Name, and Tag.
<mapping jni-class="android/support/v4/app/FragmentActivity$FragmentTag" clr-enum-type="Android.Support.V4.App.FragmentTagType">
<field jni-name="Fragment_name" clr-name="Name" value="0" />
<field jni-name="Fragment_id" clr-name="Id" value="1" />
<field jni-name="Fragment_tag" clr-name="Tag" value="2" />
</mapping>
-->
</enum-field-mappings>

View File

@ -0,0 +1,13 @@
<enum-method-mappings>
<!--
This example changes the Java method:
android.support.v4.app.Fragment.SavedState.writeToParcel (int flags)
to be:
android.support.v4.app.Fragment.SavedState.writeToParcel (Android.OS.ParcelableWriteFlags flags)
when bound in C#.
<mapping jni-class="android/support/v4/app/Fragment.SavedState">
<method jni-name="writeToParcel" parameter="flags" clr-enum-type="Android.OS.ParcelableWriteFlags" />
</mapping>
-->
</enum-method-mappings>

View File

@ -0,0 +1,36 @@
<metadata>
<!--
This sample removes the class: android.support.v4.content.AsyncTaskLoader.LoadTask:
<remove-node path="/api/package[@name='android.support.v4.content']/class[@name='AsyncTaskLoader.LoadTask']" />
This sample removes the method: android.support.v4.content.CursorLoader.loadInBackground:
<remove-node path="/api/package[@name='android.support.v4.content']/class[@name='CursorLoader']/method[@name='loadInBackground']" />
-->
<!-- 添加对 Kotlin 类型的支持 -->
<attr path="/api/package[@name='com.lvcheng.lock.shared.nfc']" name="managedName">Com.Lvcheng.Lock.Shared.Nfc</attr>
<!-- 处理 Kotlin 集合类型 -->
<attr path="/api/package[@name='com.lvcheng.lock.shared.nfc']/class[@name='*']/method[@return='java.util.List']"
name="return">java.util.List</attr>
<!-- 处理 Kotlin 函数类型 -->
<remove-node path="/api/package[@name='com.lvcheng.lock.shared.nfc']/interface[@name='*']/method[contains(@name, '$')]" />
<!-- 处理 Kotlin 枚举类型 -->
<attr path="/api/package[@name='com.lvcheng.lock.shared.nfc']/class[@name='Ctrl']" name="managedType">Com.Lvcheng.Lock.Shared.Nfc.Ctrl</attr>
<attr path="/api/package[@name='com.lvcheng.lock.shared.nfc']/class[@name='CV']" name="managedType">Com.Lvcheng.Lock.Shared.Nfc.CV</attr>
<attr path="/api/package[@name='com.lvcheng.lock.shared.nfc']/class[@name='Met']" name="managedType">Com.Lvcheng.Lock.Shared.Nfc.Met</attr>
<attr path="/api/package[@name='com.lvcheng.lock.shared.nfc']/class[@name='Result']" name="managedType">Com.Lvcheng.Lock.Shared.Nfc.Result</attr>
<!-- 处理 ComponentActivity 类型 -->
<attr path="/api/package[@name='com.lvcheng.lock.shared.nfc.example']/class[@name='NFCHelper']"
name="managedType">Com.Lvcheng.Lock.Shared.Nfc.Example.NFCHelper</attr>
<!-- 移除包含美元符号的方法名Kotlin 生成的特殊方法) -->
<remove-node path="//method[contains(@name, '$')]" />
<!-- 处理 Kotlin UInt 类型 -->
<attr path="/api/package[@name='com.lvcheng.lock.shared.nfc']/class[@name='*']/method[@return='kotlin.UInt']"
name="return">int</attr>
</metadata>

BIN
AndroidBinding1/nfc-sdk.aar (Stored with Git LFS) Normal file

Binary file not shown.