Posts

Android Interview Preparation Doc

 These are the questions usually asked for Junior or Mid level Android Developers. I have attended and shadowed multiple interviews and created this collection. You can provide comment on the Google Docs itself. https://docs.google.com/document/d/e/2PACX-1vSNX6a2NfGyGsYLPTL9qCt7Fw45Gv3UGVnlLbj1Mo8I056gmg4VpTt3bhhJsYLIgHysz65So_3iBXcy/pub

Enabling Internet in Android Emulator

Image
 This experiment is run on a Mac Laptop. For Windows, the file path may be different but the commands should be essentially the same. 1. cd to the location: `~/Library/Android/sdk/emulator` Now view the list of available emulators with: ` ./emulator -list-avds` Say you want to enable internet in the first device Nexus_5_API_30,  make sure the emulator isn't running(it's closed), then run the following command: ` ./emulator -avd Nexus_5_API_30 -dns-server 8.8.8.8 -read-only` Now the emulator starts and you can browse internet in it.  Most importantly you can now sign in to Google Play store and perform various operations, like in my case I needed to pair a phone emulator to a watch emulator which needed Wear OS app to be installed on the phone and that needed Google Play Store login. Let me know if this works for you or not.

View Binding in Activity and RecyclerView

Image
 You can read what's view binding from the official documentation. I am presenting a TL;DR of how to do it in an Activity and RecyclerView.  This is more of a reference document than a tutorial. This is how the project structure looks like: In the app level build.gradle file inside the android block add the following: buildFeatures { viewBinding = true } And then gradle sync your project. Let me know if you have any comments, question or concerns. Sulav

Capturing Screen of Android Device using ADB

 Make sure the android device is connected. adb devices Run the following command and start performing actions on your emulator or physical device. adb shell screenrecord /sdcard/example.mp4  Once you are done, press [Ctrl] + [C] to stop the recording. Pull the video from the device to your computer using: adb pull /sdcard/example.mp4 If you want to convert the mp4 to gif, run this command: ffmpeg -i example.mp4 example.gif

ADB Cheat Sheet

Take screenshot of the only emulator/device, and copy the screenshot to clipboard(so we can paste it directly somewhere) adb exec-out screencap -p > screen.png && osascript -e 'set the clipboard to POSIX file "'$(pwd)'/screen.png"' If you want to add it as an alias, in ~/.zprofile file(or whichever file it is for you), add these lines: function ss() { adb exec-out screencap -p > screen.png && osascript -e "set the clipboard to POSIX file \"$(pwd)/screen.png\"" } Lists all settings adb shell settings list global enables "show taps" on the UI adb shell settings put system show_touches 1 To set animation duration to 10x adb shell settings put global animator_duration_scale 10 adb shell settings put global window_animation_scale 10 adb shell settings put global transition_animation_scale 10 set the battery state as unplugged adb shell dumpsys battery unplug To set the battery percentage adb shell dumpsys batter

Debugging Android app with ADB command

Image
1. First find the emulators that are active. adb devices 2. You can specify the device ID or serial number to start debugging in a particular emulator/device, but I am considering you have only one emulator. Close other emulators or disconnect other devices if you have any extra. 3. Find the packages installed on the emulator so you can get the package name: adb shell pm list packages Use grep command to find the exact package name. 4. Set the app to debug at startup: adb shell am set-debug-app -w com.sulav.carowner Start the app on the emulator by clicking on the launcher icon. You will get a popup saying it's waiting for a debugger to be attached. In the Android studio, go to Run -> Attach Debugger to Android Process and select the particular process. Now if you have kept breakpoint on the code, it will stop at that point.

ADB over Wi-Fi

Image
  To debug the behavior of your application while an accessory is plugged in, you can't use ADB through a USB cable. In this case, to debug applications, you need to link your device to ADB over Wi-Fi. Steps to configure the ADB connection over Wi-Fi Connect the device and the computer to the same Wi-Fi network. If the computer is connected to VPN, disconnect from it. The device can be on VPN. Plug the device into the computer with a USB cable to configure the connection On the computer command line type:  adb tcpip 5555 On the computer command line type:  adb shell ip addr show wlan0  and copy the IP address after the "inet" until the "/". You can also go inside the Settings of the device to retrieve the IP address in Settings → About → Status. On the computer command line type:  adb connect ip-address-of-device:5555 You can disconnect the USB cable from the device and check with  adb devices  that the device is still detected. adb over wifi