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). […]
This article was posted by Independent Software, a website and database application development company based in Maputo, Mozambique. Our website offers regular write-ups on technical and design issues, ranging from details at code level to 3D Studio Max rendering. Read more about Independent Software's philosophy, or get in touch with Independent Software.
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 consequentely 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
12renderscriptTargetApi 18renderscriptSupportModeEnabled true
- 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: