Skip to content

Commit

Permalink
Merge pull request #73 from krzdabrowski/feature/baseline-profiles
Browse files Browse the repository at this point in the history
Implement Baseline and Startup Profiles, unify libs.versions.toml, update minSdk to 26
  • Loading branch information
krzdabrowski authored Mar 9, 2024
2 parents 3b04963 + c48438d commit a378ad9
Show file tree
Hide file tree
Showing 16 changed files with 35,307 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ jobs:
- name: Run Compose UI tests
uses: ReactiveCircus/android-emulator-runner@v2
with:
api-level: 24
api-level: 26
disable-animations: false
script: ./gradlew connectedCheck --stacktrace
script: ./gradlew basic-feature:connectedCheck --stacktrace

- name: Run Maestro UI tests
uses: mobile-dev-inc/action-maestro-cloud@v1
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Supports light/dark mode theming automatically.
* Room - for local database
* Coil - for image loading
* Version Catalog - for dependency management
* Baseline and Startup Profiles - for performance improvements during app launch
* Timber - for logging
* JUnit5, Turbine and MockK - for unit tests
* Jetpack Compose test dependencies, Maestro and Hilt - for UI tests
Expand Down
16 changes: 9 additions & 7 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.baseline.profile)
alias(libs.plugins.detekt)
alias(libs.plugins.hilt)
alias(libs.plugins.kotlin)
Expand All @@ -13,7 +14,7 @@ android {

defaultConfig {
applicationId = "eu.krzdabrowski.starter"
minSdk = 24
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
Expand All @@ -37,10 +38,6 @@ android {
}
}

compileOptions {
isCoreLibraryDesugaringEnabled = true
}

composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
}
Expand All @@ -54,6 +51,10 @@ android {
}
}

baselineProfile {
dexLayoutOptimization = true
}

