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

Flutter集成UPI支付遇Kotlin版本报错,求解决方案

解决UPI包Kotlin版本兼容问题

一、彻底检查Gradle配置的Kotlin版本

问题大概率出在多文件配置不一致,需逐一确认:

  • 打开项目根目录的build.gradle(android文件夹外的那个),将ext.kotlin_version设置为稳定版1.8.x(避免用2.0.x预览版,部分第三方包不兼容):
    buildscript {
        ext.kotlin_version = '1.8.20'
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath "com.android.tools.build:gradle:7.4.2"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
  • 打开android/app/build.gradle,确认kotlin插件引用的是上述变量,无硬编码旧版本:
    apply plugin: 'kotlin-android'
    // 确保此处未单独写死版本号
    
  • 检查gradle.properties文件,若存在kotlin.version类配置,同步为相同版本:
    kotlin.version=1.8.20
    

二、清理缓存与重新构建

  1. 执行终端命令:
    flutter clean
    flutter pub get
    
  2. 删除项目android/.gradle目录,或通过Android Studio的File > Invalidate Caches / Restart清理缓存
  3. 重新运行项目

三、修复自定义包的配置

如果你复刻了upi_pay/flutter_upi包:

  • 打开自定义包的android/build.gradle,确保其ext.kotlin_version与主项目一致,且无硬编码低于1.5.40的版本
  • 确认包的pubspec.yaml未依赖其他强制旧Kotlin版本的包
  • 本地引用自定义包时,确保路径正确:
    dependencies:
      flutter_upi:
        path: ./custom_flutter_upi
    

四、替代方案:绕过第三方包直接集成UPI

若以上方法均无效,直接通过原生Intent调用UPI应用(无第三方包依赖,完全可控):

  1. Flutter端创建MethodChannel,调用Android原生代码:
    class _PaymentPageState extends State<PaymentPage> {
      static const platform = MethodChannel('upi_payment');
    
      Future<void> _startUpiTransfer() async {
        try {
          await platform.invokeMethod('initiateTransfer', {
            'upiId': 'recipient@upi',
            'name': '收款方名称',
            'amount': '50.00',
            'note': 'P2P转账'
          });
        } on PlatformException catch (e) {
          print('错误:${e.message}');
        }
      }
    
      @override
      void initState() {
        super.initState();
        platform.setMethodCallHandler((call) async {
          if (call.method == 'transferResult') {
            String response = call.arguments;
            // 解析UPI响应字符串,判断交易状态
            print('交易响应:$response');
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: _startUpiTransfer,
              child: const Text('发起UPI转账'),
            ),
          ),
        );
      }
    }
    
  2. Android原生端构造UPI请求Intent:
    class MainActivity : FlutterActivity() {
        private val CHANNEL = "upi_payment"
        private val UPI_REQUEST_CODE = 101
    
        override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
            super.configureFlutterEngine(flutterEngine)
            MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
                if (call.method == "initiateTransfer") {
                    val upiId = call.argument<String>("upiId")
                    val name = call.argument<String>("name")
                    val amount = call.argument<String>("amount")
                    val note = call.argument<String>("note")
                    launchUpiIntent(upiId, name, amount, note, result)
                } else {
                    result.notImplemented()
                }
            }
        }
    
        private fun launchUpiIntent(upiId: String?, name: String?, amount: String?, note: String?, result: MethodChannel.Result) {
            val uri = Uri.parse("upi://pay").buildUpon()
                .appendQueryParameter("pa", upiId)
                .appendQueryParameter("pn", name)
                .appendQueryParameter("am", amount)
                .appendQueryParameter("cu", "INR")
                .appendQueryParameter("tn", note)
                .build()
    
            val intent = Intent(Intent.ACTION_VIEW, uri)
            val chooser = Intent.createChooser(intent, "选择UPI应用")
            if (packageManager.resolveActivity(chooser, 0) != null) {
                startActivityForResult(chooser, UPI_REQUEST_CODE)
            } else {
                result.error("NO_UPI_APP", "未安装任何UPI应用", null)
            }
        }
    
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == UPI_REQUEST_CODE) {
                val response = data?.getStringExtra("response")
                val channel = MethodChannel(flutterEngine?.dartExecutor?.binaryMessenger, CHANNEL)
                channel.invokeMethod("transferResult", response)
            }
        }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 21:53:12