8

Background

I want to research about creating Renderscript scripts on Android and Renderscript in general, and over the past year, Android-Studio became the only IDE that Google supports for Android apps development.

The problem

For this, I've found multiple websites, as such:

Thing is, all the tutorials and samples I've seen are for Eclipse, and they say that all I need to do is create an "rs" file inside the "raw" folder (also tried in the "src" folder, in the same folder of the "MainActivity.java" file), and it will auto-generate the needed Java files for me, having a prefix of "ScriptC_".

But it doesn't work for me.

What I've tried

I've created a file from some sample I've found (for Eclipse) called "julia.rs". Here's the code:

#pragma version(1)
#pragma rs java_package_name(lb.com.myapplication)

float cx;
float cy;
float width;
float height;
float zoom;
int precision;

uchar *color;

void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
    float fx = (x + 0.5f) / width * (4.f / zoom) - (2.f / zoom);
    float fy = (y + 0.5f) / height * (4.f / zoom) - (2.f / zoom);

    float t = 0;
    int k = 0;

    while(k < precision - 1) {
        t = fx * fx - fy * fy + cx;
        fy = 2 * fx * fy + cy;
        fx = t;
        if (fx * fx + fy * fy >= 4) {
           break;
        }
        k++;
    }
    out->b = color[k*3+0];
    out->g = color[k*3+1];
    out->r = color[k*3+2];
}

In the java file, I wanted to access the newly created file, so I started to write "ScriptC", and expected it to fill the needed extra characters, but it doesn't.

I can't for example use this piece of code:

mScript=new ScriptC_julia(mRS,getResources(),R.raw.julia);

I've also tried to add Renderscript support for older Android versions, but this of course didn't help:

