buildSrc/src/main/kotlin/Avdl.kt
changeset 0 0a13dcabf7d3
child 34 ce299aacc068
equal deleted inserted replaced
-1:000000000000 0:0a13dcabf7d3
       
     1 /*
       
     2  * AboutOss is a utility library to retrieve and display
       
     3  * opensource licenses in Android applications.
       
     4  *
       
     5  * Copyright (C) 2023 by Frederic-Charles Barthelery.
       
     6  *
       
     7  * This file is part of AboutOss.
       
     8  *
       
     9  * AboutOss is free software: you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation, either version 3 of the License, or
       
    12  * (at your option) any later version.
       
    13  *
       
    14  * AboutOss is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  * GNU General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 package com.geekorum.build
       
    23 
       
    24 import com.android.build.gradle.AppPlugin
       
    25 import com.android.build.gradle.DynamicFeaturePlugin
       
    26 import com.android.build.gradle.LibraryPlugin
       
    27 import com.android.build.gradle.TestedExtension
       
    28 import com.android.build.gradle.api.TestVariant
       
    29 import com.geekorum.gradle.avdl.AvdlExtension
       
    30 import com.geekorum.gradle.avdl.providers.flydroid.FlydroidPlugin
       
    31 import com.geekorum.gradle.avdl.providers.flydroid.flydroid
       
    32 import com.geekorum.gradle.avdl.tasks.LaunchDeviceTask
       
    33 import com.geekorum.gradle.avdl.tasks.StopDeviceTask
       
    34 import com.geekorum.gradle.avdl.tasks.orderForTask
       
    35 import com.geekorum.gradle.avdl.tasks.registerAvdlDevicesTask
       
    36 import org.gradle.api.Project
       
    37 import org.gradle.api.Task
       
    38 import org.gradle.api.provider.Provider
       
    39 import org.gradle.api.services.BuildService
       
    40 import org.gradle.api.services.BuildServiceParameters
       
    41 import org.gradle.api.tasks.TaskContainer
       
    42 import org.gradle.api.tasks.TaskProvider
       
    43 import org.gradle.kotlin.dsl.*
       
    44 
       
    45 
       
    46 fun Project.configureAvdlDevices(flydroidUrl: String, flydroidKey: String) {
       
    47     apply<FlydroidPlugin>()
       
    48 
       
    49     // as FlydroidPlugin add some repositories to look for dependencies
       
    50     // the repositories set in settings.gradle.kts via dependencyResolutionManagement
       
    51     // are ignored because we are in mode PREFER_PROJECT
       
    52     // to fix that we add the missing repository here too
       
    53     repositories { // this mirror contents in settings.gradle.kts
       
    54         google {
       
    55             content {
       
    56                 includeGroupByRegex("""android\.arch\..*""")
       
    57                 includeGroupByRegex("""androidx\..*""")
       
    58                 includeGroupByRegex("""com\.android\..*""")
       
    59                 includeGroupByRegex("""com\.google\..*""")
       
    60                 includeGroup("com.crashlytics.sdk.android")
       
    61                 includeGroup("io.fabric.sdk.android")
       
    62                 includeGroup("org.chromium.net")
       
    63                 includeGroup("zipflinger")
       
    64                 includeGroup("com.android")
       
    65             }
       
    66         }
       
    67         mavenCentral()
       
    68         // for geekdroid
       
    69         flatDir {
       
    70             dirs("$rootDir/libs")
       
    71         }
       
    72         maven {
       
    73             url = uri("https://jitpack.io")
       
    74         }
       
    75 
       
    76     }
       
    77     val oneInstrumentedTestService = gradle.sharedServices.registerIfAbsent(
       
    78             "oneInstrumentedTest", OneInstrumentedTestService::class.java) {
       
    79         maxParallelUsages.set(1)
       
    80     }
       
    81 
       
    82     rootProject.serializeInstrumentedTestTask(oneInstrumentedTestService)
       
    83 
       
    84     val android = the<TestedExtension>()
       
    85     configure<AvdlExtension> {
       
    86         devices {
       
    87             android.testVariants.all {
       
    88                 register("android-p-${project.path}-$baseName") {
       
    89                     setup = flydroid {
       
    90                         url = flydroidUrl
       
    91                         this.flydroidKey = flydroidKey
       
    92                         // android-q images fail, don't manage to start the tests
       
    93                         image = "android-p"
       
    94                          useTunnel = true
       
    95                     }
       
    96                 }
       
    97             }
       
    98         }
       
    99     }
       
   100 
       
   101     tasks {
       
   102         var lastStopTask: TaskProvider<out Task>? = null
       
   103         var lastTestTask: TaskProvider<out Task>? = null
       
   104         android.testVariants.all {
       
   105             val (startTask, stopTask ) =
       
   106                 registerAvdlDevicesTaskForVariant(this, listOf("android-p-${project.path}-$baseName"))
       
   107             listOf(startTask, stopTask).forEach {
       
   108                 it.configure {
       
   109                     usesService(oneInstrumentedTestService)
       
   110                 }
       
   111             }
       
   112 
       
   113             lastStopTask?.let {
       
   114                 startTask.configure {
       
   115                     mustRunAfter(it)
       
   116                 }
       
   117             }
       
   118             lastTestTask?.let {
       
   119                 startTask.configure {
       
   120                     mustRunAfter(it)
       
   121                 }
       
   122             }
       
   123             lastStopTask = stopTask
       
   124             lastTestTask = connectedInstrumentTestProvider
       
   125         }
       
   126     }
       
   127 
       
   128     afterEvaluate {
       
   129         // ensure that launchDeviceTask are run after StopDeviceTask of previous project
       
   130         rootProject.tasks {
       
   131             getByPath(":geekdroid-firebase:launchAvdlDebugAndroidTest")
       
   132                     .mustRunAfter(":geekdroid:stopAvdlDebugAndroidTest")
       
   133         }
       
   134     }
       
   135 }
       
   136 
       
   137 private fun TaskContainer.registerAvdlDevicesTaskForVariant(
       
   138     variant: TestVariant, devices: List<String>
       
   139 ): Pair<TaskProvider<LaunchDeviceTask>, TaskProvider<StopDeviceTask>> {
       
   140     val tasks =
       
   141         registerAvdlDevicesTask(variant.name, devices)
       
   142     tasks.orderForTask(variant.connectedInstrumentTestProvider)
       
   143     return tasks
       
   144 }
       
   145 
       
   146 
       
   147 private fun Project.serializeInstrumentedTestTask(oneInstrumentedTestService: Provider<OneInstrumentedTestService>) {
       
   148     fun Project.configureTestTasks() {
       
   149         extensions.configure<TestedExtension> {
       
   150             testVariants.all {
       
   151                 connectedInstrumentTestProvider.configure {
       
   152                     usesService(oneInstrumentedTestService)
       
   153                 }
       
   154             }
       
   155         }
       
   156     }
       
   157 
       
   158     allprojects {
       
   159         val project = this
       
   160         plugins.withType<AppPlugin> { project.configureTestTasks() }
       
   161         plugins.withType<DynamicFeaturePlugin> { project.configureTestTasks() }
       
   162         plugins.withType<LibraryPlugin> { project.configureTestTasks() }
       
   163     }
       
   164 }
       
   165 
       
   166 abstract class OneInstrumentedTestService : BuildService<BuildServiceParameters.None>