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.

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.

16. July 2017

Android Studio: Failure INSTALL_FAILED_TEST_ONLY

Starting application from Android Studio may fail with the mysterious message: Installation failed with message INSTALL_FAILED_TEST_ONLY.

The IDE will prompt you whether you want to uninstall the existing application.

Even this uninstallation may fail. When you open Run console, you can see something like this:

$ adb shell pm uninstall rocks.georgik.sdlapp
$ adb shell pm install -r "/data/local/tmp/rocks.georgik.sdlapp"
	pkg: /data/local/tmp/rocks.georgik.sdlapp
Failure [INSTALL_FAILED_TEST_ONLY]

$ adb shell pm uninstall rocks.georgik.sdlapp
DELETE_FAILED_INTERNAL_ERROR
Error while Installing APK

Potential Fix #1

There is a relatively simple fix to this issue. Click drop-down menu with your configuration and choose Edit Configurations…

Select tab General and add -t to Install Flags field. Click Ok.

Now start the application again and it should work.

Potential fix #2

This error migh occur if you moved the project from other computer where it was stored in different directory. To resolve the problem: Clean the project and build it again.

If this article didn’t help you to resolve your issue with Android Studio please let me know in comments below. There might be also other reasons for the error and we can discuss them.

5. July 2017

SDL2 application for Android built by Gradle 4

SDL2 is a well known library for making games or interactive application for Linux, Windows or macOS. The library has also support for mobile devices. Building application for Android requires a little bit more effort than build for desktops.

Let’s go step-by-step.

First of all download Gradle 4 and add bin directory with gradle to your PATH environment variable.

Download and install Android Studio.

Clone sdl2-android-example – branch gradle-4-using-android-experimental-plugin project from GitHub.

Open the repo in Android Studio and open SDK Manager. Go to Tools, select menu Android, and select item SDK Manager.

In the lower right corner check option Show Package Details. Install Android SDK Build Tool e.g. version 28.0.0 and NDK.

When you open the sdl2-android-example you will see following structure. There are two gradle files in the top level directory. The first is build.gradle. The file contains a dependency on gradle-experimental plugin which is recommended for NDK builds for Android.

buildscript {
    repositories {
       jcenter()
    }
    dependencies {
        // Android native build plugin compatible with Gradle 4
        classpath 'com.android.tools.build:gradle-experimental:0.11.0-alpha-preview-02'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

The second Gradle file is settings.gradle. The file contains a list of modules which are in the project. One module is SDL2 library. Then there are two further modules app and main. The app module is the bridge between Java and SDL2. The main module contains C code for the application.

gradle.ext.sdkVersion = 21

include ':app'
include ':SDL2'
include ':main'

These two files provide a skeleton for the build of the application. Each module SDL2, app and main are stored in the directory that matches the name a of module. Each module contains its own build.gradle which describes how to build the module.

The module file SDL2/build.gradle is interesting because it contains many excludes. It does not make sense to build all C files from SDL2 for Android.

The module file main/build.gradle contains information for linking the C application with SDL2 library.

The module file app/build.gradle contains information how to wrap all the C source code into final application.

First of all, it is necessary to build the libraries (.so files):

gradle distributeLib

If you want to build just one library, you can type:

gradle :SDL2:distributeLib

Gradle will build all flavors. If you’re experiencing problems with Android Studio, just sync the project after executing this command from command line.

Now you can deploy the application to a device or an emulator. Go to Gradle projects, expand :app module, expand Tasks, expand install and double click installDebug target.

We will discuss how to use further SDL2 libraries in next articles. You can find more under topic SDL2.

Note: the sample projet at GitHub already contains reference to other libraries like PNG. Please follow instructions described in the article SDL2_image for Android with PNG image format. The other option is to clone older version of application without PNG.