defaultConfig {
    renderscriptTargetApi 22
    renderscriptSupportModeEnabled true
    ...

Another thing I've tried is to use New->Folder->RenderScript folder via the context menu of the app, but then I got this error:

Error:Execution failed for task ':app:compileDebugRenderscript'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\AppData\Local\Android\Sdk\build-tools\23.0.0-preview\llvm-rs-cc.exe'' finished with non-zero exit value -1073741515

The question

What's the correct way to create and run a Renderscript script on Android-Studio?


EDIT: Sadly I have the exact same issue again, and this time, setting renderscriptTargetApi to 18 doesn't help. I've also tried another projects with Renderscript, here and here, but both have the same issues:

Error:Execution failed for task ':renderscript:compileArmDebugRenderscript'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\android\Sdk\build-tools\23.0.1\llvm-rs-cc.exe'' finished with non-zero exit value -1073741515

I've now added a bounty to solve this issue once and for all.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • http://stackoverflow.com/questions/31843272/set-up-renderscript-in-android-studio answered this yesterday. – Stephen Hines Aug 07 '15 at 08:19
  • @StephenHines I've already tried it in the past. It shows : "Error:Execution failed for task ':app:compileDebugRenderscript'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\AppData\Local\Android\Sdk\build-tools\23.0.0-preview\llvm-rs-cc.exe'' finished with non-zero exit value -1073741515 – android developer Aug 07 '15 at 08:44
  • 1
    You are using API 23 preview. Can you try again with 22? – Miloslaw Smyk Aug 07 '15 at 19:28
  • @MiloslawSmyk Tried it now. Still can't make it work. I've used this project as the source code: https://github.com/carlemil/JuliaLiveWallpaper . Here's my project: https://drive.google.com/file/d/0B-PZZGk2vPohclF3TDYxOVVGYm8/ – android developer Aug 08 '15 at 10:53
  • 1
    This project is hosed. Rather than investigate what exactly is wrong, I just created a new one (using the step-by-step from @StephenHines link above), then copied your java, layout, menu and renderscript files. Then fixed some bugs and added some code in RS and Java, so that it actually lets you see the fractal: https://drive.google.com/file/d/0B4gY85GI2HEzQTFlbnRrYld1Q1k – Miloslaw Smyk Aug 08 '15 at 18:13
  • What's a "hosed" project? I only did what you wrote me to do: create the RS folder and put there the "rs" script file. The rest is Java files trying to use it. What exactly was I missing? Please let me know the steps... – android developer Aug 08 '15 at 19:25
  • Have you started from a clean project and then added the rs folder? If so, maybe there is something wrong with your Android Studio installation. I will try to take a closer look tomorrow. In the meantime, were you able to build the project I've attached above? – Miloslaw Smyk Aug 08 '15 at 21:05
  • I think that's because I tried setting it with the latest version of some stuff, so it got into a state that some files are locked, and so even if I reverted back to older versions, it refused to compile. I should report about this issue. The code I had before should have compiled too, it seems. In any case , what's "hosed" and what did you do exactly? – android developer Aug 08 '15 at 21:55
  • 1
    "hosed" as in "corrupted". Android project file corruption is very common, across multiple generations of development tools. – rwong Aug 08 '15 at 21:55
  • I think it has issues with the versions, if they are too new. I also can't make it work, for example, on API 10 (and I have changed the code to support it). Maybe it's because of the emulator this time? – android developer Aug 08 '15 at 22:34
  • OK, reported here: https://code.google.com/p/android/issues/detail?id=182302 – android developer Aug 08 '15 at 22:53
  • If you are having problems again it means you have changed something. Have you upgraded your build tools? Have you tried reverting to the previous version? – Miloslaw Smyk Sep 29 '15 at 13:28
  • @MiloslawSmyk I've updated everything that's possible, and then returned to try out Renderscript projects, and got this issue again. This time, it didn't help to play with renderscriptTargetApi values... :( – android developer Sep 29 '15 at 14:05
  • Can you go to File/Project Structure and report what: compile SDK, Build Tools Version, Min Sdk Version and Target SDK Version you have there? – Miloslaw Smyk Sep 29 '15 at 14:37
  • @MiloslawSmyk I didn't change it on the project I've imported ( https://github.com/mrmike/RenderScriptPlayground ) : compileSdkVersion 23 buildToolsVersion "23.0.1" minSdkVersion 14 targetSdkVersion 23 – android developer Sep 29 '15 at 14:43
  • 1
    Change buildTooldVersion to "22.0.1" and targetSdkVersion to <21, and try again. – Miloslaw Smyk Sep 29 '15 at 14:52
  • changing to buildToolsVersion "22.0.1" seems to work (no need to change targetSdkVersion ). But why is it this way? And, is there a better workaround? I want it to work on more updated buildToolsVersion versions... – android developer Sep 30 '15 at 05:24
  • There seem to be multiple issues with build tools 23.0.0: https://code.google.com/p/android/issues/detail?id=183478 – Miloslaw Smyk Sep 30 '15 at 10:11
  • @MiloslawSmyk Seems like I forgot I made those posts (that's my post)... So it's not fixed yet? I was sure it got fixed after all those updates... Now I don't know what to do with the bounty, as the issue is the same issue, and the workaround is the same one. – android developer Sep 30 '15 at 11:40
  • 1
    Actually the issue is different - previously build tools 22.0.01 did not work if renderscriptTargetApi was 21+. Now build tools 23.0.0 do not work at all. Also, I don't think you are able to take back the bounty, even if I (or whoever gets it) wanted to forego it. – Miloslaw Smyk Sep 30 '15 at 11:45
  • @MiloslawSmyk I see. How could Google mess up so much ? – android developer Sep 30 '15 at 15:05
  • @MiloslawSmyk For now I will tick you answer. Please do update me about this in case you can. – android developer Sep 30 '15 at 16:51
  • The project is not "hosed" or broken, just old. I used eclipse when i put it together and haven't updated it to work with gradle or studio. If anyone wants to do so and send a pull request i will happily merge it :) – Carl-Emil Kjellstrand Jul 06 '16 at 12:47

3 Answers3

3

The only modification your project needed to build successfully was changing renderscriptTargetApi value from 22 to 18. Otherwise Renderscript script compilation produces intermediate files that use 64-bit ABI, which the build process fails to link with precompiled intermediates in 22.0.01 build tools that use 32-bit ABI.

UPDATE: as of September 2015 the new version of build tools (23.0.0) does not work with Renderscript support library, so either you have to disable it or revert the tools to 22.0.01.

Miloslaw Smyk
  • 1,305
  • 10
  • 15
  • How do you know which version of renderscriptTargetApi to put? And, how do you know about the 64 bit issue? Doesn't it make more sense to support both? – android developer Aug 09 '15 at 05:13
  • This was the value shown here: http://developer.android.com/intl/zh-CN/guide/topics/renderscript/compute.html#ide-setup, so I started with it. 19 and 20 works, too. Details here: https://code.google.com/p/android/issues/detail?id=78443 and here: http://stackoverflow.com/questions/27558999/addition-of-renderscript-in-android-studio-1-0-1-with-api-21-and-sdktools-21 – Miloslaw Smyk Aug 09 '15 at 05:25
  • so they admit newer versions are problematic for now... They should have mentioned it in the IDE itself. – android developer Aug 09 '15 at 09:53
  • I'm having the exact same issue on this project now: https://plus.google.com/+RomainGuy/posts/M3ueUxUpBs1 . May I please share the project so that you could tell me what went wrong? This time, I've tried adding this to the defaultConfig part: renderscriptSupportModeEnabled true renderscriptTargetApi 18 but it doesn't help. – android developer Sep 28 '15 at 22:28
1

Building android apps with renderscript inside is quite different from building a normal android app. Good to see that you got this working in the end :) For anyone else wanting to play around with the source it can be found at github: https://github.com/carlemil/JuliaLiveWallpaper and two other rs project that i worked on: https://github.com/carlemil/alw.plasma and https://github.com/carlemil/alw.eld and if you want to see them "in action" i got them on google play to: https://play.google.com/store/apps/developer?id=Erbsman

Carl-Emil Kjellstrand
  • 1,233
  • 1
  • 10
  • 17
  • Thank you for those cool examples. If you can put other useful links that would be great. I need to do some research about Renderscript, so I want to read all important things about it. – android developer Aug 11 '15 at 09:08
  • 1
    I did write some short post about rs a while ago: http://www.jayway.com/2014/02/11/renderscript-on-android-basics/ and another short note on Intrinsics that can do a lot for you without you having to write any rs yourself: http://www.jayway.com/2014/10/01/android-and-renderscript-intrinsics/ – Carl-Emil Kjellstrand Aug 12 '15 at 09:43
  • Thanks again. I think it'll help me. Here, have another +1 . :) – android developer Aug 12 '15 at 11:25
  • Cool, let me know what you you do with RS, perhaps i will get some inspiration to take it up again :) – Carl-Emil Kjellstrand Aug 13 '15 at 11:57
  • I'm making a presentation about it. Just need to do some research about it. Maybe later will use it for other stuff. Say, can I ask some general questions about it? For example, when you call the allocation, does it allocate more memory, this time on the native memory (real RAM, and not the heap) ? Is it possible to avoid the extra memory allocation? Is it possible, for example, to use only the input bitmap, allocate the output one on the Renderscript script, do the operation, free the input, and return the output? This way, you won't have 2 bitmaps on the heap... – android developer Aug 15 '15 at 19:34
  • Working on only one bitmap could potentially be ok. But you must not use any data from the surrounding pixels since you do no know in what order the pixels will be processed, to avoid errors/artifacts. But you most likely want to have at least 2 buffers, one that is drawn to, and on that is being displayed to the user and simultaneous used for input. Trying to save memory by cutting down to one buffer sounds like a bad idea to me :) – Carl-Emil Kjellstrand Aug 24 '15 at 08:00
  • Well, if the input bitmap becomes the output bitmap, and if the buffer of processing the bitmap could be allocated in Renderscript instead of Java (to save on heap memory), this could save you x2 memory and you could avoid OOM in cases that you are too low on memory. I've even made something similar using JNI: https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations . How do I do this on Renderscript? Is it possible? – android developer Aug 24 '15 at 08:57
  • Well, in and out allocations can be the same, no problem except for the one i mentioned earlier with the unknown order of update to the pixels. As to allocation in rs, not sure how you would want to achieve that since you actually send the allocations in to the script that you run. And you need the allocation available outside of the script to read from it. As long as you are not working on images much larger than the screen, i cant see how this would ever be a issue anyway. – Carl-Emil Kjellstrand Aug 24 '15 at 14:35
  • And if it still is a issue i suggest that you divide your allocation in smaller chunks that you can work on one at a time. Could be that you cut a image in stripes for example, and then invoke the rs for each stripe. With this approach you would never have to risk running out of memory due to the allocations taking up to much space and you could process very large datasets, as big as you could allocate on the android side basicly. – Carl-Emil Kjellstrand Aug 24 '15 at 14:38
  • Can't I allocate variables arrays in RS , and when it's done, copy the result to the bitmap that was the input ? – android developer Aug 24 '15 at 19:51
  • Not sure what variable arrays are, if you are looking for some dynamic in regards of size array, that can be changed (made bigger or smaler) after created I don't think there is such a construct in rs. – Carl-Emil Kjellstrand Aug 26 '15 at 06:13
  • variable arrays : int[] someArray . I could put the input bitmap data into one, process it, and then put the output into the input bitmap which is now the output bitmap. – android developer Aug 26 '15 at 06:36
  • As I've written, this could save heap-memory usage, so that the extra bitmap won't be needed in the java "world", and be used in the RS- "world" instead, where it uses device-RAM instead. This can help with memory management, at the cost of copying data. Of course, this will only work if your input bitmap can be the output bitmap too. – android developer Aug 26 '15 at 09:23
  • renderscript is a nightmare, are there no comapability libraries to revert to a known baseline ? API 16 was nice when I was learning, seems to go obsolete in every release no matter if you use studio or eclipse ! – Jon Goodwin Oct 02 '15 at 03:54
  • You understand that in every API release, the libraries on the target are changing (that you are referencing). If you dont use those libraries, but provide your own, then your code will always work. Doing that is the problem ! – Jon Goodwin Oct 02 '15 at 04:02
1

This has been fixed in gradle-plugin 2.1.0 and Build-Tools 23.0.3. Use the code below:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
    }
}


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    ...
}

UPDATE: If an error "Gradle version 2.10 is required" appears, do NOT change

classpath 'com.android.tools.build:gradle:2.1.0'

Instead, update the distributionUrl field of the Project\gradle\wrapper\gradle-wrapper.properties file to

distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

and change File > Settings > Builds,Execution,Deployment > Build Tools > Gradle >Gradle to Use default gradle wrapper as per "Gradle Version 2.10 is required." Error.

Community
  • 1
  • 1
Miao Wang
  • 1,120
  • 9
  • 12