33

Is it possible to use the Support Renderscript Library with Gradle? If so, how do you include it in your project?

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
Janusz
  • 187,060
  • 113
  • 301
  • 369

6 Answers6

127

Using Android Studio:

Add the following values to build.gradle for android gradle plugin v0.14+

android {
    ...
    defaultConfig {
        ...
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
    }
    ...
}

For older versions of the android gradle plugin v0.13.3 and below

android {
        ...
        defaultConfig {
            ...
            renderscriptTargetApi 19
            renderscriptSupportMode true
        }
        ...
    }

Once that is done, use android.support.v8.renderscript. anywhere in your app. The library jar and binaries are included automatically.

Patrick
  • 33,984
  • 10
  • 106
  • 126
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
  • 2
    This is the way to go. No complicated setup needed anymore. – Patrick Apr 19 '14 at 16:55
  • @for3st Your edit was incorrect. AS includes the binaries **automatically** in 0.5.x+, no need for product flavors. – Austyn Mahoney Apr 22 '14 at 18:32
  • Ah ok sry, I thought since they were in the gradle 0.9 examples this was necessary – Patrick Apr 22 '14 at 18:47
  • But if AS includes the binaries, does this work with gradle command line (thinking about CI servers)? – Patrick Apr 22 '14 at 18:54
  • Sorry, I meant the gradle build tools not AS. – Austyn Mahoney Apr 22 '14 at 18:56
  • Just checked from command line, adding the above options to your build.gradle asks the build tools to include the binaries in your apk. I unzipped it after running `gradlew assembleDebug` and see the binaries in their respective architecture folders. Xavier also posted on your answer, his team built this support. – Austyn Mahoney Apr 22 '14 at 19:00
  • Alright, thx for looking into it; this wasn't my answer I just made the edit because it confused me the first time ;) – Patrick Apr 22 '14 at 20:58
  • 1
    Also worked for me with AS 0.8.9, but I had to place .rs file(s) inside an "rs" folder (which is located at the same level of java, res and the manifest) in order to have the java glue generated. – Aladin Q Oct 18 '14 at 15:02
  • 10
    With newer gradle versions, like `0.14.0`, I think you need to change `renderscriptSupportMode` to instead be `renderscriptSupportModeEnabled`. – Charles Madere Nov 05 '14 at 17:54
  • @ScootrNova bless your heart! `renderscriptSupportModeEnabled` was the fix. Maybe you should post that as an answer. – Tonithy Nov 07 '14 at 16:09
  • Thanks to @for3st, we have the correct field name for the newer versions in the answer too. – Austyn Mahoney Nov 18 '14 at 01:27
  • @AladinQ File/folder manipulation is not necessary. All binaries are placed in the correct folder by the build tool. – Austyn Mahoney Nov 18 '14 at 01:30
  • Doing this increases release apk size by minimum 2mb :( – Harry Sharma Jul 07 '16 at 10:40
16

Gradle for Android has now Renderscript v8 support with only 2 lines in your build script. See answer by Austyn Mahoney. Keeping the original answer for historical purpose.


Old Answer:

Gradle supports native RS compilation, but not the RS support library. The reason is that the RS support library is hugely complicated compared to any other support library. The RS support library is actually a Java support library, a set of native libraries backing that Java lib, additional native libraries for every script you compile, and an alternate toolchain for generating both the standard LLVM bitcode and the native libraries for your scripts. Right now, only ADT and Ant support that; Gradle isn't there yet.

However, it's possible to use some parts of the RS support library from Gradle already. If you want to use the RS intrinsics, you can link the Java part of the support library (sdk/build-tools/android-4.3/lib/renderscript/renderscript-v8.jar) and the native components (sdk/build-tools/android-4.3/lib/renderscript/packaged/< arch >/*.so), and then you'll be set.

I know Gradle support for the support library is coming at some point in the not too distant future, but I don't have a firm ETA that I can share.

Community
  • 1
  • 1
Tim Murray
  • 2,205
  • 13
  • 13
  • 1
    Thanks for the explanation. I will go with a Java blur function for now. Easier on our setup and radius can be any size. I'm looking forward for the gradle support. – Janusz Nov 04 '13 at 11:49
  • 4
    Note that this is now supported. See the other answer below for the syntax in build.gradle to enable this. – Xavier Ducrohet Apr 10 '14 at 00:56
  • Just a side note, that for any project with flavors, etc, be sure to include code along the lines of: ```sourceSets { main { renderscript.srcDirs = ['src'] } }``` – AllDayAmazing May 01 '15 at 02:50
6

Following Tim's hint, I was able to get v8 support working with Gradle, here is my project layout:

enter image description here

I created libs folder, and copied files Tim mentioned under the SDK folder. And here is my build.gradle changes:

dependencies {
    compile files('libs/renderscript-v8.jar')
}

android {

    tasks.withType(com.android.build.gradle.tasks.PackageApplication) {
        pkgTask -> pkgTask.jniFolders = new HashSet<File>();
            pkgTask.jniFolders.add(new File(projectDir, 'libs'));
    }
}

After that, I can import android.support.v8.renderscript.* and use the intrinsics.

X.Y.
  • 13,726
  • 10
  • 50
  • 63
  • This won't be needed in the next release of the Gradle plugin, according to Xavier Ducrohet on the adt-dev mailing list. – tad Jan 18 '14 at 19:13
  • @tad Great, will it happen very soon? – X.Y. Jan 18 '14 at 23:39
  • Not sure what their timeline is. Xav said: "This is fixed already and will be released in the next plugin (which should be shortly)" – tad Jan 19 '14 at 07:37
  • 1
    I think this is solved in Android Studio 0.4.3. Release notes: "Fixed issue 64302: Add renderscript support mode jar to the dependencies in the IDE model." – X.Y. Feb 07 '14 at 23:12
  • 1
    Yep, using renderscript right now with just `renderscriptTargetApi` and `renderscriptSupportMode` set in `android.defaultConfig`. – tad Feb 07 '14 at 23:34
2

I know this has been already answered, but I thought I'd share my experience with Android-Studio and Renderscript support with Build-tools 21.1.0. This is what I found in build-system changelog lines 26-32:

  • Renamed a few properties to make things more consistent.
    • BuildType.runProguard -> minifyEnabled
    • BuildType.zipAlign -> zipAlignEnabled
    • BuildType.jniDebugBuild -> jniDebuggable
    • BuildType.renderscriptDebug -> renderscriptDebuggable
    • ProductFlavor.renderscriptSupportMode -> renderscriptSupportModeEnabled
    • ProductFlavor.renderscriptNdkMode -> renderscriptNdkModeEnabled

So you see, they have changed the properties name. I just updated build.gradle to use:

renderscriptSupportModeEnabled true

Now the libraries are added to the project and you don't need to manually add them to your lib folder.

Joe3112
  • 968
  • 8
  • 10
1

There is experimental support in the Gradle Android plugin at the time of this writing. See this test project for more info.

tad
  • 1,383
  • 11
  • 16
  • I got this error: Failed to refresh Gradle project 'RenderScript Test' Could not find com.android.tools.build:gradle:0.6.2-SNAPSHOT. Required by: :RenderScriptTest:unspecified – X.Y. Jan 18 '14 at 04:13
1

If anyone is interested in how to package this as a distributable binary .jar (e.g. for deploying to a maven repo) you can use this file structure for your .jar.

Overall file structure Java classes

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>android</groupId>
  <artifactId>renderscript</artifactId>
  <version>19.0.3</version>
  <description>Artifactory auto generated POM</description>
</project>

For build.gradle, add: compile 'android:renderscript:19.0.3' to your dependencies clojure.

PS: The renderscript library won't run on armv6 (armeabi), so make sure that Build.CPU_ABI is either armeabi-v7a, mips or x86.

Sveinung Kval Bakken
  • 3,715
  • 1
  • 24
  • 30