Share via

can I create native c++ lib in .net MAUI? or .net andorid?

mc 7,186 Reputation points
2026-05-21T01:09:36.1033333+00:00

what I want is to create android to control the GPIO of the RK3568 which run the android system.

I searched that and get I have to create one Native c++ library and call it in android.

and doSystem.Device.GPIO can use in RK3568 android?

Developer technologies | .NET | .NET Multi-platform App UI

Answer accepted by question author

Nancy Vo (WICLOUD CORPORATION) 5,700 Reputation points Microsoft External Staff Moderator
2026-05-21T03:10:23.8933333+00:00

Hello @mc ,

Thanks for your question.

Yes, you can use native C++ libraries in both .NET MAUI and .NET Android. Unfortunately, System.Device.Gpio is not supported on Android. It is designed specifically for Linux-based IoT platforms.

I recommend calling native C++ via P/Invoke:

  • Compile your C++ code as a shared library (.so file) for ARM64.
  • Place the .so in your project under lib/arm64-v8a/.
  • Call it directly from C#:
using System.Runtime.InteropServices;

public static class GpioInterop
{
    private const string LibName = "libgpio_rk3568";

    [DllImport(LibName)]
    public static extern int gpio_export(int pin);

    [DllImport(LibName)]
    public static extern int gpio_set_direction(int pin, int direction);

    [DllImport(LibName)]
    public static extern int gpio_write(int pin, int value);
}

I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.

Was this answer helpful?

1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. AI answer

    2026-05-21T01:09:52.13+00:00

    Yes, a native C/C++ library can be used from .NET for Android / .NET MAUI, but System.Device.GPIO is not documented as supported on Android/RK3568 in the provided material.

    For integrating native code, the documented options are:

    1. Native Library Interop (recommended for MAUI/Android/iOS/Mac Catalyst)
      Native Library Interop is a pattern for accessing native SDKs from .NET MAUI and .NET for Android/iOS/Mac Catalyst:
      • Create a thin native wrapper in Java/Kotlin (Android Studio) or Objective‑C/Swift (Xcode) that exposes only the APIs needed (for example, GPIO operations on RK3568).
      • Then create .NET bindings to that wrapper and call it from .NET MAUI or .NET for Android.
      • This approach is designed specifically for scenarios where only a subset of a native SDK is needed and is easier to maintain than full bindings.
      The flow is:
      • Native wrapper project in Android Studio (Java/Kotlin) that talks to the GPIO APIs on RK3568.
      • .NET for Android binding library that binds that wrapper.
      • .NET MAUI app that references the binding library and calls the managed APIs.
    2. Using AndroidGradleProject in a .NET for Android project
      For .NET for Android (and by extension MAUI’s Android target), the SDK supports building Gradle projects and consuming their AAR output:
         <ItemGroup>
           <AndroidGradleProject Include="path/to/project/build.gradle.kts" ModuleName="mylibrary" />
         </ItemGroup>
      
      • The Gradle project can contain native code (C/C++) and Java/Kotlin wrappers.
      • The build produces an AAR, which is then added as an AndroidLibrary to be bound and consumed from .NET.
    3. Using the CommunityToolkit Native Library Interop template
      The CommunityToolkit provides a template that sets up:
      • .NET for Android and .NET for iOS binding libraries.
      • Android Studio and Xcode native wrapper projects. This template can be cloned and adapted to the RK3568 GPIO scenario, simplifying the setup of bindings and native projects.

    Regarding System.Device.GPIO:

    • The provided documentation does not state that System.Device.GPIO is supported on Android or specifically on RK3568 running Android.
    • The recommended pattern for accessing device‑specific hardware like GPIO in a MAUI/Android scenario, based on the context, is via native SDKs and Native Library Interop rather than System.Device.GPIO.

    Summary:

    • Yes, a native C/C++ (or Java/Kotlin wrapper over C/C++) library can be created and used from .NET for Android / .NET MAUI using Native Library Interop and/or AndroidGradleProject.
    • System.Device.GPIO usage on RK3568 Android is not covered in the provided documentation; the supported and documented path is to use native SDKs plus bindings/interop.

    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.