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

Expo Native Module带参数函数定义报错求助

Expo Native Module Kotlin代码构建错误:多参数函数类型不匹配

编写Kotlin代码实现Expo Native Module时,定义带多参数的pay函数,Gradle构建报类型不匹配错误,提示推断类型为多参数函数,但期望无参函数。

编写的Kotlin代码

package expo.modules.oppwa

import android.content.Context

import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
import com.oppwa.mobile.connect.provider.*
import com.oppwa.mobile.connect.payment.*
import com.oppwa.mobile.connect.payment.card.CardPaymentParams
import com.oppwa.mobile.connect.exception.*

class OppwaModule : Module(), ITransactionListener {

  override fun definition() = ModuleDefinition {

    Name("Oppwa")

    Function("validateCard") { cardNumber: String ->
      CardPaymentParams.isNumberValid(cardNumber, true)
    }

    Function("pay") { checkoutId: String, cardBrand: String, cardNumber: String, cardHolder: String, cardExpiryMonth: String, cardExpiryYear: String, cardCVV: String, callbackUrl: String, isLive: Boolean ->  // Line 23
      try {
        // do stuff here
        "ok"  // return ok on sucess
      } catch (e: PaymentException) {
        e.message  // return error message on failure
      }
    }
  }
}

Gradle构建错误日志

[stderr] 
e: file:///home/expo/workingdir/build/modules/oppwa/android/src/main/java/expo/modules/oppwa/OppwaModule.kt:23:21 Type mismatch: inferred type is (String, String, String, String, String, String, String, String, Boolean) -> Any? but () -> Any? was expected

[stderr] 
e: file:///home/expo/workingdir/build/modules/oppwa/android/src/main/java/expo/modules/oppwa/OppwaModule.kt:23:23 Expected no parameters 

编辑1:已修复首个错误


解决方案

这个错误是因为Expo Kotlin Native Module定义多参数函数时,不能直接传递多参数lambda给Function方法,需要显式通过Parameters声明参数类型,再用Handler接收参数。修改pay函数的定义如下:

Function("pay") {
    Parameters(
        string("checkoutId"),
        string("cardBrand"),
        string("cardNumber"),
        string("cardHolder"),
        string("cardExpiryMonth"),
        string("cardExpiryYear"),
        string("cardCVV"),
        string("callbackUrl"),
        boolean("isLive")
    )
    Handler { checkoutId: String, cardBrand: String, cardNumber: String, cardHolder: String, cardExpiryMonth: String, cardExpiryYear: String, cardCVV: String, callbackUrl: String, isLive: Boolean ->
        try {
            // do stuff here
            "ok"  // return ok on sucess
        } catch (e: PaymentException) {
            e.message  // return error message on failure
        }
    }
}
  • Parameters块用来声明每个参数的类型和名称,对应JS端调用时传入的参数。
  • Handler块接收实际的参数值,执行业务逻辑并返回结果。

修改后编译器就能正确识别多参数函数的定义,解决类型不匹配问题。

内容的提问来源于stack exchange,提问作者Obay Abd-Algader

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:53:10