白眼鏡のblog

新しく得た知見を備忘録的に書き連ねていく

AGP 4.1系へアップデートした際のKotlinDSLを利用したtestOptionsの指定方法とDataBinding設定

AndroidGradlePluginを4.1系にアップデートした際に若干ハマったのでメモ

TestOptions

Spek2用に記載していた以下の記述がエラーを吐くようになった。

    testOptions {
        junitPlatform {
            filters {
                includeEngines("spek2")
            }
        }
        unitTests.all(KotlinClosure1<Test, Test>({
            apply {
                testLogging.events = setOf(
                    TestLogEvent.PASSED,
                    TestLogEvent.FAILED,
                    TestLogEvent.STANDARD_ERROR
                )
            }
        }, this))
    }
e: build.gradle.kts:46:9: Expression 'junitPlatform' cannot be invoked as a function. The function 'invoke()' is not found
e: build.gradle.kts:46:9: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public val TestOptions.junitPlatform: AndroidJUnitPlatformExtension defined in de.mannodermaus.gradle.plugins.junit5
e: build.gradle.kts:47:13: Unresolved reference: filters
e: build.gradle.kts:48:17: Unresolved reference: includeEngines
e: build.gradle.kts:51:23: Type mismatch: inferred type is KotlinClosure1<Test, Test> but (Test) -> Unit was expected

TestOptions.ktに存在するunitTests関数や、all関数の引数が変わっていたため対応すると以下のような形となりSyncできるようになる。

    tasks.withType<Test> {
        useJUnitPlatform {
            includeEngines("spek2")
        }
    }

    testOptions.unitTests(Action {
        all {
            closureOf<Test> {
                testLogging.events = setOf(
                    TestLogEvent.PASSED,
                    TestLogEvent.FAILED,
                    TestLogEvent.STANDARD_ERROR
                )
            }
        }
    })

DataBinding

BuildSrcでDataBinding設定を行っている場合おそらく以下のようになっているかと思う。

buildFeatures.dataBinding = true

AGPをアップデートするとBaseExtensionに存在するbuildFeaturesから何故かDataBindingが設定できなくなるため、エラーとして表示される。

Unresolved reference: dataBinding

DataBindingの設定方法を辿ってみると、ApplicationBuildFeaturesに存在するようになっているため、BaseExtensionから取得できるBuildFeaturesをApplicationBuildFeaturesにキャストすることでDataBindingが使用可能になる。

(buildFeatures as ApplicationBuildFeatures).dataBinding = true

また、マルチモジュール 構成をとっている場合、LibraryModuleはLibraryBuildFeaturesを参照する形となるため

    val buildFeatures = buildFeatures // スマートキャスト用に一旦変数に格納
    when(buildFeatures) {
        is ApplicationBuildFeatures -> buildFeatures.dataBinding = true
        is LibraryBuildFeatures -> buildFeatures.dataBinding = true
    }

のような形となる(すごい対処療法感ありますが)