Search code examples
gradlekotlindetekt

Define Kotlin's detekt version as extra property (ext)


Groovy allows definition of extra properties for the project in ext.

I wanted to define Detekt's version inside groovy's extra properties. Detekt is a static code analysis tool for Kotlin lang.

However when I do it in the folloing way:

buildscript {
    // testing, code-style, CI-tools
    ext.detect_code_analysis = '1.0.0.RC6-3' //change to 1.0.0 when available

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
plugins {
    id "io.gitlab.arturbosch.detekt" version "$detect_code_analysis"
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

detekt {
    version = "$detect_code_analysis"
    profile("main") {
        input = "$projectDir/app/src/main/java"
        config = "$projectDir/detekt-config.yml"
        filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
    }
}

it complaing with:

Error:(17, 0) startup failed:
        build file '/Users[...]build.gradle': 17: argument list must be exactly 1 literal non empty string

See https://docs.gradle.org/4.1/userguide/plugins.html#sec:plugins_block for information on the plugins {} block

@ line 17, column 5.
        id "io.gitlab.arturbosch.detekt" version "$detect_code_analysis"
^

1 error

Solution

  • “New style” Gradle plug-in definition (without including the full dependency on buildscript block) does not allow variables in the version.

    For more information refer to the section of documentation that’s referred to in the error message. There is a subsection “Limitations of the plugins dsl” that explains everything.

    If you want to continue using the variable version strings you need to return to the “old way” by using the apply plugin: “xxx” syntax.