Skip to content

Commit

Permalink
Adjust to use single perf project variant, add test to verify
Browse files Browse the repository at this point in the history
  • Loading branch information
rbro112 committed Oct 12, 2023
1 parent 5160b62 commit 7ff295b
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 36 deletions.
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 perfVariantName = performanceVariant.name.capitalize()
val appVariantName = appVariant.name.capitalize()
val perfVariantName = appVariant.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 @@ -449,24 +453,8 @@ class EmergePlugin : Plugin<Project> {
val debugSigningConfig = appExtension.signingConfigs.getByName("debug")

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

appExtension.productFlavors.forEach { appFlavor ->
appProject.logger.debug("Configuring flavor ${appFlavor.name} for performance project")
appFlavor.dimension?.let {
if (!flavorDimensions.contains(it)) {
flavorDimensions.add(it)
}
}
productFlavors.maybeCreate(appFlavor.name).apply {
dimension = appFlavor.dimension
debug {
isDebuggable = true
signingConfig = debugSigningConfig
}
}
Expand Down

0 comments on commit 7ff295b

Please sign in to comment.