MAUI library for Native Android binding failing with aar files that have Java Interfaces

Arnold laishram 1 Reputation point
2024-07-04T11:55:27.93+00:00

I recently started working on MAUI Android binding library. I have created an aar file which has a simple Kotlin class and an Interface. This interface is implemented by the Kotlin class.

The simple Kotlin class

package
package com.app.mauitestlib

import android.util.Log

class MauiLibTest: MauiTestListener {


    fun start(initKey: String) {
        Log.d("MauiTest", "start called: ")
    }

    override fun onInit() {
        Log.d("MauiTest", "onInit")
    }

}
package

The simple Kotlin Interface


package com.app.mauitestlib

interface MauiTestListener {

    fun onInit()

}

Using Android Studio, I have generated mauitestlib-release.aar file. My requirement is to bind the aar file generated to MAUI library using net8.0-android target framework. I have the following setup for MAUI library

Screenshot 2024-07-04 at 4.57.02 PM

The wfx-maui-test.csproj looks like this.

<Project Sdk="Microsoft.NET.Sdk">

To build this library, I run the following commands,


dotnet clean wfx-maui-test.csproj
dotnet restore wfx-maui-test.csproj
dotnet build -c Release

This generates dll files without any error, but when i publish the library to nuget as library and use it in a MAUI Android project. It fails the following error.


obj/Debug/net8.0-android/android/src/mono/com/app/mauitestlib/MauiTestListenerImplementor.java(8,22): javac error JAVAC0000:  error: package com.app.mauitestlib does not exist

Upon debugging, I found that a cs file name Com.App.Mauitestlib.IMauiTestListener.cs is generated which has the below code.



[global::Android.Runtime.Register ("mono/com/app/mauitestlib/MauiTestListenerImplementor")]
internal sealed partial class IMauiTestListenerImplementor : global::Java.Lang.Object, IMauiTestListener {

}

This is internally registering java file name MauiTestListenerImplementor.java under the package mono/com/app/mauitestlib which is causing the issue.

How i make sure that the file is not registered under mono/com/app/mauitestlib package?

How can i make my maui library work with aar file that has Kotlin/Java interface?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,153 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 38,456 Reputation points Microsoft Vendor
    2024-07-05T03:08:21.5533333+00:00

    Hello,

    For how to Binding aar files in MAUI, you could refer to the AndroidLibrary method recommended in this official document.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.