You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用MAUI封装.NET类库编译为AAR/XCFramework用于Flutter原生项目?

用MAUI构建.NET类库的原生封装包(AAR/Framework)并集成到Flutter

一、前置准备

  • 安装.NET 7+ SDK及MAUI工作负载:执行 dotnet workload install maui
  • 确认你的三个.NET类库为.NET Standard 2.0+或.NET 7+版本,且无Windows专属API依赖

二、创建MAUI封装类库

  1. 终端执行创建命令:dotnet new mauilib -n DotNetNativeWrapper
  2. 将你的三个.dll添加为项目引用:编辑项目的.csproj文件,加入以下节点(替换为实际类库路径):
<ItemGroup>
  <ProjectReference Include="..\YourLibrary1\YourLibrary1.csproj" />
  <ProjectReference Include="..\YourLibrary2\YourLibrary2.csproj" />
  <ProjectReference Include="..\YourLibrary3\YourLibrary3.csproj" />
</ItemGroup>

三、编写原生友好的封装层

在MAUI类库中创建公开类,将原.dll的功能封装成原生平台可调用的简单接口,避免复杂自定义类型传递(如需复杂数据可转成JSON字符串):

namespace DotNetNativeWrapper
{
    public class BusinessService
    {
        private readonly OriginalLib1.CoreService _coreService;

        public BusinessService()
        {
            _coreService = new OriginalLib1.CoreService();
        }

        public string ProcessData(string input)
        {
            return _coreService.Process(input);
        }

        public int CalculateTotal(int[] numbers)
        {
            return OriginalLib2.Calculator.Sum(numbers);
        }
    }
}

四、编译生成Android AAR

  • 切换到MAUI类库目录,执行:dotnet build -f net7.0-android
  • 编译产物路径:bin/Debug/net7.0-android/DotNetNativeWrapper.aar(Release版本替换Debug为Release)

五、编译生成iOS Framework

  • 需在Mac环境下操作,切换到MAUI类库目录,执行:dotnet build -f net7.0-ios
  • 编译产物路径:bin/Debug/net7.0-ios/DotNetNativeWrapper.framework

六、Flutter项目集成

Android端

  1. 将AAR文件复制到Flutter项目的android/app/libs目录
  2. 编辑android/app/build.gradle,添加依赖:
dependencies {
    implementation files('libs/DotNetNativeWrapper.aar')
}
  1. 通过MethodChannel调用封装方法(示例Kotlin代码):
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.dotnetnativewrapper.BusinessService

class MainActivity : FlutterActivity() {
    private val CHANNEL = "com.example/dotnet_service"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            when (call.method) {
                "processData" -> {
                    val input = call.argument<String>("input") ?: ""
                    val service = BusinessService()
                    result.success(service.processData(input))
                }
                else -> result.notImplemented()
            }
        }
    }
}

iOS端

  1. 将Framework复制到Flutter项目的ios/Frameworks目录(无此目录则新建)
  2. 打开ios/Runner.xcworkspace,将Framework拖入项目,勾选"Copy items if needed",并在Target的「Frameworks, Libraries, and Embedded Content」中设置为「Embed & Sign」
  3. 通过MethodChannel调用封装方法(示例Swift代码):
import UIKit
import Flutter
import DotNetNativeWrapper

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    private let CHANNEL = "com.example/dotnet_service"

    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let controller = window?.rootViewController as! FlutterViewController
        let channel = FlutterMethodChannel(name: CHANNEL, binaryMessenger: controller.binaryMessenger)
        channel.setMethodCallHandler { call, result in
            switch call.method {
            case "processData":
                let input = call.arguments?["input"] as? String ?? ""
                let service = BusinessService()
                result.success(service.processData(input))
            default:
                result.notImplemented()
            }
        }
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

Flutter Dart端调用

import 'package:flutter/services.dart';

class DotNetService {
  static const MethodChannel _channel = MethodChannel('com.example/dotnet_service');

  static Future<String?> processData(String input) async {
    final String? result = await _channel.invokeMethod('processData', {'input': input});
    return result;
  }
}

内容的提问来源于stack exchange,提问作者ASP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 15:06:08