简体   繁体   English

将 build.gradle 迁移到 build.gradle.Kts:无法解析 Properties 类

[英]Migrating the build.gradle to build.gradle.Kts : Not able to resolve Properties class

While converting the build.gradle to build.gradle.kts is a manual process, I'm struggling in conversion in the below piece of code.虽然将build.gradle转换为build.gradle.kts是一个手动过程,但我在下面的代码段中努力转换。

I tried with invalidating the cache and restarting the studio many times.我多次尝试使缓存无效并重新启动工作室。 However, android.varinatFilter is not recognized.但是,无法识别 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)
    }
}

Properties class of Java.util.Properties dependency doesn't get resolved in .kts file also the FileInputStream class of Java.io is not recognized. Java.util.Properties依赖项的属性类未在.kts文件中解析,也无法识别Java.io的 FileInputStream 类。

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

Also while applying the kotlin annotation processor同样在应用 kotlin 注释处理器时

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

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

doesn't work and returns compile-time error.不起作用并返回编译时错误。

Any help would be appreciated.任何帮助,将不胜感激。

UPDATE更新

Properties class of Java.util.Properties dependency doesn't get resolved in .kts file also the FileInputStream class of Java.io is not recognized. Java.util.Properties依赖项的属性类未在.kts文件中解析,也无法识别Java.io的 FileInputStream 类。

This will get resolved with Invalidate cache and Restart.(Start refactoring project level gradle then settings.gradle and then app.gradle file in sequence)这将通过 Invalidate cache 和 Restart 解决。(开始重构项目级别的 gradle,然后是 settings.gradle,然后是 app.gradle 文件)

For kapt {'androidx.lifecycle:lifecycle-common-java8:2.1.0'} - please use double quotes, eg kapt {"androidx.lifecycle:lifecycle-common-java8:2.1.0"} , please check details here .对于kapt {'androidx.lifecycle:lifecycle-common-java8:2.1.0'} - 请使用双引号,例如kapt {"androidx.lifecycle:lifecycle-common-java8:2.1.0"} ,请在此处查看详细信息

Please also use the following syntax for method:还请对方法使用以下语法:

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

/***/

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

Changes:变化:

  • You need imports with java packages at the beginning of the file.您需要在文件开头使用 java 包进行导入。
  • Use fun instead of def使用fun代替def
  • Parameter types are required for methods, use ':' for this - path: String方法需要参数类型,为此使用“:” - path: String
  • new keyword is not needed不需要new关键字
  • Variable declaration can started with val , eg if compiler is able to understand the type, you don't need to enter it manually.变量声明可以以val开头,例如,如果编译器能够理解类型,则无需手动输入。
  • Return type is mandatory if your result is not Unit .如果您的结果不是Unit返回类型是强制性的

For filter - I didn't worked with this.对于过滤器 - 我没有处理过这个。 However please consider:但是请考虑:

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

That would be那将是

android.variantFilter {
    if (buildType.name == "release" && flavors[0].name == "development") {
        ignore = true
    }
    if (buildType.name == "debug" && flavors[0].name == "production") {
        ignore = true
    }
}

although I think that a more correct approach for虽然我认为更正确的方法是

flavors[0].name = "xyz"

should be应该

flavors.map { it.name }.contains("xyz")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM