Home > Net >  Gradle buildSrc classes not initated
Gradle buildSrc classes not initated

Time:01-05

I have a simple Properties class in the buildSrc directory, without any additional package subdirectory.

buildSrc/src/main/kotlin/Properties.kt

The class is a simple properties util file to make global the properties taken from files to the rest of the project:

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

object Properties {

    val myProps = getProperties("myProps.properties")

    private fun getProperties(path: String): Properties {
        val props = Properties()
        val propertiesFile = File(path)
        props.load(FileInputStream(propertiesFile))
        return props
    }
}

This property object is used accross different modules at their build.gradle.kts configuration. No explicit import is used because the Properties object is compiled at the root of kotlin buildSrc directory:

android {
    buildTypes {
        getByName("release") {
            buildConfigField("String", "SOME_PROP", Properties.myProps.getProperty("MY_PROP"))
          ...

But I constantly face build errors mentioning that the Properties object doesn't exist, as if it wasn't compiled before the modules' build script.

Normally with some clean caches this is solved but I tried to run project in a new machine and it simply won't work and I get some LocationAwareException.

Any idea of what can be wrong?

org.gradle.internal.exceptions.LocationAwareException: Build file '/Users/hector/MyAwesomeProject/myModule/build.gradle.kts' line: 23
Could not initialize class Properties
    at org.gradle.kotlin.dsl.execution.InterpreterKt$locationAwareExceptionFor$2.invoke(Interpreter.kt:600)
    at org.gradle.kotlin.dsl.execution.InterpreterKt.locationAwareExceptionFor(Interpreter.kt:607)
    at org.gradle.kotlin.dsl.execution.InterpreterKt.locationAwareExceptionHandlingFor(Interpreter.kt:573)
    at org.gradle.kotlin.dsl.execution.InterpreterKt.access$locationAwareExceptionHandlingFor(Interpreter.kt:1)
    at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost.handleScriptException(Interpreter.kt:409)
    at Program.execute(Unknown Source)
    at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost.eval(Interpreter.kt:531)
    at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost.evaluateSecondStageOf(Interpreter.kt:455)
    at Program.execute(Unknown Source)
    at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost.eval(Interpreter.kt:531)
    at org.gradle.kotlin.dsl.execution.Interpreter.eval(Interpreter.kt:204)
    at org.gradle.kotlin.dsl.provider.StandardKotlinScriptEvaluator.evaluate(KotlinScriptEvaluator.kt:114)
    at org.gradle.kotlin.dsl.provider.KotlinScriptPluginFactory$create$1.invoke(KotlinScriptPluginFactory.kt:51)
    at org.gradle.kotlin.dsl.provider.KotlinScriptPluginFactory$create$1.invoke(KotlinScriptPluginFactory.kt:36)
    at org.gradle.kotlin.dsl.provider.KotlinScriptPlugin.apply(KotlinScriptPlugin.kt:34)
    at org.gradle.configuration.BuildOperationScriptPlugin$1.run(BuildOperationScriptPlugin.java:65)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$1.execute(DefaultBuildOperationRunner.java:29)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$1.execute(DefaultBuildOperationRunner.java:26)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
    
Caused by: java.lang.NoClassDefFoundError: Could not initialize class Properties
    at Build_gradle$1$1.invoke(build.gradle.kts:23)
    at Build_gradle$1$1.invoke(build.gradle.kts:1)
    at com.android.build.gradle.internal.dsl.CommonExtensionImpl.defaultConfig(CommonExtensionImpl.kt:200)
    at com.android.build.gradle.internal.dsl.BaseAppModuleExtension.defaultConfig(BaseAppModuleExtension.kt)
    at Build_gradle$1.execute(build.gradle.kts:18)
    at Build_gradle$1.execute(build.gradle.kts:1)
    at org.gradle.internal.extensibility.ExtensionsStorage$ExtensionHolder.configure(ExtensionsStorage.java:173)
    at org.gradle.internal.extensibility.ExtensionsStorage.configureExtension(ExtensionsStorage.java:64)
    at org.gradle.internal.extensibility.DefaultConvention.configure(DefaultConvention.java:194)
    at org.gradle.kotlin.dsl.Accessors377twfxlhpj2n65rquy9ybeqsKt.android(Unknown Source)
    at Build_gradle.<init>(build.gradle.kts:14)
    ... 174 more

buildSrc/build.gradle.kts

plugins {
    `kotlin-dsl`
    `kotlin-dsl-precompiled-script-plugins`
    java
}

repositories {
    google()
    mavenCentral()
    jcenter()
}

dependencies {
    // android gradle plugin, required by custom plugin
    implementation("com.android.tools.build:gradle:7.0.3")
    // kotlin plugin, required by custom plugin
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0")
}

CodePudding user response:

Ok now I see, buildSrc is meant for configurations and objects that are not tied to the project itself. as the BuildSrc and Project builds are compiled separately.

It was an error to try to use reference to properties files from the BuildSrc initialization (although it could be possible by moving properties to same folder).

What I did was to change how Properties were initiated so that the project sets the project path (I couldn't find a way to get a static project reference from kotlin Object)

import java.io.File
import java.io.FileInputStream
import java.util.Properties
import java.nio.file.Paths

object ProjectProperties {

    val myProps by lazy { getProperties("myProps.properties") }

    private lateinit var project: File

    fun setPath(path: File) {
        project = path
    }

    private fun getProperties(path: String): Properties {
        val props = Properties()
        val propertiesFile = File(project.getAbsolutePath(), path)
        println("Getting property at: ${path}")

        props.load(FileInputStream(propertiesFile))
        return props
    }
}

Then on the root build.gradle.kts add:

ProjectProperties.setPath(project.projectDir)
  • Related