🤖 How to fix an android emulator which does not connect to internet?


Sometimes, the Android emulator does not connect to internet, this might happen if there is a custom DNS server on the network.
This has become a classical emulator issue, but there is no simple way to quickly fix it other than using the emulator’s UI to set static local IP address along with a new DNS inside the network settings.


A quick fix is to run the emulator with a custom DNS using command line. This might not be the best long-term fix, but it might come handy in some situations.

# Get a list of android emulators on your device
emulator -list-avds

# Run an emulator with DNS set to (Google)
emulator -avd Pixel_2_API_30 -dns-server

This will open the emulator with DNS already set to “”.

🐘 How to add consistency between local and CI environments when running Android instrumented tests?

🧐 The Why

Hi everyone, ever wondered your tests run locally without any issues but fail on the CI(Continuous Integration) server? This can have many reasons such as different local environment compared to the CI environment. For example if you’re using an M1 Mac to develop your Android app vs. an x86 linux environment on the CI. Or maybe different emulator versions, especially the emulator version on the CI can get old and someone might forget to update them.

In November 2022, the Gradle managed devices were introduced for Android. It hopefully would make running tests more reliable by adding consistency between local and CI environments.

The Gradle task will simply take over anything related to creating the emulator, running the instrumented tests, and providing the test results in the build folder. The task will even automatically detect the CPU type of the environment and sets up the device accordingly, that means same config can run on a local ARM environment and on a x86 CI environment without changes. This is all simpler, requires less configuration, and gradle tasks are more familiar to Android developers. Also it is supported by the Android team at Google.

🛠️ The How

All one has to do to configure a Gradle-managed device, is to add the device in the module’s build.gradle file:

