How to Use the Renderscript Support Library with Gradle-Based Android Projects

In comparison to the other mobile platforms, Android's Java is slow to run computationally intensive tasks. RenderScript offers a framework to efficiently compute algorithms by utilizing all processors. RenderScript is fairly new to Android and most of the APIs are only available for Android 4.3 (Jellybean) or newer. Fortunately, Google offers a separate support library as a back port for all devices running Android 2.2 or higher (API level 8, Froyo).

Integrating the RenderScript Support Library

Integrating the RenderScript support library was complicated when Android's development environment was still Eclipse and ADT (Instructions). More than a year ago Android moved to Android Studio as the IDE and Gradle as the build automation. According to the official Android documentation, it is not possible to use the RenderScript support library with the Gradle-based projects. However, this is not true.

How to Use It with Gradle

The solution is buried in the comments on a StackOverflow thread. For the integration of the RenderScript support library you just have to add two lines to your build.gradle. You do not need to declare any dependencies. Depending on the gradle version you are running, the commands are slightly different:

Gradle version 0.14+ and newer

android {  
    defaultConfig {
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
    }
}

Older Gradle versions up to 0.13

android {  
    defaultConfig {
        renderscriptTargetApi 19
        renderscriptSupportMode true
    }
}

After the Gradle sync of Android Studio all classes of the package android.support.v8.renderscript.* should be available for further use. The Gradle build includes the necessary resources automatically.

In my next RenderScript post, I will demonstrate how to do easy and fast image blurring.

Let us know if you have any questions at @futurestud_io.


Explore the Library

Find interesting tutorials and solutions for your problems.