diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a9abd1b8..f7e2b547 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -62,13 +62,14 @@ material3-adaptive = "1.1.0" material3-adaptive-navigation-suite = "1.3.2" media3 = "1.7.1" # @keep -minSdk = "21" +minSdk = "35" okHttp = "4.12.0" playServicesWearable = "19.0.0" protolayout = "1.3.0" recyclerview = "1.4.0" -targetSdk = "34" +targetSdk = "35" tiles = "1.5.0" +tracing = "1.3.0" validatorPush = "1.0.0-alpha03" version-catalog-update = "1.0.0" wear = "1.3.0" @@ -152,6 +153,7 @@ androidx-tiles-renderer = { module = "androidx.wear.tiles:tiles-renderer", versi androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "tiles" } androidx-tiles-tooling = { module = "androidx.wear.tiles:tiles-tooling", version.ref = "tiles" } androidx-tiles-tooling-preview = { module = "androidx.wear.tiles:tiles-tooling-preview", version.ref = "tiles" } +androidx-tracing = { group = "androidx.tracing", name = "tracing", version.ref = "tracing" } androidx-wear = { module = "androidx.wear:wear", version.ref = "wear" } androidx-wear-ongoing = { module = "androidx.wear:wear-ongoing", version.ref = "wearOngoing" } androidx-wear-tooling-preview = { module = "androidx.wear:wear-tooling-preview", version.ref = "wearToolingPreview" } diff --git a/misc/build.gradle.kts b/misc/build.gradle.kts index e5cc3cc1..af5893d7 100644 --- a/misc/build.gradle.kts +++ b/misc/build.gradle.kts @@ -57,6 +57,7 @@ dependencies { implementation(libs.androidx.compose.ui.util) implementation(libs.androidx.compose.ui.tooling.preview) implementation(libs.androidx.compose.material3) + implementation(libs.androidx.tracing) implementation(libs.hilt.android) implementation(libs.androidx.hilt.navigation.compose) diff --git a/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerJavaSnippets.java b/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerJavaSnippets.java new file mode 100644 index 00000000..4dae4d35 --- /dev/null +++ b/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerJavaSnippets.java @@ -0,0 +1,73 @@ +package com.example.snippets.profiling; + +import android.app.Activity; +import android.os.Bundle; +import android.util.Log; +import java.util.function.Consumer; +import java.util.concurrent.Executor; +import android.os.ProfilingResult; +import java.util.concurrent.Executors; +import android.os.CancellationSignal; +import androidx.tracing.Trace; +import androidx.core.os.Profiling; +import androidx.core.os.SystemTraceRequestBuilder; +import androidx.core.os.BufferFillPolicy; + +public class ProfilingManagerJavaSnippets { + public class MainActivityJava extends Activity { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + sampleRecordSystemTrace(); + } + + // [START android_profiling_manager_record_system_trace_java] + void heavyOperation() { + // Computations you want to profile + } + + void sampleRecordSystemTrace() { + Executor mainExecutor = Executors.newSingleThreadExecutor(); + Consumer resultCallback = + new Consumer() { + @Override + public void accept(ProfilingResult profilingResult) { + if (profilingResult.getErrorCode() == ProfilingResult.ERROR_NONE) { + Log.d( + "ProfileTest", + "Received profiling result file=" + profilingResult.getResultFilePath()); + } else { + Log.e( + "ProfileTest", + "Profiling failed errorcode=" + + + profilingResult.getErrorCode() + + " errormsg=" + + profilingResult.getErrorMessage()); + } + } + }; + CancellationSignal stopSignal = new CancellationSignal(); + + SystemTraceRequestBuilder requestBuilder = new SystemTraceRequestBuilder(); + requestBuilder.setCancellationSignal(stopSignal); + requestBuilder.setTag("FOO"); + requestBuilder.setDurationMs(60000); + requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER); + requestBuilder.setBufferSizeKb(20971520); + Profiling.requestProfiling(getApplicationContext(), requestBuilder.build(), mainExecutor, + resultCallback); + + // Wait some time for profiling to start. + + Trace.beginSection("MyApp:HeavyOperation"); + heavyOperation(); + Trace.endSection(); + + // Once the interesting code section is profiled, stop profile + stopSignal.cancel(); + } + // [END android_profiling_manager_record_system_trace_java] + } +} diff --git a/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerKotlinSnippets.kt b/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerKotlinSnippets.kt new file mode 100644 index 00000000..f3f0ad99 --- /dev/null +++ b/misc/src/main/java/com/example/snippets/profiling/ProfilingManagerKotlinSnippets.kt @@ -0,0 +1,85 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * 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.example.snippets.profiling + +import android.app.Activity +import android.os.Build +import android.os.Bundle +import android.os.CancellationSignal +import android.os.ProfilingResult +import android.util.Log +import androidx.annotation.RequiresApi +import androidx.core.os.BufferFillPolicy +import androidx.core.os.SystemTraceRequestBuilder +import androidx.core.os.requestProfiling +import androidx.tracing.Trace +import java.util.concurrent.Executor +import java.util.function.Consumer +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.asExecutor + +class ProfilingManagerKotlinSnippets { + class MainActivity : Activity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + sampleRecordSystemTrace() + } + + // [START android_profiling_manager_record_system_trace_kotlin] + @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) + fun sampleRecordSystemTrace() { + val mainExecutor: Executor = + Dispatchers.IO.asExecutor() // Your choice of executor for the callback to occur on. + val resultCallback = Consumer { profilingResult -> + if (profilingResult.errorCode == ProfilingResult.ERROR_NONE) { + Log.d( + "ProfileTest", + "Received profiling result file=" + profilingResult.resultFilePath + ) + } else { + Log.e( + "ProfileTest", + "Profiling failed errorcode=" + profilingResult.errorCode + " errormsg=" + profilingResult.errorMessage + ) + } + } + val stopSignal = CancellationSignal() + + val requestBuilder = SystemTraceRequestBuilder() + requestBuilder.setCancellationSignal(stopSignal) + requestBuilder.setTag("FOO") // Caller supplied tag for identification + requestBuilder.setDurationMs(60000) + requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER) + requestBuilder.setBufferSizeKb(20971520) + requestProfiling(applicationContext, requestBuilder.build(), mainExecutor, resultCallback) + + // Wait some time for profiling to start. + + Trace.beginSection("MyApp:HeavyOperation") + heavyOperation() + Trace.endSection() + + // Once the interesting code section is profiled, stop profile + stopSignal.cancel() + } + + fun heavyOperation() { + // Computations you want to profile + } + // [END android_profiling_manager_record_system_trace_kotlin] + } +}