ThomasMalloch13
ThomasMalloch13

Reputation: 113

Maui Amazon Device Messaging Android Library Binding

I want to make an android binding library for dotnet maui that uses the amazon-device-messaging-1.2.0.jar (https://developer.amazon.com/docs/adm/set-up.html). I've built the project and the bindings seem to be working and everything compiles and runs, but once the app actually starts it throws this error:

'Stub! You are bundling a stubbed jar in the apk! Please move it to the classpath instead.'

My android binding .csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0-android</TargetFramework>
    <SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <InputJar Include="amazon-device-messaging-1.2.0.jar" />
  </ItemGroup>
</Project>

My Metadata.xml file looks like this:

<metadata>
    <!-- Package Name -->
    <attr path="/api/package[@name='com.amazon.device.messaging']" name="managedName">Amazon.Device.Messaging</attr>

    <!-- ADM -->
    <attr path="/api/package[@name='com.amazon.device.messaging']/class[@name='ADM']" name="visibility">public</attr>

    <!-- ADMMessageHandlerBase -->
    <attr path="/api/package[@name='com.amazon.device.messaging']/class[@name='ADMMessageHandlerBase']" name="extends">mono.android.app.IntentService</attr>
    <attr path="/api/package[@name='com.amazon.device.messaging']/class[@name='ADMMessageHandlerBase']" name="visibility">public</attr>

    <!-- ADMMessageHandlerJobBase -->
    <!-- ADMMessageReceiver -->
</metadata>

I'm including the generated dll in my maui project like this:

<ItemGroup>
  <Reference Include="AmazonDeviceMessaging">
    <HintPath>..\..\AmazonDeviceMessaging\AmazonDeviceMessaging\bin\Release\net8.0-android\AmazonDeviceMessaging.dll</HintPath>
  </Reference>
</ItemGroup>

I have also tried building my binding library with AndroidLibrary (all options tried with Bind and Pack), EmbeddedJar, InputJar, LibraryProjectZip, and ReferenceJar.

Does anyone know how to get my maui project to stop throwing this error?

Upvotes: 0

Views: 103

Answers (2)

ThomasMalloch13
ThomasMalloch13

Reputation: 113

As mentioned by Liyun Zhang in another answer, I was referencing the .dll directly instead of referencing the project as described in the documentation. Referencing the project directly though resulted in another error that my Maui project and my Android Binding project were not compatible.

Error       Project '..\AndroidBinding1\AndroidBinding1.csproj' targets 'net8.0-android'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v8.0'. MauiApp1    C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets   1867    

So I worked around this issue in another way. Here's how I did it:

  • I copied the .cs files generated by the android binding project from AndroidBinding/obj/generated/src to my maui project
  • I copied the .jar file to my maui project in Platforms/Android/Resources/libs and marked is as an AndroidExternalJavaLibrary

My code now compiles without error and the classes from the .jar stub are able to be used

Upvotes: 1

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14244

You don't have to reference the dll directly. You can just add the reference of the binding library project.

And I refer to it in my Maui project:

<ItemGroup  Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
  <ProjectReference Include="..\AmazonJar\AmazonJar.csproj" />
</ItemGroup>

Upvotes: 1

Related Questions