Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot choose between the following variants #600

Open
claudiogaalvao opened this issue Aug 24, 2022 · 6 comments
Open

Cannot choose between the following variants #600

claudiogaalvao opened this issue Aug 24, 2022 · 6 comments

Comments

@claudiogaalvao
Copy link

I'm trying to create a custom rule using Ktlint on a multi-module project. For that, I created a new module just for custom rules. This module is with this configuration:

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        minSdk 21
        targetSdk 32

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    compileOnly "com.pinterest.ktlint:ktlint-core:0.46.1"

    testImplementation "com.pinterest.ktlint:ktlint-core:0.46.1"
    testImplementation "com.pinterest.ktlint:ktlint-test:0.46.1"

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

I created the custom rule:

class ShouldNotDependAndroidFrameworkRule : Rule("should-not-depend-android-framework") {
    override fun visit(
        node: ASTNode,
        autoCorrect: Boolean,
        emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
    ) {
        if (isUseCaseClass(node) && hasAndroidFrameworkImport(node)) {
            emit(
                node.startOffset,
                "Use cases should not depend on Android framework",
                false
            )
        }
    }

    private fun isUseCaseClass(node: ASTNode): Boolean {
        return if (node.elementType == ElementType.CLASS) {
            val kotlinClass = node.psi as KtClass
            kotlinClass.name?.contains("UseCase") ?: false
        } else false
    }

    private fun hasAndroidFrameworkImport(node: ASTNode): Boolean {
        return if (node.elementType == ElementType.IMPORT_DIRECTIVE) {
            val importDirective = node.psi as KtImportDirective
            val path = importDirective.importPath?.pathStr
            path?.contains("android.") ?: false
        } else false
    }
}

And the RuleSetProvider:

class CustomRuleSetProvider : RuleSetProvider {

    override fun get() = RuleSet(ruleSetId, ShouldNotDependAndroidFrameworkRule())

    private companion object {
        const val ruleSetId: String = "custom-ktlint-rules"
    }
}

Then, registered creating a file with the name com.pinterest.ktlint.core.RuleSetProvider on the path /src/main/res/META-INF/services. The content of this file is the path for my CustomRuleSetProvider:

com.example.codeanalysis.CustomRuleSetProvider

My root build.gradle:

plugins{
id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
    id 'org.jlleitschuh.gradle.ktlint' version '10.3.0'
}

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

subprojects{
			apply plugin: "org.jlleitschuh.gradle.ktlint" // Version should be inherited from parent
			
			    repositories{
			// Required to download KtLint
			        mavenCentral()
			}
			
			// Optionally configure plugin
			ktlint{
				debug = true
			        android = true
			        filter{
			exclude("**/*Test.kt")
			}
    }
}

And on my build.gradle from the app I included this:

dependencies {
		// show error when I use it
    ktlintRuleset project(":codeanalysis")
    // ...
}

When I run the command ./gradlew ktlintcheck show this error:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:runKtlintCheckOverTestFixturesDebugSourceSet'.
> Could not resolve all task dependencies for configuration ':app:ktlintRuleset'.
   > Could not resolve project :codeanalysis.
     Required by:
         project :app
      > Cannot choose between the following variants of project :codeanalysis:
          - debugRuntimeElements
          - releaseRuntimeElements
        All of them match the consumer attributes:
          - Variant 'debugRuntimeElements' capability KtlintProject:codeanalysis:unspecified:
              - Unmatched attributes:
                  - Provides com.android.build.api.attributes.AgpVersionAttr '7.2.1' but the consumer didn't ask for it
                  - Provides com.android.build.api.attributes.BuildTypeAttr 'debug' but the consumer didn't ask for it
                  - Provides com.android.build.gradle.internal.attributes.VariantAttr 'debug' but the consumer didn't ask for it
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.jvm.environment 'android' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Provides org.jetbrains.kotlin.platform.type 'androidJvm' but the consumer didn't ask for it
          - Variant 'releaseRuntimeElements' capability KtlintProject:codeanalysis:unspecified:
              - Unmatched attributes:
                  - Provides com.android.build.api.attributes.AgpVersionAttr '7.2.1' but the consumer didn't ask for it
                  - Provides com.android.build.api.attributes.BuildTypeAttr 'release' but the consumer didn't ask for it
                  - Provides com.android.build.gradle.internal.attributes.VariantAttr 'release' but the consumer didn't ask for it
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.jvm.environment 'android' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Provides org.jetbrains.kotlin.platform.type 'androidJvm' but the consumer didn't ask for it

My environment

Ktlint version: 0.46.1

Ktlint gradle plugin version: 10.3.0

Gradle version: 7.3.3

Gradle plugin version: 7.2.1

@AleksanderBrzozowski
Copy link
Contributor

Well, I don't know if it is a reason why it doesn't work, but the plugin doesn't support ktlint 0.46.0+ #589

@claudiogaalvao
Copy link
Author

claudiogaalvao commented Aug 24, 2022

Changed the ktlint version from 0.46.1 to 0.42.1. The error continue.

@mikehardy
Copy link

I've seen this semi-recently with someone that moved to kotlin 1.7 but the build toolchain (gradle / gradle-plugin) was not latest versions. Pretty vague comment (and it was vague when I last saw it as well). Backing it down to 1.6.x kotlin worked. I suppose updating gradle / gradle plugin to current stable would also work

It's roughly this https://stackoverflow.com/questions/73342537/cannot-use-kotlin-1-7-10

@claudiogaalvao
Copy link
Author

Thanks for your suggestion @mikehardy.

I tried first to downgrade the kotlin version to 1.6.10. Then next, I tried with kotlin version 1.6.10 and the last version of gradle / gradle plugin (7.5.1 / 7.2.2). Then, on my last try, the kotlin and the gradle / gradle plugin with the last stable version.

Unfortunately, it doesn't work :(

@mikehardy
Copy link

Oh I'm sorry to hear that, I dug around for the related issue but couldn't find it - anyway it was definitely reproducible there and had a similar error message so I thought it might rhyme and the same fix would work. Looks like this inability to choose has multiple sources. I imagine we'll both learn something if someone else can nail it. Best of luck with your project

@wakingrufus
Copy link
Collaborator

version 12.1.0 of ktlint gradle came with fixes around AGP >= 7.0. Can you please retry with this version and report if it resolves your issue?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants