Testing on a real device. Overview of tools for launching and debugging applications on Android Connecting your phone to Android Studio
At creating android applications, most developers use various emulators. This is convenient because there is no need to store a bunch of different devices in the drawers of the desk, there is no headache with connecting devices to the computer, there are no heaps of wires on the table. We click the mouse and see the work of our program on the computer screen. Simple and elegant. However, the emulator is not a phone. He is limited in his abilities. If your application uses any hardware-specific features such as camera, Bluetooth, or USB connection, work during the reception phone call, with a high degree of probability we can say that you cannot do without debugging on a real device. Fortunately, Eclipse contains everything you need to accomplish this task.
Connecting the phone and launching the program
Before connecting your phone to a computer using USB cable data transfer, you need to perform a few preparatory steps. The fact is that Eclipse is configured to work with the emulator by default, and we need to correct the debug mode configuration. It's simple. Select the menu item Run > Debug Configuration, and then DroidDebug. On the Target tab, in the Deployment Target Selection Mode section, set the switch to Manual and click Apply. So turned on manual mode platform selection. Now, when you start the application, a dialog box will appear where you can select an emulator or one of the connected phones.
In order for the phone to work in debug mode, it must also be configured accordingly. From the home screen, you need to click on the Menu button and find the Settings app. In this application, open the “Applications” section, and then the “Development” item. In the screen that opens, check the box next to “USB Debugging” (USD Debgging). This sequence of actions works on most phones, but it is quite possible that UBS debugging mode is enabled on your device somewhere else.
Connect your phone to your computer and click the Debug button in Eclipse. In the dialog that appears, you will see two lists: “Chose a running Android device” and “Launch a new Android Virtual Device”. The first of them lists the phones connected to the computer, the second - the emulators. Select your phone from the first list. Eclipse will install the application you are debugging on your phone and launch it. You can debug your program.
Phone status monitoring DDMS (Dalvik Debug Monitor Service)
All information about the state of the phone is available through a special tool called the DDMS (Dalvik Debug Monitor Service) debug monitoring service. To launch it, click the android button in the top right corner of Eclipse.
The DDMS window consists of several panes. The Devices panel displays a list of available emulators and connected phones. Here is a list of running this moment processes. Each process runs in its own virtual machine. Threads are displayed on a separate Threads tab. tab File Explorer designed to work with file system phone. With it, you can copy files from your computer to your phone and vice versa. Using the Screen Capture button, you can take a screenshot of the phone screen. The green beetle button allows you to attach the debugger to the selected process. To use this feature, you must have an open source this process.
Also of note is the LogCat logging utility and its associated panel, which can be very helpful in troubleshooting. Working with the log is carried out through a special class android.util.Log. The most commonly used methods are:
Log.e() - for error logging,
Log.w() - for logging warnings,
Log.i() - for logging information messages,
Log.d() - for debug logging,
Log.v() - for logging verbose records,
Two strings must be passed as parameters to these methods. The first is a tag that can then be used to search and filter in LogCat. The second is the actual information that is logged.
Decompiling and Debugging Android Applications
Alexander Antipov
This article will show you how to attach a debugger to an Android application and step through the methods that are called using the information obtained after decompiling the application.
This article will show you how to attach a debugger to an Android application and step through the methods that are called using the information obtained after decompiling the application. The good news is that debugging doesn't require superuser privileges. The described techniques can be very useful during pentests. mobile applications, since we can "penetrate" the code while the program is running, get and write information that we usually do not have access to. For example, you can intercept traffic before encryption and get keys, passwords, and any other valuable information on the fly. The article will be useful for pentesters and mobile application developers who want to gain deeper knowledge about possible attacks on the Android platform.
Test environment requirements:
- Operating system: Windows / Mac OS X / Linux
- Java (version 1.7 recommended)
- IDE (Eclipse, IntelliJ IDEA, Android Studio)
- Android SDK (https://developer.android.com/sdk/index.html?hl=i)
- APKTool (https://code.google.com/p/android-apktool/)/APK Studio (http://apkstudio.codeplex.com)
- Android device/emulator
This article will use the following configuration: Windows 8, Android Studio and IntelliJ IDEA. Device: Nexus 4s android version 4.4.4. I recommend adding all utilities to environment variable PATH to make it easier and faster to access these tools.
The Android application package (APK) used in this article can be downloaded from here: com.netspi.egruber.test.apk .
Device setup
The instructions below will help you prepare the device for experiments.
Activating the Developer Options section
To begin with, debugging via USB (USB debugging option) must be enabled on the Android device, which will allow you to “communicate” with the device using the tools from the Android SDK. However, before that, you must activate the Developer options section. On the device, go to the Settings > About Phone section and click several times on the Build Number item, after which a message should appear stating that the Developer options section is activated.
Figure 1: In order to activate the Developer options section, you need to click on the Build number several times
Enable debugging via USB
To enable USB debugging, go to Settings > Developer options and check the box next to USB debugging.
Figure 2: Enabling the USB debugging option
Connecting the device and running ADB
After connecting the device to the computer via the USB port, the message "USB debugging connected on the device" should appear. You should also check if you can connect to the device using the Android Debug Bridge (ADB) app included with the Android SDK ( android package SDK Platform-tools). At the command prompt, enter the following command:
The device should appear in the list.
Figure 3: List of connected devices
If the device is not displayed in the list, then the most likely reason is incorrect installed drivers(on Windows). Depending on the device, the driver can be found either in the Android SDK or on the manufacturer's website.
Checking the application for debugging
Before debugging Android applications, you first need to check if this is possible. Verification can be done in several ways.
The first way is to run Android Device Monitor, which is part of the Android SDK (in the tools folder). AT windows file called monitor.bat. When you open Android Device Monitor, the device will appear under Devices.
Figure 4: Android application Device Monitor
If any application on the device can be debugged, that application will also appear in the list. I created a test program, but the list is empty because the program cannot be debugged.
The second way to test the application for debugging is to examine the AndroidManifest.xml file from the application package (APK, Android application package). An APK is a zip archive containing all the information needed to run an app on an Android device.
Whenever an application is loaded from Google Play Store, the application package is also downloaded. All downloaded APK files are usually stored on the device in the /data/app folder. If you don't have superuser rights, you won't be able to get a list of files from the /data/app directory. Although, if you know the name of the APK file, you can copy it using the utility adb. To find out the name of the APK file, enter the following command:
The device command line will appear. Then enter the following command:
pm list packages -f
A list of all packages on the device will be displayed.
Figure 5: List of packages on the device
Looking at the list, we find a test application.
Figure 6: Package of the created test application (highlighted in white)
Now you need to copy the package file. Open a shell and enter the following command:
adb pull /data/app/[.apk file]
Figure 7: Copy the APK file from the device to the system
Now we need to open the package file and examine the contents of AndroidManifest.xml. Unfortunately, we cannot simply unpack the archive because the APK file is encoded in binary format. For decoding, the most commonly used utility is apk tool although I am using APK Studio because this app has a friendly GUI. The rest of the article will talk about APK Studio.
In APK Studio, click on the little green icon, give the project a name and path to APK file. Then specify a path to save the project.
Figure 8: Creating a new project in APK Studio
After opening the APK, select the AndroidManifest.xml file and look at the settings for the application tag. If the android:debuggable flag is not present (or present but set to false), then the application cannot be debugged.
Figure 9: Contents of the AndroidManifest.xml file
Modifying the AndroidManifest.xml file
Using the apktool utility or APK Studio, we can modify the files and pack the contents back into a package. We will now modify the AndroidManifest.xml file so that the application can be debugged. Add the line android:debuggable="true" inside the application tag.
Figure 10: Changing the content of the application tag
After adding the flag, click on the "hammer" icon and re-assemble the package. The rebuilt package will be in the build/apk directory.
Figure 11: Package rebuild completed successfully
After rebuilding, the package is signed and can be reinstalled on the device (all Android applications must be signed). Most applications do not verify the certificate used to sign. Otherwise, you need to change the code that performs this check.
Now you need to install the rebuilt package. First, remove the old application with the following command:
adb pm uninstall
Then install the new package:
adb install [.apk file]
You can also remove and install a package with one command:
adb install -r [.apk file]
Figure 12: Installing the rebuilt package
Check that the reinstalled application runs correctly on the device. If everything works, go back to the Android Device Monitor, where the test application should appear.
Figure 13: Now the rebuilt application can be debugged
Setting up the development environment (IDE)
Now you can hook a debugger to the rebuilt application, but first you need to create a project in the development environment (this article uses IntelliJ IDEA). We create a new project. Specify an arbitrary name in the Application name field. In the Package name field, specify a name that exactly matches the folder hierarchy of the rebuilt package.
Figure 14: Creating a new project in IntelliJ IDEA
Usually the name of the APK file matches the folder structure, although if you're not sure, in APK Studio check the directory hierarchy up to the folder where the application files are located. In my case, the name and folder structure are exactly the same (com.netspi.egruber.test).
Figure 15: Test application directory hierarchy
Uncheck "Create Hello World Activity" and finish creating the project (all other options remain default). The new project should look something like this:
Figure 16: Folder and file hierarchy of the new project
After creating the project, you need to add the source code from the APK file so that the debugger "knows" the names of symbols, methods, variables, etc. The good news is that Android applications can be decompiled with almost no quality loss (the source code will match with the original). After decompilation, the source code is imported into the development environment (IDE).
Obtaining Sources from an Application Package
First you need to convert the APK to a jar file. Then, using the java decompiler, we will get the source code of the application. We will do the conversion to jar using the utility dex2jar. dex2jar has a d2j-dex2jar.bat file used to convert APK to jar. The command syntax is quite simple:
d2j-dex2jar.bat [.apk file]
Figure 17: Converting APK to jar
Then we open or drag the resulting file into the JD-GUI (this is a java decompiler).
Figure 18: Structure of the jar file
The jar file should be displayed as a hierarchical structure, inside which there are java files with readable source code. Go to File > Save All Sources to pack all sources into a zip archive.
Figure 19: Saving the sources of the decompiled file
After saving the source texts, unpack the archive into a separate directory.
Figure 20: Unpacked archive
Now you need to import both directories into the project created earlier in the IDE. In IntelliJ, go to the src folder and copy the contents of the unpacked archive (two directories) there.
Figure 21: Both folders copied to the src directory
Returning to Intellij, we see the updated project.
Figure 22: Source texts appeared in the project
If we click on any element from the list, we will see the original text. As you can see in the screenshot below (source code of the LoginActivity class), the source code has been obfuscated using ProGuard.
Figure 23: Obfuscated source code for the LoginActivity class
Connecting a debugger
Now that the project has source code, we can start setting breakpoints on methods and variables. When the breakpoints are reached, the application will stop. As an example, I set a breakpoint on the method (right in the obfuscated code) responsible for processing the information entered in the text field.
Figure 24: Set a breakpoint on an obfuscated method
As soon as a breakpoint appears, we connect the debugger to the process on the device by clicking on the icon with the screen in the right upper corner(the icon may be different on your IDE).
Figure 25: Attaching a debugger to a process
Figure 26: List of processes for attaching a debugger
After selecting a process, the debugger will connect to the device.
Figure 27: Debugger attached to a process running on the device
In the text field, I will enter the number 42 (if you remember, there is a breakpoint on the corresponding method).
Figure 28: Enter the number 42 in the text box
After pressing the "Enter Code" button, the application execution will break at the breakpoint, because the debugger is "aware" which method is being called on the device. A compiled Android application contains debugging information (such as variable names) available to any Java Debug Wire Protocol (JDWP) compliant debugger. If debugging is enabled in the application, a JDWP-compatible debugger (most of the debuggers that come with Java development environments fall into this category) will be able to connect to the Android application's virtual machine and then read and execute debugging commands.
Figure 29: Breakpoint hit
The screenshot below shows the number that we previously entered in the text box.
Figure 30: List of variables of the current instance of the class
Conclusion
We can not only read the data in the application, but also insert our own. This can be useful if, for example, we want to interrupt the flow of code execution and bypass some parts of the algorithm. With the help of a debugger, we can better understand the logic of the application and analyze what is not available ordinary user. For example, it can be very useful to view the encryption functions used and the dynamic keys. Also, sometimes when debugging, it's useful to know how functions interact with the file system or database in order to understand what information is stored by the application. Such manipulations are available on any Android device without superuser privileges.
The basic steps for creating an Android emulator for testing the created applications were shown. Using an emulator is, of course, quite convenient, but only if you have enough powerful computer and loading the emulator, as well as its work, do not hinder a lot of time. But not everyone is lucky enough to have such monster machines, so in this article I'll show you a pretty good way to test your work in Android Studio with a real connected device.
This article will suit you in 2 main cases:
1) Your computer is not powerful enough to run the emulator quickly (be it in Android Studio or Eclipce);
2) You have an old one lying around Android smartphone and you don't know where to put it (it's a pity to give/throw away).
It is also worth noting that this method can also be useful for owners of productive computers, because testing on an emulator is one thing, and observing the application on a real smartphone is quite another.
So, in order to connect your smartphone to Android Studio, you first need to install the required set of drivers. As a rule, torment with a standard set of drivers often will not lead to anything, so I recommend downloading this file right away (don't be afraid - it's not a virus, although the antivirus swears) and, having previously removed the old drivers, install this file (during installation, the phone it is better not to connect).
After installing the drivers, connect your device to the computer via USB. All the necessary settings will start and automatically be installed.
Next, launch Android Studio. Go to Tools ---Android and check the box next to the line "Enable ADB Integration" (ADB - Android Debug Bridge). After that, you need to configure Android Studio so that when you click on the green "Run" button, your application is immediately installed and launched on the connected smartphone. Go to Run --- Edit Configurations. The following window appears:
and in the "Target Device" block, check the "USB Device" box and click OK.
That's all, congratulations, if you did everything correctly, the drivers detected your device, then when you click on the application launch button (highlighted in red):
the application will install and run on the connected device.
After that, you just have to pick up the connected device and test your programming work.
It is worth noting that if the application was installed, but did not automatically start, then most likely this means that there is an error somewhere in the application (although it may not be highlighted in Android Studio, for example, an important piece of code is simply missed).
I hope this article is useful to you. Good luck in your endeavors!
The goal of this chapter is to write a minimal Android application. But we will never know for sure if we could write something workable without trying to run it on a real device. This is what we will do in this article.
The ability to test on a smartphone is provided by ADB (Android Debug Bridge). In this article, we will set it up and run our app on a real smartphone.
What is adb
Android Debug Bridge (ADB) is a versatile command line tool that facilitates communication between a development environment, in our case Android Studio, and AVD emulators or physical Android devices in order to be able to run and debug applications.
ADB consists of a client, a server that runs as a background process on the developer's machine, and a daemon that runs as a background process on each instance of the emulator or real device.
Setting up an Android device to work with ADB
In order to use ADB with a device connected via USB, you need to enable USB debugging in system settings phone or tablet in Developer Options (name may vary). On some devices, this section is hidden by default. Consider the steps in the case when the required settings section is not available.
- Go to settings, section "About device"
- Find the item "Build number" and click on it 7 times. A window should appear informing you that the developer mode is activated. The developer options section should now appear in the settings.
- Enable the "USB debugging" option in it.
Now, when you connect your device to your computer, depending on the model, you may have a new connection option.
Setting up ADB on Windows
At Windows setup First, make sure you have Google USB Driver installed. Go to the SDK Manager in the Extras section and find the Google USB Driver, install it if necessary.
![](https://i2.wp.com/aristov-vasiliy.ru/upload/medialibrary/bd3/bd3210ad30cdd6a6ab9a82caf772d25e.jpg)
Now you should update the driver. Connect your device to your computer, go to Control Panel -> Hardware and Sound -> Device Manager, find your device. Right click on your device to open context menu and select "Update Drivers...". The driver can be found in the sdk directory under \(sdk directory)\extras\google\usb_driver.
How to check if ADB settings are correct?
To check if ADB is working, connect the device to the computer, run it in the \(sdk directory)\platform-tools folder command line and enter the command in it:
adb devices
A list like this should appear:
List of devices attached 5cdb2ced device
Running the application on a real device
Everything is the same as in the case of running on an emulator. Open our project in Android Studio, click on the green triangle, but in the device selection window that appears, select your device.
![](https://i0.wp.com/aristov-vasiliy.ru/upload/medialibrary/175/175fb5b7a6e37a4892861f7dcd523aa0.jpg)
If it says that the device is offline, replug the USB and enable USB debugging on the computer:
![](https://i2.wp.com/aristov-vasiliy.ru/upload/medialibrary/0c4/0c46784b759b3ebfc7d015a4dcabac44.jpg)
As a result, our application will appear on the screen of your phone or tablet.
![](https://i2.wp.com/aristov-vasiliy.ru/upload/medialibrary/c75/c75db190be9d2ca22c337a6633aaeb25.jpg)
Conclusion
This is where the chapter ends. We have been successful: we have been able to set up our system for developing Android applications and even run one of them on a real device.
If something is actively failing for you or you are confused, please unsubscribe in the comments and I will help you deal with your problem.
Earlier I wrote . You can solve the problem radically, do not use the emulator, but test immediately on mobile device running android. I have a wonderful HTC, and set it up for it. So, in order to run applications on physical device need:
- Enable USB debugging on your phone. You can do this by going to Settings -> Applications -> Development and checking the box "USB debugging"
- Wait for the driver to install
- Disconnect and reconnect your phone to the computer.
To check that the phone is visible to the computer, you need to do the following:
If this is the case for you, then you can use special program: PdaNet. () Usually it is used to use the Internet from the phone from the computer, but it just so happens that it contains a huge number of drivers for phones. During installation, I was asked for the device model, and then a beautiful red balun popped up with a warning that the driver was not tested, and everything could end badly, but no - everything is fine, it was installed. After that, I ran the check again (the command above) and my phone appeared in the list of devices.
And how to work from Eclipse? In fact, everything has already been done - just launch the project and it opens on the phone.