Skip to content

Commit

Permalink
Adjust perf testing variants to only have a single debug variant, and…
Browse files Browse the repository at this point in the history
… properly generate tasks for app variants (#85)

* Add support for product flavors for Perf testing

* Adjust to use single perf project variant, add test to verify

* Adjust sample

* Fixes
  • Loading branch information
rbro112 authored Oct 12, 2023
1 parent 4c28383 commit 527349b
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 43 deletions.
10 changes: 0 additions & 10 deletions gradle-plugin/plugin/performance-project-template/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,3 @@ dependencies {
// Add additional dependencies here as needed.
// Note Espresso is not supported as it degrades performance.
}

/*
* Example code to disable build types other than "release"
androidComponents {
beforeVariants(selector().all()) {
it.enable = it.buildType == "release"
}
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.emergetools.android.gradle

import com.emergetools.android.gradle.base.EmergeGradleRunner
import com.emergetools.android.gradle.mocks.assertSuccessfulUploadRequests
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class MultiProjectFlavorsBuildTypesEmergePluginTest : EmergePluginTest() {

@Test
fun multiProjectUpload() {
EmergeGradleRunner.create("multi-project-with-flavors-buildtypes")
.withArguments("emergeUploadMinApi24DemoStagingAab")
.withDefaultServer()
.assert { result, server ->
assertSuccessfulUploadRequests(server)
result.assertSuccessfulTask(":app:emergeUploadMinApi24DemoStagingAab")
}
.build()
}

@Test
fun multiProjectUploadPerfBundle() {
EmergeGradleRunner.create("multi-project-with-flavors-buildtypes")
.withArguments("emergeUploadMinApi21FullReleasePerfBundle")
.withDefaultServer()
.assert { result, server ->
assertSuccessfulUploadRequests(server)
result.assertSuccessfulTask(":app:emergeUploadMinApi21FullReleasePerfBundle")
}
.build()
}

@Test
fun multiProjectGeneratePerformanceProjectMissing() {
EmergeGradleRunner.create("multi-project-with-flavors-buildtypes")
.withArguments(":app:emergeGeneratePerformanceProject")
.withDefaultServer()
.assert { result, _ ->
assertTrue(
result.output.contains(
"task 'emergeGeneratePerformanceProject' not found in project ':app'"
)
)
}
.buildAndFail()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
plugins {
id 'com.android.application'
id 'com.emergetools.android'
}

android {
namespace 'com.example.myapplication'
compileSdk 31

defaultConfig {
applicationId 'com.example.myapplication'
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
}

buildTypes {
staging {
initWith debug
applicationIdSuffix ".debugStaging"
}
release {
initWith release
applicationIdSuffix ".release"
}
}

flavorDimensions "api", "mode"

productFlavors {
demo {
dimension "mode"
applicationIdSuffix ".demo"
versionNameSuffix "-demo"
}

full {
dimension "mode"
applicationIdSuffix ".full"
versionNameSuffix "-full"
}

minApi24 {
dimension "api"
minSdkVersion 24
}

minApi21 {
dimension "api"
minSdkVersion 21
}
}
}

emerge {
apiToken = 'abcdef123'

performance {
projectPath = ':performance'
}

vcs {
sha = 'testSha'
baseSha = 'testBaseSha'
branchName = 'testBranchName'
gitHub {
repoOwner = 'repoOwner'
repoName = 'repoName'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.myapplication" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id 'com.android.test' apply false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id 'org.jetbrains.kotlin.android'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.myapplication.performance" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
google()
}
}

include(":app")
include(":performance")

dependencyResolutionManagement {
repositories {
google()
mavenCentral()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.android.build.api.variant.AndroidTest
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.ApplicationVariant
import com.android.build.api.variant.TestAndroidComponentsExtension
import com.android.build.api.variant.TestVariant
import com.android.build.api.variant.Variant
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.android.build.gradle.internal.tasks.factory.dependsOn
Expand Down Expand Up @@ -138,6 +139,7 @@ class EmergePlugin : Plugin<Project> {
TestAndroidComponentsExtension::class.java
)

// In practice, we configure only one variant (debug) for the perf project, so this should only run for that single variant
androidTestComponents.onVariants { perfVariant ->
appProject.logger.debug(
"Registering performance project tasks for ${performanceProject.path} from appProject ${appProject.path} with android test plugin for variant ${perfVariant.name}"
Expand Down Expand Up @@ -198,24 +200,26 @@ class EmergePlugin : Plugin<Project> {
appProject: Project,
performanceProject: Project,
extension: EmergePluginExtension,
perfVariant: Variant,
perfVariant: TestVariant,
) {
registerEmergeLocalTestTask(appProject, performanceProject, perfVariant)
registerUploadPerfBundleTask(appProject, performanceProject, perfVariant, extension)
// We're not concerned with the variants of the performance project, rather the app variants,
// generate a perf task for each app variant for the single debug variant of the perf project.
appVariants.forEach { appVariant ->
registerEmergeLocalTestTask(appProject, performanceProject, appVariant, perfVariant)
registerUploadPerfBundleTask(
appProject, performanceProject, appVariant, perfVariant, extension
)
}
}

private fun registerUploadPerfBundleTask(
appProject: Project,
performanceProject: Project,
performanceVariant: Variant,
appVariant: Variant,
performanceVariant: TestVariant,
extension: EmergePluginExtension,
) {
val taskName = "${EMERGE_TASK_PREFIX}Upload${performanceVariant.name.capitalize()}PerfBundle"
val appVariant = appVariants.find { it.name == performanceVariant.name }
checkNotNull(appVariant) {
"Could not find app variant matching performance variant ${performanceVariant.name}"
}

val taskName = "${EMERGE_TASK_PREFIX}Upload${appVariant.name.capitalize()}PerfBundle"
appProject.tasks.register(taskName, UploadPerfBundle::class.java) {
it.group = EMERGE_TASK_GROUP
it.description = "Builds & uploads an AAB for variant ${appVariant.name} to " +
Expand Down Expand Up @@ -249,23 +253,23 @@ class EmergePlugin : Plugin<Project> {
private fun registerEmergeLocalTestTask(
appProject: Project,
performanceProject: Project,
performanceVariant: Variant,
appVariant: ApplicationVariant,
performanceVariant: TestVariant,
) {
val appVariant = appVariants.find { it.name == performanceVariant.name }
val appVariantName = appVariant.name.capitalize()
val perfVariantName = performanceVariant.name.capitalize()

val taskName = "emergeLocal${perfVariantName}Test"
val taskName = "emergeLocal${appVariantName}Test"
val task = appProject.tasks.register(taskName, LocalPerfTest::class.java) {
it.group = EMERGE_TASK_GROUP
it.description = "Installs and runs tests for ${performanceVariant.name} on" +
" connected devices. For testing and debugging."
// If possible get the application ID from the variant of the same name
appVariant?.let { appVariant -> it.appPackageName.set(appVariant.applicationId) }
it.appPackageName.set(appVariant.applicationId)
it.testPackageName.set(performanceVariant.namespace)
}

val uninstallAppTaskPath = "${appProject.path}:uninstall$perfVariantName"
val installAppTaskPath = "${appProject.path}:install$perfVariantName"
val uninstallAppTaskPath = "${appProject.path}:uninstall$appVariantName"
val installAppTaskPath = "${appProject.path}:install$appVariantName"

// We need the uninstall task to run first but don't want to enforce that
// order unless the local test task is actually being run
Expand Down Expand Up @@ -447,13 +451,9 @@ class EmergePlugin : Plugin<Project> {
}

buildTypes {
val debugSigningConfig = getByName("debug").signingConfig
appExtension.buildTypes.forEach { appBuildType ->
appProject.logger.debug("Configuring build type ${appBuildType.name} for performance project")
maybeCreate(appBuildType.name).apply {
isDebuggable = false
signingConfig = debugSigningConfig
}
debug {
isDebuggable = true
signingConfig = appExtension.signingConfigs.getByName("debug")
}
}

Expand Down
43 changes: 43 additions & 0 deletions performance/sample/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,53 @@ android {
signingConfig = signingConfigs.getByName("debug")
}
debug {
isDefault = true
applicationIdSuffix = ".debug"
}
}

flavorDimensions.add("api")
flavorDimensions.add("mode")

productFlavors {
create("demo") {
dimension = "mode"
applicationIdSuffix = ".demo"
versionNameSuffix = "-demo"
}

create("full") {
dimension = "mode"
applicationIdSuffix = ".full"
versionNameSuffix = "-full"
}

create("minApi24") {
dimension = "api"
minSdk = 24
}

create("minApi21") {
dimension = "api"
minSdk = 21
}
}

flavorDimensions.add("store")
flavorDimensions.add("distribution")

productFlavors {
create("play") {
dimension = "store"
signingConfig = signingConfigs.getByName("debug")
isDefault = true
}
create("playstore") {
dimension = "distribution"
signingConfig = signingConfigs.getByName("debug")
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
9 changes: 0 additions & 9 deletions performance/sample/perfTesting/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,3 @@ dependencies {
// Emerge's UIAutomator helper library (Optional): https://github.com/EmergeTools/relax
implementation("com.emergetools.test:relax:0.1.0")
}

/**
* Example code to disable build types other than "release"
*/
androidComponents {
beforeVariants(selector().all()) {
it.enable = it.buildType == "release"
}
}

0 comments on commit 527349b

Please sign in to comment.