Android Studio 3.0 allows you to profile and debug APKs without having to build them from an Android Studio project. However, you need to make sure you’re using a debuggable version of the APK.
To start debugging an APK, click Profile or debug APK from the Android Studio Welcome screen. Or, if you already have a project open, click File > Profile or debug APK from the menu bar. In the next dialog window, select the APK you want to import into Android Studio and click OK. You should see something similar to figure 1.
Note: When you import an APK into Android Studio, the IDE creates a new project in your home directory and makes a local copy of the target APK. This means that if you rebuild or update the original APK, you need to manually import the updated version into Android Studio again.
The Android view in the Project pane allows you to inspect the following contents of your APK:
- APK file: Double-clicking the APK opens the APK analyzer.
- manifests: The application manifests, extracted from the APK.
- java: Contains Java code that Android Studio disassembles (into
.smalifiles) from your APK’s DEX files. Each
.smalifile in this directory corresponding to a Java class.
- cpp: If your app includes native code, this directory contains your
APK’s native libraries (
- External Libraries: Contains the Android SDK.
You can immediately use the Android profiler to start testing your app’s performance—however, the profiler currently works only with your app’s Java processes. To debug your app’s Java and native code using breakpoints, you need to first attach the Java sources and attach native debug symbols.
Attach Java sources
By default, Android Studio extracts Java code from your APK and saves them as
.smali files. To debug the Java code using breakpoints, you need to attach
the Java sources to each
.smali file you want to debug.
To attach Java sources, proceed as follows:
- Double click on a
.smalifile from the Project pane (use the Android view). After opening the file, the editor displays a yellow warning banner asking you to select the Java sources, as shown in figure 1.
- Click Attach Java Sources at the top of the editor window.
- Navigate to the directory with the app’s Java source files and click Open.
In the Project window, the IDE replaces
.smali files with their
.java files. You can now add breakpoints and
debug your app as you normally would.
Attach native debug symbols
If your APK includes native libraries (
.so files) that don’t include debug
symbols, the IDE shows you a warning in the Messages window, similar to
the one shown in figure X. You can not debug the APK’s native code or use
breakpoints without attaching debuggable native libraries. If you haven’t
already done so, make sure to
download the NDK and tools.
To attach debuggable native libraries, proceed as follows:
- Under the cpp directory in the Project window, double click a native library file that doesn’t include debug symbols. The editor shows a table of all the ABIs your APK supports.
- Click Add at the top right corner of the editor window.
- Navigate to the directory that includes the debuggable native libraries you want to attach and click OK.
- If the APK and debuggable native libraries were built using a different workstation, you need to specify local paths to the debug symbols by following these steps:
- Add local paths to missing debug symbols by editing the field under the Local Paths column in the Path Mappings section of the editor window, as shown in figure 2. The IDE automatically maps paths to a remote NDK to paths in your local NDK download.
- Click Apply.
You should now see the native source files in the Project window. Open those native files to add breakpoints and debug your app as you normally would.
Known issue: When attaching debug symbols to an APK, both the APK and
.so files must be built using the same workstation or