Android Renderscript has been around for a few years, but it’s still in need of a lot of documentation. What documentation and tutorials are floating around on the internet focus on getting Renderscript set up in Eclipse, but Eclipse is out – and Android Studio is in (at least as far as Google is concerned).

Moreover, Renderscript’s API changes fast and this affects how you must configure your app when you want to use Renderscript. I had just moved from Eclipse to Android Studio 1.3 and needed to write an app using Renderscripts. Having gone through various tutorials, I thought I understood what Renderscript did and I was ready to start playing with it. Unfortunately, this wasn’t so easy. The tutorials, when implemented in Android Studio, didn’t seem to compile the Renderscript .rs scripts, and the Java code consequently couldn’t find them. Also, I didn’t know what the latest Renderscript libraries were called so I didn’t know how to import them (they do come with the Android SDK, so rest assured that they are there).

Setting up Renderscript

This is how you set up Renderscript in Android Studio:

  • Create a project as normal, with a blank activity in it.
  • Create a folder where your scripts (*.rs) will live. To do this, right-click on app in the project tree on the left, and select New Folder -> Renderscript folder. This will add an rs folder to your project tree.
  • Open the Gradle build file your your app. It’s called build.gradle (Module:app)
  • To this file, add
    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true
  

at the end of the defaultConfig section. Note that there are newer Renderscript target APIs (up to 22) but Googling around I see people are saying that anywhere above 18 is buggy.

  • After you change this file, Android Studio will ask whether it should sync Gradle files for the IDE to work properly. Let Studio do the sync.

You can now add .rs files to the rs directory you just created. When you build your project, they will be compiled for you.

Importing the right Renderscript library

Playing with some Renderscript tutorial source code I couldn’t get it to compile, because there have been changes to Renderscript’s namespace.

It turns out that, were you find:

import android.renderscript.Allocation;
import android.renderscript.RenderScript;

you must now write:

import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.Allocation;

Happy developing!