Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #379 from tikurahul/feature/app_startup
Browse files Browse the repository at this point in the history
Minor improvements to PR #376
  • Loading branch information
JoseAlcerreca authored Jul 9, 2021
2 parents 6e6963a + c6d4467 commit 6f96c15
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 51 deletions.
3 changes: 3 additions & 0 deletions buildSrc/src/main/java/Libs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ object Libs {
const val ACTIVITY_COMPOSE = "androidx.activity:activity-compose"
const val ACTIVITY_KTX = "androidx.activity:activity-ktx"
const val APPCOMPAT = "androidx.appcompat:appcompat"
const val APP_STARTUP = "androidx.startup:startup-runtime"
const val ARCH_TESTING = "androidx.arch.core:core-testing"
const val ARCORE = "com.google.ar:core"
const val BENCHMARK = "androidx.benchmark:benchmark-junit4"
const val BENCHMARK_MACRO = "androidx.benchmark:benchmark-macro-junit4"
const val BROWSER = "androidx.browser:browser"
const val CARDVIEW = "androidx.cardview:cardview"
const val CONSTRAINT_LAYOUT = "androidx.constraintlayout:constraintlayout"
Expand Down Expand Up @@ -87,4 +89,5 @@ object Libs {
const val TIMBER = "com.jakewharton.timber:timber"
const val VIEWMODEL_COMPOSE = "androidx.lifecycle:lifecycle-viewmodel-compose"
const val VIEWPAGER2 = "androidx.viewpager2:viewpager2"
const val UI_AUTOMATOR = "androidx.test.uiautomator:uiautomator"
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object Versions {
const val TARGET_SDK = 30
const val MIN_SDK = 21

const val ANDROID_GRADLE_PLUGIN = "7.0.0-beta03"
const val ANDROID_GRADLE_PLUGIN = "7.0.0-beta05"
const val BENCHMARK = "1.0.0"
const val COMPOSE = "1.0.0-beta04"
const val FIREBASE_CRASHLYTICS = "2.3.0"
Expand Down
6 changes: 6 additions & 0 deletions depconstraints/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ plugins {
val appcompat = "1.1.0"
val activity = "1.2.0-rc01"
val activityCompose = "1.3.0-alpha03"
val appStartup = "1.1.0-beta01"
val cardview = "1.0.0"
val archTesting = "2.0.0"
val arcore = "1.7.0"
val benchmark = "1.0.0"
val benchmarkMacro = "1.1.0-alpha02"
val browser = "1.0.0"
val compose = Versions.COMPOSE
val constraintLayout = "1.1.3"
Expand Down Expand Up @@ -70,16 +72,19 @@ val threetenabp = "1.0.5"
val timber = "4.7.1"
val viewpager2 = "1.0.0"
val viewModelCompose = "1.0.0-alpha02"
val uiAutomator = "2.2.0"

dependencies {
constraints {
api("${Libs.ACTIVITY_COMPOSE}:$activityCompose")
api("${Libs.ACTIVITY_KTX}:$activity")
api("${Libs.APPCOMPAT}:$appcompat")
api("${Libs.APP_STARTUP}:$appStartup")
api("${Libs.CARDVIEW}:$cardview")
api("${Libs.ARCH_TESTING}:$archTesting")
api("${Libs.ARCORE}:$arcore")
api("${Libs.BENCHMARK}:$benchmark")
api("${Libs.BENCHMARK_MACRO}:$benchmarkMacro")
api("${Libs.BROWSER}:$browser")
api("${Libs.COMPOSE_ANIMATION}:$compose")
api("${Libs.COMPOSE_MATERIAL}:$compose")
Expand Down Expand Up @@ -145,6 +150,7 @@ dependencies {
api("${Libs.TIMBER}:$timber")
api("${Libs.VIEWPAGER2}:$viewpager2")
api("${Libs.VIEWMODEL_COMPOSE}:$viewModelCompose")
api("${Libs.UI_AUTOMATOR}:$uiAutomator")
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Feb 10 08:38:31 CET 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
1 change: 1 addition & 0 deletions macrobenchmark/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
50 changes: 50 additions & 0 deletions macrobenchmark/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
id 'com.android.test'
id 'kotlin-android'
}

android {
compileSdkVersion 30
defaultConfig {
minSdkVersion 29
targetSdkVersion 30

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}

buildTypes {
// declare a build type (release) to match the target app's build type
release {
debuggable = true
signingConfig = debug.signingConfig
}
}

targetProjectPath = ":mobile"
experimentalProperties["android.experimental.self-instrumenting"] = true
}

androidComponents {
beforeVariants(selector().all()) {
// enable only the release buildType, since we only want to measure
// release build performance
enabled = buildType == 'release'
}
}

dependencies {
api platform(project(":depconstraints"))
implementation Libs.BENCHMARK_MACRO
implementation Libs.ESPRESSO_CORE
implementation Libs.EXT_JUNIT
implementation Libs.KOTLIN_STDLIB
implementation Libs.UI_AUTOMATOR
}
Empty file.
21 changes: 21 additions & 0 deletions macrobenchmark/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
25 changes: 25 additions & 0 deletions macrobenchmark/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2021 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.google.samples.apps.iosched.macrobenchmark">

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.samples.apps.iosched.macrobenchmark

import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.filters.LargeTest
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@LargeTest
@RunWith(Parameterized::class)
class StartupBenchmark(private val startupMode: StartupMode) {
@get:Rule
val benchmarkRule = MacrobenchmarkRule()

@Test
fun startup() = benchmarkRule.measureStartup(
profileCompiled = false,
startupMode = startupMode,
iterations = 3
) {
action = "com.google.samples.apps.iosched.STARTUP_ACTIVITY"
}

companion object {
@Parameterized.Parameters(name = "mode={0}")
@JvmStatic
fun parameters(): List<Array<Any>> {
return listOf(StartupMode.COLD, StartupMode.WARM, StartupMode.HOT)
.map { arrayOf(it) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.samples.apps.iosched.macrobenchmark

import android.content.Intent
import androidx.benchmark.macro.CompilationMode
import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.StartupTimingMetric
import androidx.benchmark.macro.junit4.MacrobenchmarkRule

const val TARGET_PACKAGE = "com.google.samples.apps.iosched"

fun MacrobenchmarkRule.measureStartup(
profileCompiled: Boolean,
startupMode: StartupMode,
iterations: Int = 3,
setupIntent: Intent.() -> Unit = {}
) = measureRepeated(
packageName = TARGET_PACKAGE,
metrics = listOf(StartupTimingMetric()),
compilationMode = if (profileCompiled) {
CompilationMode.SpeedProfile(warmupIterations = 3)
} else {
CompilationMode.None
},
iterations = iterations,
startupMode = startupMode
) {
pressHome()
val intent = Intent()
intent.setPackage(TARGET_PACKAGE)
setupIntent(intent)
startActivityAndWait(intent)
}
1 change: 1 addition & 0 deletions mobile/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

implementation(Libs.CORE_KTX)
implementation(Libs.APP_STARTUP)

// UI
implementation(Libs.ACTIVITY_KTX)
Expand Down
28 changes: 27 additions & 1 deletion mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@
</activity>

<activity
android:name=".ui.MainActivity" />
android:name=".ui.MainActivity" >
<intent-filter>
<action android:name="com.google.samples.apps.iosched.STARTUP_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
android:name=".ui.onboarding.OnboardingActivity"
Expand Down Expand Up @@ -87,6 +92,27 @@
android:exported="false" />
<receiver android:name=".shared.notifications.CancelNotificationBroadcastReceiver"
android:exported="false" />

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data
android:name="com.google.samples.apps.iosched.util.initializers.AndroidThreeTenInitializer"
android:value="androidx.startup" />
<meta-data
android:name="com.google.samples.apps.iosched.util.initializers.StrictModeInitializer"
android:value="androidx.startup" />
<meta-data
android:name="com.google.samples.apps.iosched.util.initializers.TimberInitializer"
android:value="androidx.startup" />

</provider>
<!-- enable profiling by macrobenchmark -->
<profileable
android:shell="true"
tools:targetApi="q" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,7 @@
package com.google.samples.apps.iosched

import android.app.Application
import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy.Builder
import com.google.samples.apps.iosched.shared.analytics.AnalyticsHelper
import com.google.samples.apps.iosched.util.CrashlyticsTree
import com.jakewharton.threetenabp.AndroidThreeTen
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber
import javax.inject.Inject

/**
* Initialization of libraries.
*/
@HiltAndroidApp
class MainApplication : Application() {

// Even if the var isn't used, needs to be initialized at application startup.
@Inject lateinit var analyticsHelper: AnalyticsHelper

override fun onCreate() {
// ThreeTenBP for times and dates, called before super to be available for objects
AndroidThreeTen.init(this)

// Enable strict mode before Dagger creates graph
if (BuildConfig.DEBUG) {
enableStrictMode()
}
super.onCreate()

if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
} else {
Timber.plant(CrashlyticsTree())
}
}

private fun enableStrictMode() {
StrictMode.setThreadPolicy(
Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build()
)
}
}
class MainApplication : Application()
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ class AppModule {
@Provides
fun provideAnalyticsHelper(
@ApplicationScope applicationScope: CoroutineScope,
@ApplicationContext context: Context,
signInDelegate: SignInViewModelDelegate,
preferenceStorage: PreferenceStorage
): AnalyticsHelper =
FirebaseAnalyticsHelper(applicationScope, context, signInDelegate, preferenceStorage)
FirebaseAnalyticsHelper(applicationScope, signInDelegate, preferenceStorage)

@Singleton
@Provides
Expand Down
Loading

0 comments on commit 6f96c15

Please sign in to comment.