6. July 2019

Web request command line generator

Tool for generating commands for web requests in different technologies.

3. July 2019

Android Studio: How to display compiler error output

When build in Android Studio fails with following error:

Compilation failed; see the compiler error output for details.

You can display error output by click on Toggle view icon on left side of Build panel.

Then you can walk through build errors and navigate to problematic parts of source code in Android Studio.

11. June 2019

How to start D-Bus in Docker container

Many Linux applications require D-Bus which is not running by default in Docker containers.

When you attempt to start the application you might get the following error:

D-Bus library appears to be incorrectly set up; failed to read machine uuid: Failed to open "/var/lib/dbus/machine-id": No such file or directory
See the manual page for dbus-uuidgen to correct this issue.
  D-Bus not built with -rdynamic so unable to print a backtrace
Aborted

First of all you need to generate missing machine-id by command:

dbus-uuidgen > /var/lib/dbus/machine-id

The application will be able to start even when the daemon is not running.

To start the D-Bus daemon inside the container you need to run following command on Ubuntu:

mkdir -p /var/run/dbus
dbus-daemon --config-file=/usr/share/dbus-1/system.conf --print-address

Command on Centos:

dbus-daemon --config-file=/etc/dbus-1/system.conf --print-address

Now the daemon is running and your application should be able to start. The output should look like this:

unix:path=/var/run/dbus/system_bus_socket,guid=9cfabcc6f66027251e092e955d09e707

8. June 2019

How to add custom mapping of a hostname to IP address in Android Emulator

There is a simple trick which allows your application to resolve a hostname to IP address defined by you even in cases when you do not have access to DNS server. This trick could be also used when you’re connected via VPN and the emulator is not able to resolve DNS record available only in private network.

Let’s add following hostname record which should be resolved by application on Android:

192.168.1.2 test.georgik.rocks

First of all, you need to start your AVD with parameter ‘-writable-system’, because emulator’s filesystem is by default read-only.

Let’s get names of available AVDs.

Example for macOS:

cd ~/Library/Android/sdk/tools
./emulator -avd -list

The output might look like:

Pixel_2_API_23
Pixel_2_API_24

Start the emulator:

./emulator -avd "Pixel_2_API_23" -writable-system

You should see the following warning:

emulator: WARNING: System image is writable

The emulator will start. Now you need to remount the filesystem so it will become writable. This could be done via adb tool which is in platform-tools directory.

Version for macOS:

cd ~/Library/Android/sdk/platform-tools
./adb remount

Now you can start the shell and append the line with configuration to /etc/hosts:

./adb shell 'echo "192.168.1.2 test.georgik.rocks" >> /etc/hosts'

Now you can test the configuration by ping:

root@generic_x86_64:/etc # ping test.georgik.rocks
PING test.georgik.rocks (192.168.1.2) 56(84) bytes of data.

7. June 2019

How to solve Android Emulator java.io.FileNotFoundException open failed: EACCES (Permission denied)

Following error in Android application is very annoying and you may waste several hours by hunting the root cause:

D/file: java.io.FileNotFoundException: /storage/emulated/0/Download/20190604_084533.jpg: 
open failed: EACCES (Permission denied)

It might occur even when the manifest is correct and contains proper permission:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Here is sample code which will pass on file existence check, but the execution will fail on readBytes:

var file:File = File("/storage/emulated/0/Download/20190604_084533.jpg")
if (file.exists()) {
    val content:ByteArray;
    Log.d("file", "exist")
    try {
        content = f.readBytes()
     } catch (FileNotFoundException) {
        Log.d("file", e.toString())
     }

} else {
     Log.d("file", "does not exist")
}

To fix this problem it is necessary to go to Settings – Apps – My App – Permission.

Access to Storage is probably disabled. Tap Storage to enable it:

With enabled Storage option, the application was able to read the file.

To solve this problem in a proper way, it’s necessary to add a request for permission to the code. Here is a sample in Kotlin:

val MY_READ_EXTERNAL_REQUEST : Int = 1
if (checkSelfPermission(
    Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), MY_READ_EXTERNAL_REQUEST)
}

You can find more details about requesting permissions in Android documentation.