Update Benchmark & Startup to use the androidx.tracing.

Test: Ran unit tests.
Change-Id: I51d7d2911d1a5c12887858ebb3cfe949356363aa
diff --git a/benchmark/common/api/1.1.0-alpha01.ignore b/benchmark/common/api/1.1.0-alpha01.ignore
new file mode 100644
index 0000000..44a73f7
--- /dev/null
+++ b/benchmark/common/api/1.1.0-alpha01.ignore
@@ -0,0 +1,3 @@
+// Baseline format: 1.0
+RemovedClass: androidx.benchmark.TraceCompatKt:
+    Removed class androidx.benchmark.TraceCompatKt
diff --git a/benchmark/common/api/1.1.0-alpha01.txt b/benchmark/common/api/1.1.0-alpha01.txt
index 26eb848..61b357d 100644
--- a/benchmark/common/api/1.1.0-alpha01.txt
+++ b/benchmark/common/api/1.1.0-alpha01.txt
@@ -14,8 +14,5 @@
   public static final class BenchmarkState.Companion {
   }
 
-  public final class TraceCompatKt {
-  }
-
 }
 
diff --git a/benchmark/common/api/current.txt b/benchmark/common/api/current.txt
index 26eb848..61b357d 100644
--- a/benchmark/common/api/current.txt
+++ b/benchmark/common/api/current.txt
@@ -14,8 +14,5 @@
   public static final class BenchmarkState.Companion {
   }
 
-  public final class TraceCompatKt {
-  }
-
 }
 
diff --git a/benchmark/common/api/public_plus_experimental_1.1.0-alpha01.txt b/benchmark/common/api/public_plus_experimental_1.1.0-alpha01.txt
index e87771e..907fabb 100644
--- a/benchmark/common/api/public_plus_experimental_1.1.0-alpha01.txt
+++ b/benchmark/common/api/public_plus_experimental_1.1.0-alpha01.txt
@@ -21,8 +21,5 @@
   @kotlin.annotation.Retention(AnnotationRetention.BINARY) @kotlin.annotation.Target(allowedTargets=AnnotationTarget.FUNCTION) public static @interface BenchmarkState.Companion.ExperimentalExternalReport {
   }
 
-  public final class TraceCompatKt {
-  }
-
 }
 
diff --git a/benchmark/common/api/public_plus_experimental_current.txt b/benchmark/common/api/public_plus_experimental_current.txt
index e87771e..907fabb 100644
--- a/benchmark/common/api/public_plus_experimental_current.txt
+++ b/benchmark/common/api/public_plus_experimental_current.txt
@@ -21,8 +21,5 @@
   @kotlin.annotation.Retention(AnnotationRetention.BINARY) @kotlin.annotation.Target(allowedTargets=AnnotationTarget.FUNCTION) public static @interface BenchmarkState.Companion.ExperimentalExternalReport {
   }
 
-  public final class TraceCompatKt {
-  }
-
 }
 
diff --git a/benchmark/common/api/restricted_1.1.0-alpha01.ignore b/benchmark/common/api/restricted_1.1.0-alpha01.ignore
new file mode 100644
index 0000000..44a73f7
--- /dev/null
+++ b/benchmark/common/api/restricted_1.1.0-alpha01.ignore
@@ -0,0 +1,3 @@
+// Baseline format: 1.0
+RemovedClass: androidx.benchmark.TraceCompatKt:
+    Removed class androidx.benchmark.TraceCompatKt
diff --git a/benchmark/common/api/restricted_1.1.0-alpha01.txt b/benchmark/common/api/restricted_1.1.0-alpha01.txt
index 26eb848..61b357d 100644
--- a/benchmark/common/api/restricted_1.1.0-alpha01.txt
+++ b/benchmark/common/api/restricted_1.1.0-alpha01.txt
@@ -14,8 +14,5 @@
   public static final class BenchmarkState.Companion {
   }
 
