格式化字符串 res 时,gradle 插件 lint 错误

lit*_*nal 4 android lint android-gradle-plugin

我在下面有一个字符串 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)
Run Code Online (Sandbox Code Playgroud)

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

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))
Run Code Online (Sandbox Code Playgroud)

但它在 AGP 3.0.1 中没有错误。

Kum*_*uma 11

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

在您的情况下,您可以尝试:

val total : Double = it.total
applicationContext.getString(R.string.amount_format, total)
Run Code Online (Sandbox Code Playgroud)

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

  • 这仍然是 Android Gradle Plugin 4.0.1 中的一个错误,这个解决方案仍然有效 (2认同)