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.

13. June 2018

Android Studio NDK could not start mips64el-linux-android-strip

If you’re using Android Studio to build C++ application with NDK you might encounter following error during build:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformNativeLibsWithStripDebugSymbolForDebug'.
...
Caused by: java.io.IOException: Cannot run program ".../Android/Sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/linux-x86_64/bin/mips64el-linux-android-strip": 
error=2, No such file or directory

The problem is caused by the upgrade of NDK in Android Studio to NDK v17. This version has a different structure of tools. It’s not compatible with Android Experimental Plugin v0.11.

The directory which should contain binary of tools contains just file NOTICE-MIPS64:

This mips64el-linux-android-4.9 directory exists to make the NDK compatible with the Android
SDK's Gradle plugin, version 3.0.1 and earlier, which expects the NDK
to have a MIPS64 toolchain directory.

The solution is to download NDK v16 from NDK older releases. Extract it and replace former ndk-bundle directory.

Commands for macOS or Linux users:

cd ~/Android/Sdk
unzip ~/Downloads/android-ndk-r16b-linux-x86_64.zip
mv ndk-bundle ndk-bundle-v17
mv android-ndk-r16b ndk-bundle

If you’re Windows user the location of NDK is typically in your profile AppData\Local\Android. Do not forget to unblock the ZIP after downloading (right click, Properties, Unblock, Ok)

Commands for Windows users:

cd ~\AppData\Local\Android\Sdk
unzip ~\Downloads\android-ndk-r16b-windows-x86_64.zip
mv ndk-bundle ndk-bundle-v17
mv android-ndk-r16b ndk-bundle

Start Android Studio. It should prompt you to update NDK. Do not confirm this request, it will replace NDK with v17. You should see the following screen in SDK Manager:

You can find related sample source code at GitHub in sdl2-android-example – branch gradle-4-using-android-experimental-plugin repository. Further articles about SDL2 and Android are available under the tag SDL2.

Since Google is moving away from Gradle Android Experimental plugin you might consider using Gradle 5 + CMake. The migration is relatively easy and it could save you the trouble of being stuck with old NDK. You need to create CMakeLists.txt instead of build.gradle for C/C++ parts of the code. Example of migration is at GitHub – sdl2-android-example – PR#6.

10. May 2018

SDL2_gfx for Android – Graphic primitives

In previous article, we were talking about drawing pictures in JPEG format. Let’s look how to draw some graphic primitives like a line. SDL2_gfx is small library which has support for graphic primitives and some surface functions.

The initial steps to add SDL2_gfx to Android project is the same like in case of SDL2_jpeg.

Register library in settings.gradle:

include ':SDL2_gfx'

The library should be stored in SDL2_gfx directory with build.gradle.

Now update our C application.

Here is simple example which draws a line:

thickLineColor(renderer, 0, 300, 300, 300, 20, 0xFF00FFFF);

You can find the source code at GitHub in sdl2-android-example repository. Further articles about SDL2 and android are available under the tag SDL2.

Special thanks for this extension of SDL2 Android example goes to jojomickymack who suggested how to add support for SDL2_gfx to Android project. Thank you.

1. October 2017

Missing Run or Debug action in Android Studio with Gradle project

Imagine the simple situation. You want to open an Android project in Android Studio.

Click File, Open. Find the project and click Ok.

Android Studio will open the project. Gradle seems to be working, but there is no Run or Debug action.

Android Studio does not provide any hint where is the issue.

The solution is simple. One important file is missing: settings.gradle. The file defines which modules should be included during the build.

To fix the issue just create settings.gradle in the root of your project:

include ':app'

Click Sync Gradle and within few moments you should be able to run the application.

If your application contains more modules, you can specify them in a similar way like in sdl2-android-example/settings.gradle.