-  public final class TraceCompatKt {
-  }
-
 }
 
diff --git a/benchmark/common/api/restricted_current.txt b/benchmark/common/api/restricted_current.txt
index 26eb848..61b357d 100644
--- a/benchmark/common/api/restricted_current.txt
+++ b/benchmark/common/api/restricted_current.txt
@@ -14,8 +14,5 @@
   public static final class BenchmarkState.Companion {
   }
 
-  public final class TraceCompatKt {
-  }
-
 }
 
diff --git a/benchmark/common/build.gradle b/benchmark/common/build.gradle
index edbc120..047827b 100644
--- a/benchmark/common/build.gradle
+++ b/benchmark/common/build.gradle
@@ -31,6 +31,7 @@
     implementation(KOTLIN_STDLIB)
     api("androidx.annotation:annotation:1.0.0")
     api("androidx.annotation:annotation-experimental:1.0.0-rc01")
+    implementation(project(':tracing:tracing-ktx'))
     implementation(ANDROIDX_TEST_MONITOR)
 
     androidTestImplementation(ANDROIDX_TEST_RULES)
diff --git a/benchmark/common/src/main/java/androidx/benchmark/BenchmarkState.kt b/benchmark/common/src/main/java/androidx/benchmark/BenchmarkState.kt
index 10c0751..4cee094 100644
--- a/benchmark/common/src/main/java/androidx/benchmark/BenchmarkState.kt
+++ b/benchmark/common/src/main/java/androidx/benchmark/BenchmarkState.kt
@@ -27,6 +27,7 @@
 import androidx.annotation.VisibleForTesting
 import androidx.benchmark.Errors.PREFIX
 import androidx.test.platform.app.InstrumentationRegistry
+import androidx.tracing.Trace
 import java.io.File
 import java.text.NumberFormat
 import java.util.concurrent.TimeUnit
