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.

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.

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.

28. June 2017

How to speed up Gradle build in Android Studio caused by problems with proxy

Update: This article targets one particular scenario when Android builds are too slow. There might be other reasons behind your slow build. Let me know if you need assistance. Now back to original article.

Gradle build should be fast. Even when you build C++ dependencies for Android.

Unfortunately, it often happens that builds take forever and Gradle is not providing any hint how to speed up the build.

One of the most common reasons is that Gradle is trying to download many dependencies. This will take some time even on a fast network. If you’re behind proxy it might take forever.

A quick way to improve performance is to switch Gradle to offline mode.

Go to File, Settings.

Search for “work offline”, check Work offline option and click Ok.

Next Gradle sync or build should be at least 10 times faster.

Once you’ve proved that you have this issue, go to Settings and update Proxy configuration which matches your network.

Enjoy faster builds :-)

If you’re still facing slow builds let me know.