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

Flutter中如何获取Android和iOS设备的CPU架构信息?

获取Android和iOS设备CPU架构信息的可行方案

如果pub.dev上的第三方包无法正常工作,直接通过MethodChannel调用原生API是最可靠的解决方式,以下是具体实现步骤:

Flutter 端代码

创建MethodChannel并封装获取架构的工具类:

import 'package:flutter/services.dart';

class CpuArchUtils {
  static const MethodChannel _channel = MethodChannel('cpu_arch_channel');

  static Future<List<String>?> getCpuArchitectures() async {
    try {
      final List<dynamic>? result = await _channel.invokeMethod('getCpuArch');
      return result?.cast<String>();
    } on PlatformException catch (e) {
      print('获取CPU架构失败: ${e.message}');
      return null;
    }
  }
}

Android 端原生实现(Kotlin)

在MainActivity.kt中添加MethodChannel的处理逻辑:

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
    private val CHANNEL = "cpu_arch_channel"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "getCpuArch") {
                // 返回设备支持的所有CPU架构列表,兼容性覆盖全Android版本
                val archs = android.os.Build.SUPPORTED_ABIS.toList()
                result.success(archs)
            } else {
                result.notImplemented()
            }
        }
    }
}

iOS 端原生实现(Swift)

在AppDelegate.swift中添加MethodChannel处理:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
        let channel = FlutterMethodChannel(name: "cpu_arch_channel", binaryMessenger: controller.binaryMessenger)
        channel.setMethodCallHandler({ [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
            guard call.method == "getCpuArch" else {
                result(FlutterMethodNotImplemented)
                return
            }
            // 获取简洁架构标识(如arm64、x86_64)
            let basicArch = ProcessInfo.processInfo.machine
            // 获取详细CPU型号(如Apple A15 Bionic)
            var size = 0
            sysctlbyname("machdep.cpu.brand_string", nil, &size, nil, 0)
            var brandBuffer = [CChar](repeating: 0, count: size)
            sysctlbyname("machdep.cpu.brand_string", &brandBuffer, &size, nil, 0)
            let detailArch = String(cString: brandBuffer)
            result([basicArch, detailArch])
        })
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

使用示例

在Flutter页面中调用工具类获取信息:

void _fetchCpuArch() async {
  var archInfo = await CpuArchUtils.getCpuArchitectures();
  print("CPU架构信息: $archInfo");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 15:22:22