← Back to Blog Off-Topic

An adb logcat One-Liner That Shows Only Your App's Logs

A small shell routine that cuts out the noise from a normal Android logcat stream.


logcat by itself is useless

If you've tried to debug a JNI leak or a native crash on a device, you know running adb logcat is painful. The system floods your screen with background logging:

  • battery status

  • Wi-Fi signal updates

  • package and notification events

Finding one engine line in that is basically impossible.

A self-ending debug loop

To speed up build-deploy-test cycles, I chained a few standard commands into a one-shot debugging command that cleans the buffer, relaunches the app, captures only its logs, and then exits.

Here's the command to drop into your terminal setup:

adb shell am force-stop your.package.identifier && \
sleep 1 && \
adb logcat -c && \
adb shell am start your.package.identifier/.AndroidLauncher && \
sleep 3 && \
timeout 5 adb logcat --pid=$(adb shell pidof -s your.package.identifier) | head -300


What each part does:

  • am force-stop and logcat -c: kill any running copy of the app on the device and clear the old log buffer.

  • am start and sleep 3: launch the activity and wait 3 seconds for the engine and splash to come up.

  • timeout 5: the important part. It kills the logcat capture exactly 5 seconds in, so the command doesn't hang forever.

  • --pid=$(adb shell pidof -s ...): looks up the live PID of your app and filters the stream to show only its logs.

  • head -300: caps output at the first 300 lines, enough to see the startup and asset-loading sequence.

Make it an alias

Retyping the full package name every time is tedious. Put it in your ~/.bashrc or ~/.zshrc as a function:

debug_mobile() {
    local ADB="adb" # Update path if adb is not globally exported
    local PKG="your.package.identifier"
    
    $ADB shell am force-stop $PKG && sleep 1 && $ADB logcat -c
    $ADB shell am start $PKG/${PKG}.AndroidLauncher && sleep 3
    
    local PID=$($ADB shell pidof -s $PKG)
    if [ -n "$PID" ]; then
        timeout 5 $ADB logcat --pid=$PID | head -300
    else
        echo "Error: App failed to boot or crashed instantly during setup!"
    fi
}


Now the whole build, deploy, launch, and capture step is one command: debug_mobile.