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.

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.

23. April 2019

Android Studio Unresolved reference: activity_main – R.layout and R.id

You can easily run into mysterious errors with Android Studio. Here is one.

Suddenly all references related to R.layout or R.id stopped to work and build failed with a message like this:

Compilation error. See log for more details
Unresolved reference: activity_main
Unresolved reference: url_edittext
Unresolved reference: update_button

The code looked like this:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

I tried clean, builds, rebuilds. Nothing helped. The problem was caused by simple import which accidentally occurred in Activity file:

import android.R

After removing this line the whole project was working again and even code hinting was correct.

15. April 2019

Android Emulator application crash: Chromium WebView package does not exist

Android application with WebView might crash with a strange error in Android Emulator:

E/WebViewFactory: Chromium WebView package does not exist
    android.webkit.WebViewFactory$MissingWebViewPackageException: Failed to load WebView provider: No WebView installed
        at android.webkit.WebViewFactory.getWebViewContextAndSetProvider(WebViewFactory.java:334)
        at android.webkit.WebViewFactory.getProviderClass(WebViewFactory.java:398)
        at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:211)
....

Check the definition of Virtual Device. It’s very likely that your device is using plain Android target which does not contain the WebView. You have to create Virtual Device which contains Google APIs.

Click Create Virtual Device. Select an image with “Google APIs“:

Now you can start the application in the newly created emulator with Google APIs.

The application should run without a problem.

20. February 2019

openSUSE firewall – YaST cannot continue the configuration

When you want to configure the firewall on openSUSE you can just type yast into the terminal.

Select Security and Users and from options select Firewall.

On fresh installation of openSUSE you might encounter following error:

Error
YaST cannot continue the configuration
without installing the required packages.

The fix is simple, just install yast2-firewall:

sudo zypper install yast2-firewall

Now you can start YaST and the firewall configuration will work.