Gather and interpret Android performance evidence on an adb target using Simpleperf CPU profiles, Perfetto or Compose traces, gfxinfo frame data, dumpsys meminfo snapshots, Java heap dumps, and native allocation traces. Use when asked to profile an Android app flow, find CPU-heavy functions, diagnose jank, capture startup or frame timing evidence, compare before/after performance, explain what code is taking time, or gather memory/leak profiling artifacts.
../android-emulator-qa/SKILL.md for device selection, build/install/launch, UI driving, screenshots, UI trees, and logcat capture.if [ -z "${ARTIFACT_DIR:-}" ]; then
ARTIFACT_DIR="$(mktemp -d "${TMPDIR:-/tmp}/codex-android-perf.XXXXXX")"
fi
mkdir -p "$ARTIFACT_DIR"
ARTIFACT_DIR under SKILL_DIR; the skill folder is for bundled instructions and scripts, not run artifacts.--app works best when the installed package is debuggable or profileable from shell. Preflight before recording:SERIAL="<adb-serial>"
PACKAGE="<app package>"
adb -s "$SERIAL" shell dumpsys package "$PACKAGE" | grep -Ei 'DEBUGGABLE|profileable|isProfileable' || true
simpleperf record --app fails, install a debug/profileable build when possible. If that is not possible, use Perfetto or gfxinfo instead of treating missing CPU samples as evidence.SERIAL="<adb-serial>"
PACKAGE="<app package>"
MAX_DURATION_SECONDS=60
adb -s "$SERIAL" shell rm -f /data/local/tmp/perf.data
adb -s "$SERIAL" logcat -c
adb -s "$SERIAL" shell simpleperf record \
--app "$PACKAGE" \
-o /data/local/tmp/perf.data \
-e cpu-clock -f 4000 -g \
--duration "$MAX_DURATION_SECONDS"
android-emulator-qa.adb -s "$SERIAL" shell 'pid="$(pidof simpleperf 2>/dev/null || true)"; [ -n "$pid" ] && kill -INT $pid'
Operation not permitted, send Ctrl-C to the original adb shell simpleperf record command session and wait for it to exit.adb -s "$SERIAL" pull /data/local/tmp/perf.data "$ARTIFACT_DIR/perf.data"
adb -s "$SERIAL" logcat -d > "$ARTIFACT_DIR/logcat.txt"
SKILL_DIR="<absolute path to this loaded skill folder>"
FIRST_PARTY_REGEX="$(printf '%s' "$PACKAGE" | sed 's/\./\\./g')"
"$SKILL_DIR/scripts/simpleperf_hotspots.sh" \
"$ARTIFACT_DIR/perf.data" \
"$ARTIFACT_DIR" \
--serial "$SERIAL" \
--first-party-regex "$FIRST_PARTY_REGEX"
SKILL_DIR from the target app repo's pwd; installed plugins usually live outside the app being profiled. Keep FIRST_PARTY_REGEX scoped to the app's package or app-owned module prefixes; avoid broad framework patterns such as kotlin, Compose, or androidx.compose when reporting app-owned rows.$ARTIFACT_DIR/simpleperf-self.txt$ARTIFACT_DIR/simpleperf-children.txt$ARTIFACT_DIR/simpleperf.csv when supported by the installed Simpleperfadb shell simpleperf report when the device still has /data/local/tmp/perf.data.gfxinfo, or repeated wall-clock measurements.Trace sections for PACKAGE; it is not a substitute for a full project-specific Perfetto config when you need detailed frame timeline or Compose runtime internals.SERIAL="<adb-serial>"
PACKAGE="<app package>"
TRACE_DURATION_SECONDS=30
TRACE_BASENAME="app-flow-$(date +%Y%m%d-%H%M%S).pftrace"
TRACE_DEVICE="/data/misc/perfetto-traces/$TRACE_BASENAME"
PERFETTO_PID="$(adb -s "$SERIAL" shell perfetto \
--background-wait \
-o "$TRACE_DEVICE" \
-t "${TRACE_DURATION_SECONDS}s" \
--app "$PACKAGE" \
sched freq idle am wm gfx view binder_driver hal dalvik | tr -d '\r' | tail -n 1)"
printf 'Perfetto PID: %s\n' "$PERFETTO_PID"
TRACE_DURATION_SECONDS expires. To stop early, gracefully terminate the background Perfetto process and give it a moment to flush:adb -s "$SERIAL" shell kill -TERM "$PERFETTO_PID" 2>/dev/null || true
adb -s "$SERIAL" shell "
last_size=-1
stable_count=0
i=0
while [ \$i -lt 30 ]; do
size=\$(ls -l '$TRACE_DEVICE' 2>/dev/null | awk '{ print \$5 }')
if [ -n \"\$size\" ] && [ \"\$size\" -gt 0 ] && [ \"\$size\" = \"\$last_size\" ]; then
stable_count=\$((stable_count + 1))
[ \$stable_count -ge 2 ] && exit 0
else
stable_count=0
fi
last_size=\"\${size:-0}\"
i=\$((i + 1))
sleep 1
done
exit 1
"
TRACE_DURATION_SECONDS expire instead of stopping early. If the stop command fails because the trace already ended, still wait until the output file exists and its size is stable before pulling. If the direct command is too coarse, use Android Studio System Trace or a project-specific Perfetto config. Only report frame timeline or Compose recomposition details when those tracks/events are actually present in the captured trace; the light command above does not guarantee them.adb -s "$SERIAL" pull "$TRACE_DEVICE" "$ARTIFACT_DIR/$TRACE_BASENAME"
SERIAL="<adb-serial>"
PACKAGE="<app package>"
adb -s "$SERIAL" shell pidof "$PACKAGE"
adb -s "$SERIAL" shell dumpsys window | grep -F "$PACKAGE"
adb -s "$SERIAL" shell dumpsys gfxinfo "$PACKAGE" reset
# Perform the focused flow.
adb -s "$SERIAL" shell dumpsys gfxinfo "$PACKAGE" > "$ARTIFACT_DIR/gfxinfo.txt"
adb -s "$SERIAL" shell dumpsys gfxinfo "$PACKAGE" framestats > "$ARTIFACT_DIR/gfxinfo-framestats.txt"
dumpsys gfxinfo fails to dump the process, or the device shows an ANR/dialog/splash screen instead of the flow, discard that capture and use Perfetto for root cause.SERIAL="<adb-serial>"
PACKAGE="<app package>"
adb -s "$SERIAL" shell am force-stop "$PACKAGE"
adb -s "$SERIAL" shell monkey -p "$PACKAGE" 1
# Exercise the focused flow, then navigate back to a stable idle screen.
adb -s "$SERIAL" shell dumpsys meminfo "$PACKAGE" > "$ARTIFACT_DIR/meminfo-flow.txt"
TOTAL PSS, Java heap, native heap, graphics, Views, Activities, binder counts, and object counts. Treat one noisy sample as a lead, not a conclusion.HEAP="/data/local/tmp/app-flow.hprof"
HPROF="$ARTIFACT_DIR/app-flow.hprof"
if ! command -v shark-cli >/dev/null; then
echo "Install Shark CLI, or analyze the HPROF with Android Studio Profiler / MAT." >&2
fi
adb -s "$SERIAL" shell am dumpheap -g "$PACKAGE" "$HEAP"
adb -s "$SERIAL" pull "$HEAP" "$HPROF"
adb -s "$SERIAL" shell rm -f "$HEAP"
if command -v shark-cli >/dev/null; then
shark-cli --hprof "$HPROF" analyze | tee "$ARTIFACT_DIR/shark-analysis.txt"
fi
shark-analysis.txt first when it exists. Report suspected leaking objects, retained sizes, and reference chains. Look for retained feature objects, activities, fragments, view models, Compose state holders, repositories, listeners, callbacks, and caches that should have been released after leaving the flow. If Shark CLI is unavailable, still preserve the HPROF path and inspect it with the best available heap analyzer; do not claim leak roots from meminfo alone.perfetto rejects -t together with --config.TRACE_DEVICE="/data/misc/perfetto-traces/native-alloc.pftrace"
adb -s "$SERIAL" shell perfetto -o "$TRACE_DEVICE" \
--txt -c - <<EOF
duration_ms: 60000
buffers { size_kb: 262144 fill_policy: RING_BUFFER }
data_sources {
config {
name: "android.heapprofd"
heapprofd_config {
sampling_interval_bytes: 65536
shmem_size_bytes: 8388608
block_client: true
process_cmdline: "$PACKAGE"
}
}
}
EOF
adb -s "$SERIAL" pull "$TRACE_DEVICE" "$ARTIFACT_DIR/native-alloc.pftrace"
trace_processor_shell and save the outputs:SKILL_DIR="<absolute path to this loaded skill folder>"
"$SKILL_DIR/scripts/heapprofd_reports.sh" \
"$ARTIFACT_DIR/native-alloc.pftrace" \
"$ARTIFACT_DIR"
heapprofd-summary.txt, heapprofd-top-allocations.txt, heapprofd-top-stack.txt, heapprofd-health.txt, and meminfo together. Report net native allocation size, top allocating frames/mappings, the expanded stack for the largest callsite, and whether trace stats show heapprofd health issues such as client errors, packet loss, or buffer overruns. Prefer Java heap dumps for retained app objects; heapprofd is for native allocation behavior.