异常:发布运行应用程序时出现ClassNotFoundException和NoClassDefFoundError

时间:2019-07-06 11:11:47

标签: java android exception

在“调试”模式下运行应用程序时,工作正常,但是在使用“发行版本”运行时,出现以下错误:

Fatal Exception: java.lang.NoClassDefFoundError: Failed resolution of: Laa$c;
       at com.marvhong.videoeffect.helper.MagicFilterFactory.filterType2Name + 105(MagicFilterFactory.java:105)
       at com.ui.audiovideoeditor.activity.TrimVideoActivity.initView + 265(TrimVideoActivity.java:265)
       at com.ui.audiovideoeditor.base.TrimmerBaseActivity.onCreate + 78(TrimmerBaseActivity.java:78)
       at android.app.Activity.performCreate + 7032(Activity.java:7032)
       at android.app.Activity.performCreate + 7023(Activity.java:7023)
       at android.app.Instrumentation.callActivityOnCreate + 1236(Instrumentation.java:1236)
       at android.app.ActivityThread.performLaunchActivity + 2814(ActivityThread.java:2814)
       at android.app.ActivityThread.handleLaunchActivity + 2943(ActivityThread.java:2943)
       at android.app.ActivityThread.-wrap11(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage + 1630(ActivityThread.java:1630)
       at android.os.Handler.dispatchMessage + 106(Handler.java:106)
       at android.os.Looper.loop + 164(Looper.java:164)
       at android.app.ActivityThread.main + 6626(ActivityThread.java:6626)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 438(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main + 811(ZygoteInit.java:811)


Caused by java.lang.ClassNotFoundException: Didn't find class "aa$c" on path: DexPathList[[zip file "/data/app/com.socialmediavideoadsmaker-TZFvA-VXiNm9BBp_hNHgGw==/base.apk"],nativeLibraryDirectories=[/data/app/com.socialmediavideoadsmaker-TZFvA-VXiNm9BBp_hNHgGw==/lib/arm, /data/app/com.socialmediavideoadsmaker-TZFvA-VXiNm9BBp_hNHgGw==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib]]
       at dalvik.system.BaseDexClassLoader.findClass + 125(BaseDexClassLoader.java:125)
       at java.lang.ClassLoader.loadClass + 379(ClassLoader.java:379)
       at java.lang.ClassLoader.loadClass + 312(ClassLoader.java:312)
       at com.marvhong.videoeffect.helper.MagicFilterFactory.filterType2Name + 105(MagicFilterFactory.java:105)
       at com.ui.audiovideoeditor.activity.TrimVideoActivity.initView + 265(TrimVideoActivity.java:265)
       at com.ui.audiovideoeditor.base.TrimmerBaseActivity.onCreate + 78(TrimmerBaseActivity.java:78)
       at android.app.Activity.performCreate + 7032(Activity.java:7032)
       at android.app.Activity.performCreate + 7023(Activity.java:7023)
       at android.app.Instrumentation.callActivityOnCreate + 1236(Instrumentation.java:1236)
       at android.app.ActivityThread.performLaunchActivity + 2814(ActivityThread.java:2814)
       at android.app.ActivityThread.handleLaunchActivity + 2943(ActivityThread.java:2943)
       at android.app.ActivityThread.-wrap11(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage + 1630(ActivityThread.java:1630)
       at android.os.Handler.dispatchMessage + 106(Handler.java:106)
       at android.os.Looper.loop + 164(Looper.java:164)
       at android.app.ActivityThread.main + 6626(ActivityThread.java:6626)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 438(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main + 811(ZygoteInit.java:811)

此异常来自Crshlytics。 我使用过的视频效果库,在此库中,以下是Release App中找不到的类:

public class MagicFilterFactory {

    /**
     * 视频滤镜效果
     * @return
     */
    public static GlFilter getFilter() {
        MagicFilterType filterType = ConfigUtils.getInstance().getMagicFilterType();
        return getFilter(filterType);
    }

    private static GlFilter getFilter(MagicFilterType filterType) {
        switch (filterType) {
            case NONE:
                return new GlFilter();
            case BILATERAL:
                return new GlBilateralFilter();
            case BOXBLUR:
                return new GlBoxBlurFilter();
            case BULGEDISTORTION:
                return new GlBulgeDistortionFilter();
            case CGACOLORSPACE:
                return new GlCGAColorspaceFilter();
            case GAUSSIANBLUR:
                return new GlGaussianBlurFilter();
            case GRAYSCALE:
                return new GlGrayScaleFilter();
            case HAZE:
                return new GlHazeFilter(0.5f, 0.3f);
            case INVERT:
                return new GlInvertFilter();
            case LUT:
                return new GlVignetteFilter();
            case MONOCHROME:
                return new GlMonochromeFilter();
            case SEPIA:
                return new SepiaEffect();
            case SHARPEN:
                return new GlSharpenFilter(3.f);
            case SPHEREREFRACTION:
                return new GlSphereRefractionFilter();
            case TONECURVE:
                return new GlToneCurveFilter();
            case VIGNETTE:
                return new GlVignetteFilter(0.5f, 0.5f, 0.2f, 0.85f);
            case BLACKANDWHITE:
                return new BlackAndWhiteEffect();
            case OVERLAY:
                return new OverlayEffect();
            case BARRELBLUR:
                return new BarrelBlurEffect();
            case POSTERIZE:
                return new PosterizeEffect();
            case CONTRAST:
                return new ContrastEffect();
            case GAMMA:
                return new GammaEffect();
            case CROSSPROCESS:
                return new CrossProcessEffect();
            case HUE:
                return new HueEffect();
            case TEMPERATURE:
                return new TemperatureEffect();
            case SKETCH:
                return new SketchEffect();
            default:
                return new GlFilter();
        }
    }

    public static int filterType2Name(MagicFilterType filterType) {
        switch (filterType) {
            case NONE:
                return R.string.filter_none;
            case INVERT:
                return R.string.filter_invert;
            case MONOCHROME:
                return R.string.filter_monochrome;
            case SEPIA:
                return R.string.filter_sepia;
            case GRAYSCALE:
                return R.string.filter_grayscale;
            case SHARPEN:
                return R.string.filter_sharpen;
            case SPHEREREFRACTION:
                return R.string.filter_sphererefraction;
            case BULGEDISTORTION:
                return R.string.filter_bulgedistortion;
            case CGACOLORSPACE:
                return R.string.filter_cgacolorspace;
            case HAZE:
                return R.string.filter_haze;
            case BILATERAL:
                return R.string.filter_bilateral;
            case TONECURVE:
                return R.string.filter_tonecurve;
            case VIGNETTE:
                return R.string.filter_vignette;
            case BLACKANDWHITE:
                return R.string.filter_blackandwhite;
            case OVERLAY:
                return R.string.filter_overlay;
            case BARRELBLUR:
                return R.string.filter_barrelblur;
            case POSTERIZE:
                return R.string.filter_posterize;
            case CONTRAST:
                return R.string.filter_contrast;
            case GAMMA:
                return R.string.filter_gamma;
            case CROSSPROCESS:
                return R.string.filter_crossprocess;
            case HUE:
                return R.string.filter_hue;
            case TEMPERATURE:
                return R.string.filter_temperature;
            case SKETCH:
                return R.string.filter_sketch;
            default:
                return R.string.filter_none;
        }
    }

    public static int filterType2Color(MagicFilterType filterType) {
        switch (filterType) {
            case NONE:
                return R.color.filter_category_greenish_dummy;

            default:
                return R.color.filter_category_greenish_normal;
        }
    }

    public static int filterType2Thumb(MagicFilterType filterType) {
        switch (filterType) {
            case NONE:
                return R.drawable.filter;
            case INVERT:
                return R.drawable.filter_invert;
            case SEPIA:
                return R.drawable.filter_sepia;
            case GRAYSCALE:
                return R.drawable.filter_grayscale;
            case CGACOLORSPACE:
                return R.drawable.filter_cgacolorspace;
            case BARRELBLUR:
                return R.drawable.filter_barrelblur;
            case BLACKANDWHITE:
                return R.drawable.filter_blackandwhite;
            case CONTRAST:
                return R.drawable.filter_contrast;
            case CROSSPROCESS:
                return R.drawable.filter_crossprocess;
            case GAMMA:
                return R.drawable.filter_gamma;
            case HUE:
                return R.drawable.filter_hue;
            case OVERLAY:
                return R.drawable.filter_overlay;
            case TEMPERATURE:
                return R.drawable.filter_temperature;
            default:
                return R.drawable.filter;
        }
    }
}

有一个在发布时找不到的类。

此外,我已应用以下解决方案: 1)Android: java.lang.NoClassDefFoundError on the release APK 2)Android: NoClassDefFoundError only on release build

1 个答案:

答案 0 :(得分:2)

minifyEnabledshrinkResources在build.gradle文件中非常重要,如果您想减小特定构建类型(最好是“发布”类型)的生成apk的大小。

minifyEnabled通过删除未使用的代码和引用来收缩代码。

另一方面,

shrinkResources从/ res文件夹中删除代码中未引用的未使用资源文件。

您必须使用此代码

res = getResources().getIdentifier(filter_none, "string", getPackageName());

代替

R.string.filter_none

现在,如果您使用Resources.getIdentifier()方法动态引用资源,则由于未引用的资源已被删除,因此会发生意外行为。

为避免这种情况,在动态访问资源时,请按照本示例在Android User Guide中说明的那样使用此技巧

String name = String.format("img_%1d", angle + 1);
res = getResources().getIdentifier(name, "drawable", getPackageName());