dependencies {
implementation(project(":core"))
implementation(project(":basic-feature"))
Expand All @@ -63,11 +64,12 @@ dependencies {
implementation(libs.room.ktx)
implementation(libs.timber)

implementation(libs.test.android.profile.installer)
baselineProfile(project(":baseline-profiles"))

ksp(libs.hilt.compiler)
ksp(libs.room.compiler)

coreLibraryDesugaring(libs.desugar)

detektPlugins(libs.detekt.compose.rules)
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidStarter"
tools:targetApi="31" />
tools:targetApi="34" />

</manifest>
17,530 changes: 17,530 additions & 0 deletions app/src/release/generated/baselineProfiles/baseline-prof.txt

Large diffs are not rendered by default.

17,530 changes: 17,530 additions & 0 deletions app/src/release/generated/baselineProfiles/startup-prof.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions baseline-profiles/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
49 changes: 49 additions & 0 deletions baseline-profiles/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@file:Suppress("UnstableApiUsage")

plugins {
alias(libs.plugins.android.test)
alias(libs.plugins.baseline.profile)
alias(libs.plugins.detekt)
alias(libs.plugins.kotlin)
alias(libs.plugins.ktlint)
}

android {
compileSdk = 34
namespace = "eu.krzdabrowski.starter.baselineprofiles"

with (defaultConfig) {
minSdk = 28
targetSdk = 34
}

defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

kotlin {
jvmToolchain(17)
}

targetProjectPath = ":app"
}

androidComponents {
onVariants { v ->
v.instrumentationRunnerArguments.put(
"targetAppId",
v.testedApks.map { v.artifacts.getBuiltArtifactsLoader().load(it)?.applicationId }
)
}
}

baselineProfile {
useConnectedDevices = true
}

dependencies {
implementation(libs.test.android.benchmark.macro)
implementation(libs.test.android.junit)

detektPlugins(libs.detekt.compose.rules)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package eu.krzdabrowski.starter.baselineprofiles

import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* This test class generates a basic startup baseline profile for the target package.
*
* We recommend you start with this but add important user flows to the profile to improve their performance.
* Refer to the [baseline profile documentation](https://d.android.com/topic/performance/baselineprofiles)
* for more information.
*
* You can run the generator with the "Generate Baseline Profile" run configuration in Android Studio or
* the equivalent `generateBaselineProfile` gradle task:
* ```
* ./gradlew :app:generateReleaseBaselineProfile
* ```
* The run configuration runs the Gradle task and applies filtering to run only the generators.
*
* Check [documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args)
* for more information about available instrumentation arguments.
*
* After you run the generator, you can verify the improvements running the [StartupBenchmarks] benchmark.
*
* When using this class to generate a baseline profile, only API 33+ or rooted API 28+ are supported.
*
* The minimum required version of androidx.benchmark to generate a baseline profile is 1.2.0.
**/
@RunWith(AndroidJUnit4::class)
class BaselineProfileGenerator {

@get:Rule
val rule = BaselineProfileRule()

@Test
fun generate() {
// The application id for the running build variant is read from the instrumentation arguments.
rule.collect(
packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
?: throw IllegalArgumentException("targetAppId not passed as instrumentation runner arg"),

// See: https://d.android.com/topic/performance/baselineprofiles/dex-layout-optimizations
includeInStartupProfile = true,
) {
// This block defines the app's critical user journey. Here we are interested in
// optimizing for app startup. But you can also navigate and scroll through your most important UI.

// Start default activity for your app
pressHome()
startActivityAndWait()

// Write more interactions to optimize advanced journeys of your app.
// For example:
// 1. Wait until the content is asynchronously loaded
// 2. Scroll the feed content
// 3. Navigate to detail screen

// Check UiAutomator documentation for more information how to interact with the app.
// https://d.android.com/training/testing/other-components/ui-automator
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package eu.krzdabrowski.starter.baselineprofiles

import androidx.benchmark.macro.BaselineProfileMode
import androidx.benchmark.macro.CompilationMode
import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.StartupTimingMetric
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* This test class benchmarks the speed of app startup.
* Run this benchmark to verify how effective a Baseline Profile is.
* It does this by comparing [CompilationMode.None], which represents the app with no Baseline
* Profiles optimizations, and [CompilationMode.Partial], which uses Baseline Profiles.
*
* Run this benchmark to see startup measurements and captured system traces for verifying
* the effectiveness of your Baseline Profiles. You can run it directly from Android
* Studio as an instrumentation test, or run all benchmarks for a variant, for example benchmarkRelease,
* with this Gradle task:
* ```
* ./gradlew :baseline-profiles:connectedBenchmarkReleaseAndroidTest
* ```
*
* You should run the benchmarks on a physical device, not an Android emulator, because the
* emulator doesn't represent real world performance and shares system resources with its host.
*
* For more information, see the [Macrobenchmark documentation](https://d.android.com/macrobenchmark#create-macrobenchmark)
* and the
* [instrumentation args documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args)
**/
@RunWith(AndroidJUnit4::class)
class StartupBenchmarks {

@get:Rule
val rule = MacrobenchmarkRule()

@Test
fun startupCompilationNone() =
benchmark(CompilationMode.None())

@Test
fun startupCompilationBaselineProfiles() =
benchmark(CompilationMode.Partial(BaselineProfileMode.Require))

private fun benchmark(compilationMode: CompilationMode) {
// The application id for the running build variant is read from the instrumentation arguments.
rule.measureRepeated(
packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
?: throw IllegalArgumentException("targetAppId not passed as instrumentation runner arg"),
metrics = listOf(StartupTimingMetric()),
compilationMode = compilationMode,
startupMode = StartupMode.COLD,
iterations = 10,
setupBlock = {
pressHome()
},
measureBlock = {
startActivityAndWait()

// Add interactions to wait for when your app is fully drawn.
// The app is fully drawn when Activity.reportFullyDrawn is called.
// For Jetpack Compose, you can use ReportDrawn, ReportDrawnWhen and ReportDrawnAfter
// from the AndroidX Activity library.

// Check the UiAutomator documentation for more information on how to
// interact with the app.
// https://d.android.com/training/testing/other-components/ui-automator
},
)
}
}
8 changes: 1 addition & 7 deletions basic-feature/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
namespace = "eu.krzdabrowski.starter.basicfeature"

with (defaultConfig) {
minSdk = 24
minSdk = 26
targetSdk = 34
}

Expand All @@ -33,10 +33,6 @@ android {
}
}

compileOptions {
isCoreLibraryDesugaringEnabled = true
}

composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
}
Expand Down Expand Up @@ -83,7 +79,5 @@ dependencies {
ksp(libs.hilt.compiler)
kspAndroidTest(libs.test.android.hilt.compiler)

coreLibraryDesugaring(libs.desugar)

detektPlugins(libs.detekt.compose.rules)
}
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.android.test) apply false
alias(libs.plugins.baseline.profile) apply false
alias(libs.plugins.detekt)
alias(libs.plugins.hilt) apply false
alias(libs.plugins.junit) apply false
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
namespace = "eu.krzdabrowski.starter.core"

with (defaultConfig) {
minSdk = 24
minSdk = 26
targetSdk = 34
}

Expand Down
21 changes: 0 additions & 21 deletions core/proguard-rules.pro

This file was deleted.

Loading

0 comments on commit a378ad9

Please sign in to comment.