gpt4 book ai didi

android - 将 build.gradle 迁移到 build.gradle.Kts : Not able to resolve Properties class

转载 作者:行者123 更新时间:2023-12-03 03:17:21 26 4
gpt4 key购买 nike

虽然将 build.gradle 转换为 build.gradle.kts 是一个手动过程,但我在下面的代码中遇到了困难。

我多次尝试使缓存无效并重新启动工作室。但是,无法识别 android.varinatFilter。

android.variantFilter { variant ->
if (variant.buildType.name == 'release'
&& variant.getFlavors().get(0).name == 'development') {
variant.setIgnore(true)
}
if (variant.buildType.name == 'debug'
&& variant.getFlavors().get(0).name == 'production') {
variant.setIgnore(true)
}
}

Java.util.Properties 依赖项的属性类在 .kts 文件以及 Java.io 的 FileInputStream 类中未得到解析无法识别。

 def getProps(path) {
Properties props = new Properties()
props.load(new FileInputStream(file(path)))
return props
}

同时应用 kotlin 注释处理器

kapt 'androidx.lifecycle:lifecycle-common-java8:2.1.0' To

kapt {'androidx.lifecycle:lifecycle-common-java8:2.1.0'}

不起作用并返回编译时错误。

如有任何帮助,我们将不胜感激。

更新

Properties class of Java.util.Properties dependency doesn't get resolved in .kts file also the FileInputStream class of Java.io is not recognized.

这将通过使缓存无效并重新启动来解决。(开始重构项目级别的 gradle,然后是 settings.gradle,然后是 app.gradle 文件)

最佳答案

对于 kapt {'androidx.lifecycle:lifecycle-common-java8:2.1.0'} - 请使用双引号,例如kapt {"androidx.lifecycle:lifecycle-common-java8:2.1.0"},请check details here .

方法也请使用以下语法:

import java.io.FileInputStream
import java.util.Properties

/***/

fun getProps(path: String): Properties {
val props = Properties()
props.load(FileInputStream(file(path)))
return props
}

变化:

  • 您需要在文件开头导入 java 包。
  • 使用 fun 而不是 def
  • 方法需要参数类型,为此使用“:” - path: String
  • 不需要新的关键字
  • Variable declaration可以以 val 开头,例如如果编译器能够理解类型,则无需手动输入。
  • Return type is mandatory如果您的结果不是 Unit

对于过滤器 - 我没有使用它。但是请考虑:

  • 将引号 ' 替换为 "
  • variant.getFlavors().get(0).name 替换为 variant.flavors[0].name
  • variant.setIgnore(true) 替换为 variant.ignore=true

关于android - 将 build.gradle 迁移到 build.gradle.Kts : Not able to resolve Properties class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com