RenderScript No Class Def found, when trying to blur background image

跟風遠走 提交于 2019-12-25 03:13:25

问题


When trying to blur an image I want to use

import android.support.v8.renderscript.*

So I put this in my build.gradle file:

android {
compileSdkVersion 23
buildToolsVersion '23.0.1'"

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 23

    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true

}}

And I use this method to blur the image:

    public static Bitmap blur(Context ctx, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(ctx);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

But the code crashes on this line:

RenderScript rs = RenderScript.create(ctx);

Giving this error:

03-03 14:40:02.965 27865-27865/com.myorder.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                             java.lang.NoClassDefFoundError: android.support.v8.renderscript.RenderScript
                                                                 at com.myorder.profile.BlurBuilder.blur(BlurBuilder.java:27)
                                                                 at com.myorder.ui.SuggestionsHome$1.onResponse(SuggestionsHome.java:70)
                                                                 at com.myorder.ui.SuggestionsHome$1.onResponse(SuggestionsHome.java:59)
                                                                 at com.myorder.core.api.dao.impl.AuthDAOImpl$1.onResponse(AuthDAOImpl.java:75)
                                                                 at com.myorder.core.api.dao.impl.AuthDAOImpl$1.onResponse(AuthDAOImpl.java:62)
                                                                 at mobvolley.JacksonRequest.deliverResponse(JacksonRequest.java:84)
                                                                 at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                 at android.os.Handler.handleCallback(Handler.java:730)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                 at android.os.Looper.loop(Looper.java:176)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:525)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                 at dalvik.system.NativeStart.main(Native Method)

I don;t know why as I have followed everything from here: http://developer.android.com/guide/topics/renderscript/compute.html

Can anyone help?


回答1:


I have a project with an app module and a library module.

In order to be able to import the support.v8 classes, i declared the use of Renderscript in the library module build.gradle file using :

    renderscriptTargetApi 23
    renderscriptSupportModeEnabled true

This is good for compilation, but still causes the NoClassDefFoundError.

I then declared again the use of Renderscript in the app module build.gradle file and now it works ! (tested with the emulator and a Nexus 5x device both running API 23).




回答2:


This may have happened because of Proguard stripping. There are some guides for how to keep all RS-related classes/etc. included in your .apk.



来源:https://stackoverflow.com/questions/35774191/renderscript-no-class-def-found-when-trying-to-blur-background-image

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