android 未启动Gradle buildSrc类

vjrehmav  于 2023-01-03  发布在  Android
关注(0)|答案(1)|浏览(150)

我在buildSrc目录中有一个简单的Properties类,没有任何附加的package子目录。
生成源代码/源代码/main/Kotlin/属性.kt
该类是一个简单的属性实用程序文件,用于将从文件获取的属性全局化到项目的其余部分:

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
    }
}

此属性对象在不同模块的build.gradle.kts配置中使用。由于Properties对象在KotlinbuildSrc目录的根目录下编译,因此未使用显式导入:

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

但是我经常遇到构建错误,指出Properties对象不存在,好像它在模块的构建脚本之前没有编译过。
通常有一些干净的缓存,这是解决,但我试图在一个新的机器上运行项目,它根本不会工作,我得到一些LocationAwareException
你知道会出什么问题吗?

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

构建源/构建.分级.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")
}
7gcisfzg

7gcisfzg1#

好了,现在我明白了,buildSrc是用于配置和对象,而不是绑定到项目本身。因为BuildSrc和项目构建是单独编译的。
尝试从BuildSrc初始化使用对属性文件的引用是错误的(尽管可以通过将属性移动到同一文件夹来实现)。
我所做的是更改Properties的初始化方式,以便项目设置项目路径(我无法找到从KotlinObject获取静态项目引用的方法)

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
    }
}

然后在根build.gradle.kts上添加:

ProjectProperties.setPath(project.projectDir)

相关问题