@@ -176,7 +177,7 @@
     }
 
     private fun beginWarmup() {
-        beginTraceSection("Warmup")
+        Trace.beginSection("Warmup")
         // Run GC to avoid memory pressure from previous run from affecting this one.
         // Note, we don't use System.gc() because it doesn't always have consistent behavior
         Runtime.getRuntime().gc()
@@ -217,12 +218,12 @@
         thermalThrottleSleepSeconds = 0
         state = RUNNING
         metrics.captureInit()
-        beginTraceSection("Benchmark")
+        Trace.beginSection("Benchmark")
         metrics.captureStart()
     }
 
     private fun endBenchmark() {
-        endTraceSection() // paired with start in beginBenchmark()
+        Trace.endSection() // paired with start in beginBenchmark()
         when (Arguments.profilingMode) {
             ProfilingMode.Sampled, ProfilingMode.Method -> {
                 Debug.stopMethodTracing()
@@ -364,7 +365,7 @@
 
                 throwIfPaused() // check each loop during warmup
                 if (warmupManager.onNextIteration(lastDuration)) {
-                    endTraceSection() // paired with start in beginWarmup()
+                    Trace.endSection() // paired with start in beginWarmup()
                     beginBenchmark()
                 }
                 return true
diff --git a/benchmark/common/src/main/java/androidx/benchmark/TraceCompat.kt b/benchmark/common/src/main/java/androidx/benchmark/TraceCompat.kt
deleted file mode 100644
index 491fe42..0000000
--- a/benchmark/common/src/main/java/androidx/benchmark/TraceCompat.kt
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright 2019 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
- *
- *      http://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 androidx.benchmark
-
-import android.os.Build
-import android.os.Trace
-import androidx.annotation.RestrictTo
-
-/*
- * Simple reimplementation of TraceCompat, to avoid androidx.core dependency for two methods
- *
- * Note:
- * Benchmark isn't especially careful about terminating tracing sections, because our APIs are
- * called *from* the long-running synchronous chunks of work, instead of wrapping them.
- *
- * We try and match up begi/end pairs, but it's entirely possibly that ends will be missed due to
- * the app returning early from the benchmark loop (whether intentionally, or by crashing).
- *
- * Because there's no elegant way to fix this, we just accept that crashing benchmarks can result in
- * never-terminating traces from benchmark.
- */
-
-/**
- * @suppress
- */
-@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
-fun beginTraceSection(sectionName: String) {
-    if (Build.VERSION.SDK_INT >= 18) {
-        Trace.beginSection(sectionName)
-    }
-}
-
-/**
- * @suppress
- */
-@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
-fun endTraceSection() {
-    if (Build.VERSION.SDK_INT >= 18) {
-        Trace.endSection()
-    }
-}
\ No newline at end of file
diff --git a/benchmark/junit4/build.gradle b/benchmark/junit4/build.gradle
index 8aef6fb..639c57a 100644
--- a/benchmark/junit4/build.gradle
+++ b/benchmark/junit4/build.gradle
@@ -33,6 +33,7 @@
 
 dependencies {
     api(project(":benchmark:benchmark-common"))
+    implementation(project(':tracing:tracing-ktx'))
 
     api(JUNIT)
     api(KOTLIN_STDLIB)
diff --git a/benchmark/junit4/src/main/java/androidx/benchmark/junit4/BenchmarkRule.kt b/benchmark/junit4/src/main/java/androidx/benchmark/junit4/BenchmarkRule.kt
index 9d149e9..21aa740 100644
--- a/benchmark/junit4/src/main/java/androidx/benchmark/junit4/BenchmarkRule.kt
+++ b/benchmark/junit4/src/main/java/androidx/benchmark/junit4/BenchmarkRule.kt
@@ -20,9 +20,8 @@
 import android.util.Log
 import androidx.annotation.RestrictTo
 import androidx.benchmark.BenchmarkState
-import androidx.benchmark.beginTraceSection
-import androidx.benchmark.endTraceSection
 import androidx.test.rule.GrantPermissionRule
+import androidx.tracing.trace
 import org.junit.Assert.assertTrue
 import org.junit.rules.RuleChain
 import org.junit.rules.TestRule
@@ -188,11 +187,8 @@
             internalState.traceUniqueName = description.testClass.simpleName + "_" +
                     invokeMethodName
 
-            try {
-                beginTraceSection(description.displayName)
+            trace(description.displayName) {
                 base.evaluate()
-            } finally {
-                endTraceSection()
             }
 
             if (enableReport) {
diff --git a/jetifier/jetifier/core/src/main/resources/default.config b/jetifier/jetifier/core/src/main/resources/default.config
index 493210a..9b14a9d 100644
--- a/jetifier/jetifier/core/src/main/resources/default.config
+++ b/jetifier/jetifier/core/src/main/resources/default.config
@@ -318,11 +318,11 @@
             "to": "androidx/core/content/res/GrowingArrayUtils{0}"
         },
 
-	{
+        {
             "from": "android/support/v4/content/res/ColorStateListInflaterCompat(.*)",
             "to": "androidx/core/content/res/ColorStateListInflaterCompat{0}"
         },
-	{
+        {
             "from": "android/support/v7/content/res/AppCompatColorStateListInflater(.*)",
             "to": "androidx/core/content/res/ColorStateListInflaterCompat{0}"
         },
diff --git a/jetifier/jetifier/migration.config b/jetifier/jetifier/migration.config
index c0471a6..d585d2e 100644
--- a/jetifier/jetifier/migration.config
+++ b/jetifier/jetifier/migration.config
@@ -441,6 +441,10 @@
       "to": "ignore"
     },
     {
+      "from": "androidx/tracing/(.*)",
+      "to": "ignore"
+    },
+    {
       "from": "androidx/fragment/app/DefaultSpecialEffectsController(.*)",
       "to": "android/support/v4/app/DefaultSpecialEffectsController{0}"
     },
diff --git a/startup/startup-runtime/build.gradle b/startup/startup-runtime/build.gradle
index 6a4c794..45014d0 100644
--- a/startup/startup-runtime/build.gradle
+++ b/startup/startup-runtime/build.gradle
@@ -39,6 +39,7 @@
 
 dependencies {
     implementation("androidx.annotation:annotation:1.1.0")
+    implementation(project(':tracing:tracing-ktx'))
     lintPublish(project(':startup:startup-runtime-lint'))
     androidTestImplementation(KOTLIN_STDLIB)
     androidTestImplementation(ANDROIDX_TEST_EXT_JUNIT)
diff --git a/startup/startup-runtime/src/main/java/androidx/startup/AppInitializer.java b/startup/startup-runtime/src/main/java/androidx/startup/AppInitializer.java
index 1d925bd..bda6dcb 100644
--- a/startup/startup-runtime/src/main/java/androidx/startup/AppInitializer.java
+++ b/startup/startup-runtime/src/main/java/androidx/startup/AppInitializer.java
@@ -24,6 +24,7 @@
 import android.os.Bundle;
 
 import androidx.annotation.NonNull;
+import androidx.tracing.Trace;
 
 import java.util.HashMap;
 import java.util.HashSet;
@@ -102,11 +103,11 @@
             @NonNull Class<? extends ComponentInitializer<?>> component,
             @NonNull Set<Class<?>> initializing) {
         synchronized (sLock) {
-            boolean isTracingEnabled = TraceCompat.isEnabled();
+            boolean isTracingEnabled = Trace.isEnabled();
             try {
                 if (isTracingEnabled) {
                     // Use the simpleName here because section names would get too big otherwise.
-                    TraceCompat.beginSection(component.getSimpleName());
+                    Trace.beginSection(component.getSimpleName());
                 }
                 if (initializing.contains(component)) {
                     String message = String.format(
@@ -147,7 +148,7 @@
                 }
                 return (T) result;
             } finally {
-                TraceCompat.endSection();
+                Trace.endSection();
             }
         }
     }
@@ -155,7 +156,7 @@
     @SuppressWarnings("unchecked")
     void discoverAndInitialize() {
         try {
-            TraceCompat.beginSection(SECTION_NAME);
+            Trace.beginSection(SECTION_NAME);
             ApplicationInfo applicationInfo =
                     mContext.getPackageManager()
                             .getApplicationInfo(mContext.getPackageName(), GET_META_DATA);
@@ -183,7 +184,7 @@
         } catch (PackageManager.NameNotFoundException | ClassNotFoundException exception) {
             throw new StartupException(exception);
         } finally {
-            TraceCompat.endSection();
+            Trace.endSection();
         }
     }
 }
diff --git a/startup/startup-runtime/src/main/java/androidx/startup/TraceCompat.java b/startup/startup-runtime/src/main/java/androidx/startup/TraceCompat.java
deleted file mode 100644
index 16e896b..0000000
--- a/startup/startup-runtime/src/main/java/androidx/startup/TraceCompat.java
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * Copyright 2020 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
- *
- *      http://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 androidx.startup;
-
-import android.os.Build;
-import android.os.Trace;
-import android.util.Log;
-
-import androidx.annotation.NonNull;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-
-/**
- * Writes trace events to the system trace buffer.  These trace events can be
- * collected and visualized using the Systrace tool.
- *
- * <p>This tracing mechanism is independent of the method tracing mechanism
- * offered by {@link android.os.Debug#startMethodTracing}.  In particular, it enables
- * tracing of events that occur across multiple processes.
- * <p>For information about using the Systrace tool, read <a
- * href="{@docRoot}studio/profile/systrace/">Overview of system tracing</a>.
- */
-final class TraceCompat {
-
-    private static final String TAG = "TraceCompat";
-
-    private static long sTraceTagApp;
-    private static Method sIsTagEnabledMethod;
-
-    static {
-        if (Build.VERSION.SDK_INT >= 18 && Build.VERSION.SDK_INT < 29) {
-            try {
-                Field traceTagAppField = Trace.class.getField("TRACE_TAG_APP");
-                sTraceTagApp = traceTagAppField.getLong(null);
-                sIsTagEnabledMethod = Trace.class.getMethod("isTagEnabled", long.class);
-            } catch (Exception e) {
-                Log.i(TAG, "Unable to initialize via reflection.", e);
-            }
-        }
-    }
-
-    /**
-     * Checks whether or not tracing is currently enabled. This is useful to avoid intermediate
-     * string creation for trace sections that require formatting. It is not necessary
-     * to guard all Trace method calls as they internally already check this. However it is
-     * recommended to use this to prevent creating any temporary objects that would then be
-     * passed to those methods to reduce runtime cost when tracing isn't enabled.
-     *
-     * @return true if tracing is currently enabled, false otherwise
-     */
-    public static boolean isEnabled() {
-        if (Build.VERSION.SDK_INT >= 29) {
-            return Trace.isEnabled();
-        } else if (Build.VERSION.SDK_INT >= 18) {
-            try {
-                return (boolean) sIsTagEnabledMethod.invoke(null, sTraceTagApp);
-            } catch (Exception e) {
-                Log.v(TAG, "Unable to invoke isTagEnabled() via reflection.");
-            }
-        }
-
-        // Never enabled on < API 18
-        return false;
-    }
-
-    /**
-     * Writes a trace message to indicate that a given section of code has begun. This call must
-     * be followed by a corresponding call to {@link #endSection()} on the same thread.
-     *
-     * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
-     * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
-     * these characters they will be replaced with a space character in the trace.
-     *
-     * @param sectionName The name of the code section to appear in the trace.  This may be at
-     * most 127 Unicode code units long.
-     */
-    public static void beginSection(@NonNull String sectionName) {
-        if (Build.VERSION.SDK_INT >= 18) {
-            Trace.beginSection(sectionName);
-        }
-    }
-
-    /**
-     * Writes a trace message to indicate that a given section of code has ended. This call must
-     * be preceded by a corresponding call to {@link #beginSection(String)}. Calling this method
-     * will mark the end of the most recently begun section of code, so care must be taken to
-     * ensure that beginSection / endSection pairs are properly nested and called from the same
-     * thread.
-     */
-    public static void endSection() {
-        if (Build.VERSION.SDK_INT >= 18) {
-            Trace.endSection();
-        }
-    }
-
-    private TraceCompat() {}
-}
diff --git a/tracing/tracing-ktx/src/main/AndroidManifest.xml b/tracing/tracing-ktx/src/main/AndroidManifest.xml
index 976de72..ede3fba 100644
--- a/tracing/tracing-ktx/src/main/AndroidManifest.xml
+++ b/tracing/tracing-ktx/src/main/AndroidManifest.xml
@@ -15,6 +15,6 @@
   -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="androidx.tracing">
+    package="androidx.tracing.ktx">
 
 </manifest>
diff --git a/ui/settings.gradle b/ui/settings.gradle
index fc7f4a4..49e753b 100644
--- a/ui/settings.gradle
+++ b/ui/settings.gradle
@@ -43,6 +43,8 @@
 includeProject(":compose:compose-runtime-benchmark", "../compose/compose-runtime/compose-runtime-benchmark")
 includeProject(":compose:compose-runtime:samples", "../compose/compose-runtime/samples")
 includeProject(":lint-checks", "../lint-checks")
+includeProject(":tracing:tracing", "../tracing/tracing")
+includeProject(":tracing:tracing-ktx", "../tracing/tracing-ktx")
 includeProject(":ui:integration-tests:benchmark", "integration-tests/benchmark")
 includeProject(":ui:integration-tests:demos", "integration-tests/demos")
 includeProject(":ui:integration-tests:test", "integration-tests/test")