kotlin Flutter包:如何获取父级BuildConfig.java类值?

2sbarzqh  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(220)

我正在实现一个flutter包,我正在尝试检索值 从父应用程序的Android BuildConfig文件中调用,这可能吗?
我试过这样的代码:

val packageName = flutterActivity?.packageName
if (packageName != null) {
    val value = Class.forName(packageName + ".BuildConfig").getField("FLAVOR").get(null) as String
    print(value)
}

但找不到文件...

sgtfey8w

sgtfey8w1#

我使用以下方法解决问题:

private var flutterActivity: Activity? = null

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        this.flutterActivity = binding.activity
    }

    /// Get The Build Flavor config.
    private fun getFlavor(result: Result) {
        flutterActivity?.let {
            val pack = it.localClassName.split(".").dropLast(1).joinToString(".")
            val flavor =
                Class.forName(pack.plus(".BuildConfig")).getField("FLAVOR").get(null) as String
            result.success(flavor)
        } ?: run {
            result.success(null)
        }
    }

相关问题