/* * Geekdroid is a utility library for development on the Android * Platform. * * Copyright (C) 2017-2020 by Frederic-Charles Barthelery. * * This file is part of Geekdroid. * * Geekdroid is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Geekdroid is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Geekdroid. If not, see . */ package com.geekorum.build import com.android.build.gradle.AppPlugin import com.android.build.gradle.DynamicFeaturePlugin import com.android.build.gradle.LibraryPlugin import com.android.build.gradle.TestedExtension import com.android.build.gradle.api.TestVariant import com.geekorum.gradle.avdl.AvdlExtension import com.geekorum.gradle.avdl.providers.flydroid.FlydroidPlugin import com.geekorum.gradle.avdl.providers.flydroid.flydroid import com.geekorum.gradle.avdl.tasks.LaunchDeviceTask import com.geekorum.gradle.avdl.tasks.StopDeviceTask import com.geekorum.gradle.avdl.tasks.orderForTask import com.geekorum.gradle.avdl.tasks.registerAvdlDevicesTask import org.gradle.api.Project import org.gradle.api.Task import org.gradle.api.provider.Provider import org.gradle.api.services.BuildService import org.gradle.api.services.BuildServiceParameters import org.gradle.api.tasks.TaskContainer import org.gradle.api.tasks.TaskProvider import org.gradle.kotlin.dsl.* fun Project.configureAvdlDevices(flydroidUrl: String, flydroidKey: String) { apply() val oneInstrumentedTestService = gradle.sharedServices.registerIfAbsent( "oneInstrumentedTest", OneInstrumentedTestService::class.java) { maxParallelUsages.set(1) } rootProject.serializeInstrumentedTestTask(oneInstrumentedTestService) val android = the() configure { devices { android.testVariants.all { register("android-n-${project.path}-$baseName") { setup = flydroid { url = flydroidUrl this.flydroidKey = flydroidKey // android-q images fail, don't manage to start the tests image = "android-n" // useTunnel = true } } } } } tasks { var lastStopTask: TaskProvider? = null var lastTestTask: TaskProvider? = null android.testVariants.all { val (startTask, stopTask ) = registerAvdlDevicesTaskForVariant(this, listOf("android-n-${project.path}-$baseName")) listOf(startTask, stopTask).forEach { it.configure { usesService(oneInstrumentedTestService) } } lastStopTask?.let { startTask.configure { mustRunAfter(it) } } lastTestTask?.let { startTask.configure { mustRunAfter(it) } } lastStopTask = stopTask lastTestTask = connectedInstrumentTestProvider } } afterEvaluate { // ensure that launchDeviceTask are run after StopDeviceTask of previous project rootProject.tasks { getByPath(":manage_feeds:launchAvdlFreeDebugAndroidTest") .mustRunAfter(":app:stopAvdlFreeDebugAndroidTest", ":app:stopAvdlGoogleDebugAndroidTest") getByPath(":manage_feeds:launchAvdlGoogleDebugAndroidTest") .mustRunAfter(":app:stopAvdlFreeDebugAndroidTest", ":app:stopAvdlGoogleDebugAndroidTest") } } } private fun TaskContainer.registerAvdlDevicesTaskForVariant( variant: TestVariant, devices: List ): Pair, TaskProvider> { val tasks = registerAvdlDevicesTask(variant.name, devices) tasks.orderForTask(variant.connectedInstrumentTestProvider) return tasks } private fun Project.serializeInstrumentedTestTask(oneInstrumentedTestService: Provider) { fun Project.configureTestTasks() { extensions.configure { testVariants.all { connectedInstrumentTestProvider.configure { usesService(oneInstrumentedTestService) } } } } allprojects { val project = this plugins.withType { project.configureTestTasks() } plugins.withType { project.configureTestTasks() } plugins.withType { project.configureTestTasks() } } } abstract class OneInstrumentedTestService : BuildService