Lifecycle of extension

Android gradle plugin has extension properties which might be set in build.gradle:

android {
  defaultConfig {
    testInstrumentationRunnerArgument("annotation", "com.hello.RunThisTestOnly")
  }
}

And also can be set with command line arguments:

./gradlew connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.annotation=com.hello.RunThisTestOnly

But I can’t set it from another task:

task taskX {
  doFirst {
    android.defaultConfig.testInstrumentationRunnerArgument 'annotation', 'com.hello.RunThisTestOnly'
  }
  finalizedBy 'connectedAndroidTest'
}

Or from custom plugin/task with Kotlin.

Could it be a bug or I don’t know something about lifecycle of extensions?

The android plugin is read in the Gradle configuration phase and values are final after that. In order to change any setting you need to hook into the configuration phase. Since a task is executed any time later (in the execution phase) I believe it is not possible to modify the values of the android plugin afterwards.

1 Like

Thanks, that’s what I also started to suspect after different attempts to change it.