当前位置:  开发笔记 > 编程语言 > 正文

4.1中的java.lang.NoClassDefFoundError,在android studio中使用5.1.using multidex

如何解决《4.1中的java.lang.NoClassDefFoundError,在androidstudio中使用5.1.usingmultidex》经验,为你挑选了1个好方法。

我从3天开始就遇到了一个非常愚蠢的问题但仍然没有运气.我正在使用Parse.com进行注册并登录.我也使用Twitter和facebook.我的应用程序类oncreate方法: -

  public void onCreate() {
    super.onCreate();
    Parse.initialize(this, AppConstants.PARSE_APP_ID, AppConstants.PARSE_CLIENT_KEY);

    ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();

    defaultACL.setPublicReadAccess(true);

    ParseACL.setDefaultACL(defaultACL, true);
    FacebookSdk.sdkInitialize(getApplicationContext());
}
}

我的gradle构建: -

apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
useLibrary 'org.apache.http.legacy'
defaultConfig {
    applicationId "rsi.com.componentsdemo"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}
dexOptions {
    incremental true
    javaMaxHeapSize "2048M"
    jumboMode = true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
}}dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:8.3.0'
compile 'de.hdodenhof:circleimageview:1.3.0'
compile 'org.apache.httpcomponents:httpmime:4.3'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.facebook.android:facebook-android-sdk:4.2.0'
compile('org.twitter4j:twitter4j-core:4.0.1') {
    exclude group: 'com.google.android', module: 'support-v7'
}
compile project(':lib')
compile files('libs/Parse-1.11.0.jar')

}

我在libs文件夹中使用parse 1.11.0.jar lib.

现在问题是在Android 5.0.1设备中运行相同的代码但在4.1.1版本中崩溃了.

LogCat如下: -

FATAL EXCEPTION: main
                                                                    java.lang.NoClassDefFoundError: com.parse.ParsePlugins$Android
                                                                        at com.parse.Parse.initialize(Parse.java:191)
                                                                        at rsi.com.componentsdemo.ComponentsDemoAppClass.onCreate(ComponentsDemoAppClass.java:20)
                                                                        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
                                                                        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
                                                                        at android.app.ActivityThread.access$1300(ActivityThread.java:130)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                        at android.os.Looper.loop(Looper.java:137)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:511)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                        at dalvik.system.NativeStart.main(Native Method)

请帮我解决这个问题.谢谢!!



1> user1140237..:

如果你正在使用incremental = true意味着多个dex选项是启用的东西不适用于较低的设备,如4.1(AFAIK它不会在4.4以下工作)因为多个dex文件

对于4.4以下的设备,我面临同样的问题.问题不在于ParsePlugin.

解决我在我的问题中给出的问题 build.gradle

dexOptions {
        incremental true
        // here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY

        javaMaxHeapSize "4g"
    }


dependencies {
     compile 'com.android.support:multidex:1.0.1'
    //    your dependencies which you are using.

}

整个 build.gradle

apply plugin: 'com.android.application'
repositories {
    mavenCentral()

}
configurations {
//    all*.exclude group: 'com.android.support', module: 'recyclerview-v7'
}

android {
    signingConfigs {
        /*
        releasebuild {
            keyAlias 'hellotest'
            keyPassword 'hellotest'
            storeFile file('path to keystore')
            storePassword 'hellotest'
        }
        */
    }
    compileSdkVersion 'Google Inc.:Google APIs:22'
    buildToolsVersion '23.0.0'
    /* if you got error regarding duplicate file of  META-INF/LICENSE.txt from jar file
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }
    */
    dexOptions {
        jumboMode = true
        incremental true
        // here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY

        javaMaxHeapSize "4g"
    }
    defaultConfig {
        multiDexEnabled true
        applicationId "com.myapp.packagenme"
        minSdkVersion 17
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.releasebuild
        }
        debug {
            signingConfig signingConfigs.releasebuild
        }
    }
}

dependencies {
     compile 'com.android.support:multidex:1.0.1'
    //    your dependencies which you are using.

}

在Application类中,我扩展了MultiDexApplication

对于MultiDex.install

    public class MyAppClass extends MultiDexApplication{
@Override
    protected void attachBaseContext(Context newBase) {
        MultiDex.install(newBase);
        super.attachBaseContext(newBase);
    }
}

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有