Search code examples
androidgradlebuild.gradlegradle-kotlin-dsl

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.

I tried with invalidating the cache and restarting the studio many times. However, android.varinatFilter is not recognized.

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.

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

Also while applying the kotlin annotation processor

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.

This will get resolved with Invalidate cache and Restart.(Start refactoring project level gradle then settings.gradle and then app.gradle file in sequence)


Solution

  • For kapt {'androidx.lifecycle:lifecycle-common-java8:2.1.0'} - please use double quotes, e.g. kapt {"androidx.lifecycle:lifecycle-common-java8:2.1.0"}, please check details here.

    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.
    • Use fun instead of def
    • Parameter types are required for methods, use ':' for this - path: String
    • new keyword is not needed
    • Variable declaration can started with val, e.g. if compiler is able to understand the type, you don't need to enter it manually.
    • Return type is mandatory if your result is not 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].name
    • Replacing variant.setIgnore(true) to variant.ignore=true