How to create Renderscript scripts on Android Studio, and make them run?

倖福魔咒の 提交于 2019-12-12 10:39:37

问题


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:

  • https://developer.android.com/guide/topics/renderscript/compute.html#writing-an-rs-kernel
  • How to use the Renderscript Support Library with Gradle
  • http://developer.android.com/guide/topics/renderscript/compute.html
  • https://futurestud.io/blog/how-to-use-the-renderscript-support-library-with-gradle-based-android-projects/
  • http://developer.android.com/guide/topics/renderscript/advanced.html

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.


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/31872140/how-to-create-renderscript-scripts-on-android-studio-and-make-them-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!