1
2

More than 1 year has passed since last update.

Gradle 6.7.1以下 -> 6.8以上 にアップデートするとbuild.gradleのdependenciesのexcludeの挙動が変わる

Posted at

概要

androidアプリを作成中、gradleをアップデートしたら、それまで問題なく使えていたアプリソース中のkotlinファイルでのimport kotlinx.coroutines.CoroutineDispatcherに対してUnresolved reference: CoroutineDispatcherとエラーメッセージがつくようになってビルドができなくなった。 
アプリケーションモジュールのbuild.gradleのdependenciesにimplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:$kotlin_coroutine_ver"を追加してやったところビルドが通るようになった。

理由

アプリで使っているcoroutineライブラリとpagingライブラリに入っているそれとがバージョンが違っていたのでpagingライブラリのほうから抜くためにアプリケーションモジュールのbuild.gradleのdependenciesで以下のようにしていたのだが、gradle 6.8以上にしたところなぜかアプリ全体の依存から除外されるようになってしまったもよう。
gradle6.8だけではなく、6.9.1や7.0.2にしても同様の挙動をしたのでバグではなく仕様と思われる。
gradleのrelease noteも読んでみたけど、軽くさらっただけでは明確な仕様の変更の記述は見つけることができなかった。

dependencies {
    implementation('androidx.paging:paging-runtime-ktx:3.0.1') {
        exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core-jvm" 
    }
}

詳細なコード

gradle 6.7.1時代

  • gradle/gradle-wrapper.properties
#Sun Jan 09 17:34:51 JST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
  • プロジェクトレベルのbuild.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
        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
    }
}

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

  • アプリケーションモジュールのbuild.gradle

buildscript {
    repositories {
        maven { url 'https://maven.google.com' }
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
    }
}

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.kkkkan.coroutineresearch"
        minSdk 24
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    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 {
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'

    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'

    implementation('androidx.paging:paging-runtime-ktx:3.0.1') {
        // バージョンが食い違うので除外
        exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core-jvm"
    }

}

gradle 6.8 アップデート後

  • gradle/gradle-wrapper.properties
#Sun Jan 09 17:34:51 JST 2022
distributionBase=GRADLE_USER_HOME
#以下変更 6.7.1->6.8に
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
#変更終了
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
  • プロジェクトレベルのbuild.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
        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
    }
}

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

  • アプリケーションモジュールのbuild.gradle

buildscript {
    repositories {
        maven { url 'https://maven.google.com' }
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
    }
}

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.kkkkan.coroutineresearch"
        minSdk 24
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    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 {
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
    // 以下追加。gradle 6.8にアップデートしたら、これがないとビルドが通らなくなった。
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.5.2"
    // 追加終了。

    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'

    implementation('androidx.paging:paging-runtime-ktx:3.0.1') {
        // バージョンが食い違うので除外
        exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core-jvm" 
    }

}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2