Keywords: Android | Debugging | Android Studio | ADB
Abstract: This technical article provides a comprehensive guide on enabling debugging for Android apps in Android Studio, focusing on setting build variants to debug mode, using the debug toolbar icon, and incorporating additional tips from community answers and official documentation. It systematically addresses common issues, such as app not being recognized as debuggable, with step-by-step solutions, code examples, and advanced techniques like breakpoint management and Logcat usage to enhance developer productivity.
Introduction
Debugging is a critical aspect of Android app development, yet many developers face challenges when trying to debug apps in Android Studio, particularly after migrating from Eclipse. Common issues include the device being recognized but the app not marked as debuggable, preventing breakpoints from firing or logs from displaying. This article systematically resolves these problems based on high-scoring Stack Overflow answers and official documentation.
Core Solution: Configure Build Variants
In Gradle-based Android Studio projects, the traditional approach of adding <application android:debuggable="true"> to the AndroidManifest.xml file is deprecated and may not work. Instead, use the build variants system. Navigate to Build > Select Build Variant in the menu, and choose the "debug" variant from the dropdown panel. This ensures the build includes debugging symbols and allows the debugger to attach properly.
If custom build types are defined, specify the debug property in the build.gradle file. For example, in Groovy script:
android {
buildTypes {
customDebugType {
debuggable true
// other configurations
}
}
}In Kotlin script:
android {
buildTypes {
create("customDebugType") {
isDebuggable = true
// other configurations
}
}
}Using the Debug Toolbar Icon
Android Studio features a dedicated debug icon on the toolbar, resembling a bug and located next to the run icon. Clicking this icon initiates a debug session for your app; if the app is already running, you may be prompted to restart it in debug mode. This method is straightforward and often resolves debugging issues by automatically applying the correct build variant.
Additional Troubleshooting Steps
Drawing from community answers, the following actions can help address debugging problems:
- Reset ADB Integration: Go to Tools > Android > Disable ADB Integration, then re-enable it. Unplug and replug the USB connection to your device, and try debugging with Shift + F9.
- Avoid Enabling Proguard for Debug: Ensure Proguard is not enabled for debug builds, as it can obfuscate code and remove debug information. Check your build.gradle file and set
minifyEnabled falsefor debug variants if necessary. - Restart ADB Server: In some cases, the ADB server may need a restart. Open a terminal and run
adb kill-serverfollowed byadb start-serverto refresh the connection.
Advanced Debugging Techniques
Referencing official documentation, first enable debugging on your device by going to Settings > Developer options and turning on USB debugging. Set breakpoints in your code by clicking the gutter next to the line number or using the shortcut Control+F8 (Command+F8 on Mac). The debug window allows for variable inspection, expression evaluation, and stepping through code. For logging, use the Log class in your code, for example:
import android.util.Log;
public class MyActivity extends Activity {
private static final String TAG = MyActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
Log.d(TAG, "onCreate() Restoring previous state");
} else {
Log.d(TAG, "onCreate() No saved state available");
}
}
}View outputs in the Logcat window. For apps with native code, select the appropriate debug type in the run configuration (e.g., Detect Automatically or Dual) to handle Java and C/C++ debugging. Utilize watchpoints to monitor memory access and customize resource display formats in the variables pane.