android {
  testOptions {
    managedDevices {
      devices {
        maybeCreate<com.android.build.api.dsl.ManagedVirtualDevice>("pixel2api31").apply {
          device = "Pixel 2"
          apiLevel = 31
          systemImageSource = "google"

Now, if you have a build variant named .debug, you can run the Android instrumented tests for it all in one line:

./gradlew pixel2api31DebugAndroidTest

This will run a headless Android emulator from the command line, runs your Android instrumented tests on it.

📁 How to export the coverage file from the managed device?
It’s very simple!
Once the task is finished, you can find the output files such as your test coverage files (e.g. Jacoco .xml or .ec coverage files) under the following path:


// for example, if you have enabled jacoco to produce an .ec coverage file, it will be at the following path:

🎁 Bonus

If you are working with a CI system with MacOS environments and intel CPUs, or you just want to simulate it on your local environment, the following bash script can silently install android sdk, accept the licenses, install the intel HAXM for virtualisation and run the instrumented tests on a Gradle managed device:

# install android commandline tools and required packages
brew install --cask android-commandlinetools
yes | sdkmanager "tools" "platform-tools" "build-tools;33.0.1" "platforms;android-33" "system-images;android-31;google_apis;x86_64" "extras;intel;Hardware_Accelerated_Execution_Manager"

# accept licenses
yes | sdkmanager --licenses
export ANDROID_HOME=/usr/local/share/android-commandlinetools
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools

# install intel haxm
cd $ANDROID_HOME/extras/intel/Hardware_Accelerated_Execution_Manager/
chmod +x silent_install.sh
sudo ./silent_install.sh
/usr/local/share/android-commandlinetools/tools/emulator -accel-check
cd ~/project/

# run the instrumented tests using gradle managed devices
./gradlew pixel2api31DebugAndroidTest

More Info about using Gradle Managed Devices on Android Official Docs.

Please leave a comment for your questions and feedbacks. 🙂

How to take screenshot of Composable functions in Jetpack Compose

Assuming a drawing app that allows users to export a canvas, or a simple image editing app that users can share the edited photo, there might be limited ways to convert a composable to a bitmap.

In this post, I will share my experience of using standard methods to convert a composable into a bitmap and store it as a PNG in internal storage.

According to official docs you can access the view version of your composable function using LocalView.current, and export that view to a bitmap file like this (the following code goes inside the composable function):

    val view = LocalView.current
    val context = LocalContext.current

    val handler = Handler(Looper.getMainLooper())
    handler.postDelayed(Runnable {
        val bmp = Bitmap.createBitmap(view.width, view.height,
            Bitmap.Config.ARGB_8888).applyCanvas {
        bmp.let {
            File(context.filesDir, "screenshot.png")
                .writeBitmap(bmp, Bitmap.CompressFormat.PNG, 85)
    }, 1000)

The writeBitmap method is a simple extension function for File class. Example:

private fun File.writeBitmap(bitmap: Bitmap, format: Bitmap.CompressFormat, quality: Int) {
    outputStream().use { out ->
        bitmap.compress(format, quality, out)

If you liked this approach, vote for the stackoverflow post. Looking forward to read your feedback soon! 🙂

Arduino USB Terminal

During the isolation period caused by Corona, I was playing around with Arduino as a quite small hobby in my free time to create a simple robot that is being controlled by Android. 🙂

The robot uses an Arduino and a Motor Shield to control the motors, but since I am an Android developer, I wanted to somehow make use of Android to control it. 📱🤖

The concept is simple: There is an Android phone on the robot which is connected to the Arduino via a Bluetooth cable and sends commands to Arduino to control the motors.

Because of that, I had to test the USB connection between the Android phone and the Arduino, so I created a simple terminal to achieve this. But the terminal can be used for many more purposes than testing this robot, so I decided to publish it as an open-source project on GitHub and also a free app on Google Play Store:

Get it on Google Play

The robot itself, is still under construction 🚧 🔨 and I will probably send a photo of it when it is done, but until then, please feel free to use the new Terminal and I am looking forward to receiving your feedback. 😉

Unit testing Android LiveData using Espresso with Kotlin? Not a mess anymore!

After searching a lot about how to test the live data returned by Room DAOs for unit testing, I found it would be a good idea to share it with other people. The language is Kotlin and I am using Robolectric to mock the context; But here, I just focus on the problem itself and I’m not gonna explain how to write the tests. You can of course check the links provided in the text.


I also have to mention that there are also other workarounds to achieve this, like using Mockito, but honestly in my opinion, this was the most clean approach. But if you prefer those ones, good luck with that. 😊


What did I want to do? Check if the LiveData objects returned by a DAO method are correct; And to have 100% test coverage on those files. 😉

What I did? Created a Kotlin extension function for LiveData object in my test class to block the main thread and wait for the observer to call onChange method and return the value. How? Using CountdownLatch. Read the answer to this Stack Overflow post.

private fun <T> LiveData<T>.blockingObserve(): T? {

    var value: T? = null
    val latch = CountDownLatch(1)
    val observer = Observer<T> {

        t ->
        value = t


    latch.await(2, TimeUnit.SECONDS)
    return value

The Problem: Although CountDownLatch is used to block the thread and wait for the value, but it would not block the thread and the value was always null. This is probably because of LiveData’s main feature which is usually being used in the main thread and obviously, it is not supposed to block the main thread, never!

Solution: Well, that’s pretty easy! You just need to add this rule to your test class:

val rule = InstantTaskExecutorRule()

It will allow the liveData observer to block the main thread and Viola!

Just don’t forget to add the core-testing dependency which is part of the Architecture library to your project and also match its version to the version of your current Architecture library that you’re using and everything will work like a charm:

testImplementation "android.arch.core:core-testing:1.1.1"

The answer to this post on StackOverflow also explains it.

Now the tests should work fine and as a reminder, I am running the tests as Local Unit Tests using Robolectric and not as Android Instrumented tests.

Good Luck Unit Testing 😊

New Site Migration

The old blog is now migrated to wordpress.com as a  casual weblog. 😎 –NEAT!
The old posts may seem a little bit unorganized since the last website was using the self-hosted version of WordPress with many plugins to support different languages and blah blah! 😀  Why am I keeping such content cram? Just in case I’m gonna miss my old posts about Assembly x86 code and the whole NOOB-ish content I wrote back then. (As well as some of my students in my university workshops when I was their Teaching Assistant!).

So I hope you accept my apology. 🙂 Thank you. And now that I’m a free human being from all those WordPress self-hosted stuff, I’m gonna focus only on posting Android-Dev related content. So whenever there’s something to say about the beautiful and colorful world of Android Development, I’ll post it here with NO hesitation!

It’s gonna be fun everyone. So please don’t hesitate to share your opinions with me. 👍

Kotlin, the second official programming language for Android

Kotlin was being used in Android by Android Developers unofficially using libraries like ANKO. Yesterday, Kotlin was introduced as a second officially supported Android programming language in Google I/O.

Here are some links and resources to presentations and tools which I found useful for getting started with Kotlin for Android.

Here is an introduction presented by Wojtek Kaliciński which gives you a better perspective about how to use Kotlin in Android Studio 3.0:
Android Tool Time: Getting Started with Kotlin

I also found this presentation from Hadi Hariri in CERN a good resource to get up and running with Kotlin in about 45 minutes, I really encourage you to watch it.
Kotlin – Ready for production by Hadi Hariri

And finally download the new version of Android Studio 3.0 Canary from the official preview website.
Android Studio Preview Download

I hope these resources help jumpstart your next Android project using Kotlin and improve your productivity in Android Development.

Kotlin, die zweite offizielle Programmiersprache für Android

Kotlin wurde in Android von Android Developers inoffiziell mit Bibliotheken wie ANKO verwendet. Gestern wurde Kotlin als zweite offiziell unterstützte Android-Programmiersprache in Google I / O eingeführt.

Hier sind einige Links und Ressourcen zu Präsentationen und Tools, die ich für den Einstieg in Kotlin für Android nützlich fand.

Hier ist eine Einführung von Wojtek Kaliciński, die Ihnen eine bessere Perspektive über die Verwendung von Kotlin in Android Studio 3.0 bietet:
Android Tool Time: Getting Started with Kotlin

Ich habe auch diese Präsentation von Hadi Hariri im CERN eine gute Quelle gefunden, um mit Kotlin in ca. 45 Minuten aufzustehen, ich ermutige Sie wirklich, es zu sehen.
Kotlin – Ready for production by Hadi Hariri

Und schließlich laden Sie die neue Version von Android Studio 3.0 Canary aus der offiziellen Vorschau-Website herunter.
Android Studio Preview Download

Ich hoffe, diese Ressourcen helfen Jumpstart Ihre nächste Android-Projekt mit Kotlin und verbessern Sie Ihre Produktivität in Android Development.