Sonarqube running on older gradle version in gitlab

I have one android project which is running perfectly but when i try to run sonarqube-check from CI/CD it’s showing lower gradle error my project is already running on latest gradle 7.0.3

sonarqube is already setup in gitlab

Project level gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
        maven {
            url 'https://sdk.squareup.com/public/android'
        }
    }
}

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

app-level gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id "org.sonarqube" version "3.3"
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.sonarqube"
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
    }
    buildFeatures {
        viewBinding true
    }

}
sonarqube {
    tasks.sonarqube.dependsOn build
    properties {
        property "sonar.projectKey", "root_sonarqube_android_xyz-"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.sources", "src"
    }
}
dependencies {
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

.gitlab-ci.yml

sonarqube-check:
  tags:
    - runner
  image: gradle:jre11-slim
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: gradle sonarqube
  allow_failure: true
  when: manual
  only:
    - dev_mitesh # or the name of your main branch

i am trying to check from CI/CD pipelines

i am getting something like this in response

Please suggest me what is wrong

Any suggestion would be highly appreciated.

After adding this in script i am able to run gradle in 7.0.2

script:
    - sh gradlew sonarqube

but now i am facing sdk issue it’s showing like this

application is running on my system but from gitlab it’s not running

Hello @miteshmakwana,

Thanks for your message. It doesn’t seem to me that your issue is related to the sonarqube. Do you have same issues when trying to build your project in the ci? I can assume the outcome should be the same.

First of all, the sonarqube task currently depends on compile task, and from the log I can see that it’s compile task that is failing. And the reason why it’s failing is that you either do not have an Android SDK in the CI machine or the environment variable $ANDROID_SDK_ROOT is not pointing to it.

And it probably works locally because you have it installed.

From the ci script you provided, I do not really see that you’re installing Android SDK and I can assume the image you’re using doesn’t really contain Android SDK. You can read more about this here or here.

Best,
Margarita

@Margarita_Nedzelska i set my .gitlab-ci.yml as per sonarscanner for gradle doc i am using gitlab in our server so we need to install android sdk on that system?

If you want to build an Android project then yes. If you just want to run an analysis, you still need it, because sonarqube task depends on compile.

If you don’t want to build the project in ci at all, you can run gradle sonarqube -x compile, but I wouldn’t recommend you to do it, because without building the project, the result of the analysis might be inaccurate. Going this way you won’t need Andeoid Sdk, because you’re not actually building your project.

And a side note: It’s a good practice to use Gradle wrapper This will help you to always run your project with the right Gradle version.

use gradle wrapper so you mean like use in cache path like this?

sonarqube-check:
  tags:
    - runner
  image: jangrewe/gitlab-ci-android
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .gradle/wrapper
      - .gradle/caches
      - .sonar/cache
  before_script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - chmod +x ./gradlew
  script:
    - sh gradlew sonarqube
  allow_failure: true
  when: manual
  only:
    - dev_mitesh # or the name of your main branch

@Margarita_Nedzelska thank you for your response as per this i added jangrewe/gitlab-ci-android
and now i am not getting any sdk error but i am getting something like this

* What went wrong:
Execution failed for task ':app:sonarqube'.
> File src/test/java/com/example/sonarqube/ExampleUnitTest.kt can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files

I think your last question is not related to the topic, so I consider the initial question to be closed, As for the error you’re getting, I can’t say more than the error message: “File src/test/java/com/example/sonarqube/ExampleUnitTest.kt can’t be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files” Make sure you mapped correctly your main and test properties. In the case of a Gradle scanner, I’d suggest not configuring them at all, as the Gradle scanner maps them correctly automatically.

And another point, you should first try to check if task is not failing locally before configuring it in your CI.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.