【问题标题】:gradle plugin lint error when format string res格式化字符串 res 时的 gradle 插件 lint 错误
【发布时间】:2023-03-10 23:20:01
【问题描述】:

我在下面有一个字符串 res 和 kotlin 数据类:

<string name="amount_format">¥%1$.2f</string>

data class TagAndTotal(
    @ColumnInfo(name = "tag_name") var tagName: String,
    @ColumnInfo(name = "total") var total: Float)

升级到 AGP 3.1.0 后运行 ./gradlew lint 时出现以下错误。

Error: Wrong argument type for formatting argument '#1' in 
amount_format: conversion is 'f', received <ErrorType> (argument #2 in 
method call) [StringFormatMatches]
        applicationContext.getString(R.string.amount_format, it.total))

但在 AGP 3.0.1 中没有错误。

【问题讨论】:

    标签: android android-gradle-plugin lint


    【解决方案1】:

    我遇到了同样的问题。声明一个明确的局部变量类型解决我的问题。

    在你的情况下,你可以试试:

    val total : Double = it.total
    applicationContext.getString(R.string.amount_format, total)
    

    我认为这是 AGP 中的一个错误

    【讨论】:

    • 这仍然是 Android Gradle Plugin 3.6.2 中的一个错误,这个解决方案仍然有效
    • 这仍然是 Android Gradle Plugin 4.0.1 中的一个错误,这个解决方案仍然有效
    【解决方案2】:

    这是 AGP 中的一个错误 - https://issuetracker.google.com/issues/169751286

    它已在 4.1.0-alpha08 中修复。

    在编写带有修复程序的最“稳定”版本是 4.1.0-rc03。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2022-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多