Skip to content

Instantly share code, notes, and snippets.

@ziadtawfeek
Last active September 13, 2021 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziadtawfeek/3dabc997f20bca0974ba4b223cae15bd to your computer and use it in GitHub Desktop.
Save ziadtawfeek/3dabc997f20bca0974ba4b223cae15bd to your computer and use it in GitHub Desktop.
The Android Debug Bridge is better known as ADB. It is a command-line utility, included in Android SDK, which allows us to interact with an Android device or emulator over a USB connection, allowing files to be transferred, installing applications, changing permissions of applications, taking screenshots, and much more.
ADB over Wi-Fi:
---------------
To connect an Android device via Wi-Fi we need to use the adb connect <ip-address> command.
We can find the current ip address by going into the "Settings > About device > Status" screen on Android device. Let's imaging that the IP address is the following: 192.168.1.42
# Connect to the device by its IP address
adb connect 192.168.1.42
# Connect to the device by its IP address with specific port
adb connect 192.168.1.42:5554
# Disconnect from the device by it's IP address
adb disconnect 192.168.1.42
Note: The adb connect and adb disconnect use a default port: 5555. If you cannot connect to the device by it's IP address, this is most probably due to your device not being able to listen for a TCP/IP connection on 5555 port
Send input event:
-----------------
We can enter any text to the selected input field by adb shell input text "<text>" command.
# all spaces should be replaced with "%s"
adb shell input text "insert%syour%stext"
# We can emulate pressing the hardware buttons with adb shell input keyevent <key|action-name> command. You can find the keys of hardware buttons here.
adb shell input keyevent 26
adb shell input keyevent POWER
Testing app with Monkey testing approach:
-----------------------------------------
ADB has a possibility for such type of testing. We can use the adb shell monkey -p <package> -v <event-count> -s <seed>. The seed parameter is needed if we want to reproduce the testing session, which in that case, similar events will be generated during the next run.
adb shell monkey -p "PACKAGE_NAME" -v 10000 -s 100
Changing permissions:
---------------------
# To grant permissions(s) we can use the adb shell pm grant <package> <permission> command.
adb shell pm grant com.alexzh.mapnotes android.permission.ACCESS_FINE_LOCATION
adb shell pm grant com.alexzh.mapnotes android.permission.ACCESS_COARSE_LOCATION
# We can grant all requested permissions to application by adding -g to the adb shell pm grant command.
adb shell pm grant -g com.alexzh.mapnotes
# To revoke permissions(s) we can use the adb shell pm revoke <package> <permission> command.
adb shell pm revoke com.alexzh.mapnotes android.permission.ACCESS_FINE_LOCATION
adb shell pm revoke com.alexzh.mapnotes android.permission.ACCESS_COARSE_LOCATION
# Note: When we install an application with the adb install <apk> command, we can add the -g parameter, and all permissions for the application will be granted.
adb install -g mapnotes.apk
Testing Deep Links:
-------------------
Deep links are one of those few things tedious to test because manually, you have to click on the links in some third party app to see if your app is correct in handling the links. We can remove this need of third party apps by testing this function through the use of adb. As mentioned in this documentation, you can run the following command
adb shell am start
-W -a android.intent.action.VIEW
-d <URI> <PACKAGE>
adb shell am start
-W -a android.intent.action.VIEW
-d "example://gizmos" com.example.android
To break this command down;
am means activity manager. To make it simple, in Android, each app has an activity or multiple activities that hosts the UI that the user can see. Activity Manager knows which apps has which activities and we can launch specific screen/activity through the activity manager.
-W means wait for the launch to be completed.
-a android.intent.action.VIEW is where we specify the action that we want to execute. In Android, we have predefined actions that the OS understand and the OS can use this information to route the acti0n to the corresponding apps. If you ever open a file and wonder where all of these apps are coming from in the popup, then the answer is through this OS feature. The OS knows which app can handle which file, i.e, which action.
-d <URI> <PACKAGE> : destination specify two parameters, first is our deep link uri, and the second one is the app we want to view this deeplink on. For the first one, replace with the link you want to test, and for the second, you can enter your app's package name.
If you run that command, the OS should launch your app. First, OS will first ask the app if it can handle this type of deep link. If the app can handle the deep link, it will receive the deeplink, and now the app is responsible for dissecting the deep link and manage it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment