在尝试模糊背景图像时,找到RenderScript No Class Def

时间:2016-03-03 13:57:52

标签: background-image android-support-library v8 renderscript android-renderscript

尝试模糊我想要使用的图像时

import android.support.v8.renderscript.*

所以我把它放在我的build.gradle文件中:

android {
compileSdkVersion 23
buildToolsVersion '23.0.1'"

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 23

    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true

}}

我用这种方法来模糊图像:

    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;
}

但代码在这一行崩溃了:

RenderScript rs = RenderScript.create(ctx);

给出此错误:

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)

我不知道为什么我从这里跟踪了所有内容:http://developer.android.com/guide/topics/renderscript/compute.html

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

我有一个带有app模块和库模块的项目。

为了能够导入support.v8类,我使用以下方法在库模块build.gradle文件中声明了Renderscript的使用:

    renderscriptTargetApi 23
    renderscriptSupportModeEnabled true

这适用于编译,但仍会导致 NoClassDefFoundError

然后我再次声明在app模块build.gradle文件中使用Renderscript,现在它可以工作了! (使用模拟器和运行API 23的Nexus 5x设备进行测试。)

答案 1 :(得分:0)

这可能是因为Proguard剥离而发生的。有一些指南可以保存所有与RS相关的类/等。包含在你的.apk中。