Gradle和Android Studio升级后的NoClassDefFound

时间:2015-02-09 21:00:08

标签: android android-studio noclassdeffounderror build.gradle android-multidex

自从我升级Android Studio以来,我被迫将Gradle的版本,buildToolsVersion和java的版本转移到了7。

我的问题是我可以在Android Lollipop上完美地运行该项目,但我失去了兼容性,我无法在API 21之前在设备上运行应用程序。

Android Studio版本: 1.0.2 Gradle Wrapper版本: gradle-2.2.1-all

我遵循Configuring Your App for Multidex with Gradle指南的每一步。

TL; DR 代码完美编译,在Android Lollipop上运行,不会在棒棒糖之前运行。

apply plugin: 'com.android.application'

android {

    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId 'com.mundodescuento'
        minSdkVersion 14
        targetSdkVersion 21
        multiDexEnabled true
    }

    buildTypes {

        release {
            proguardFile('proguard-rules.pro')
            signingConfig signingConfigs.MundoDescuento
            debuggable false
            jniDebuggable false
            renderscriptDebuggable false
            minifyEnabled false
            zipAlignEnabled true
        }

        debug {
            proguardFile('proguard-rules.pro')
            debuggable true
            jniDebuggable true
            renderscriptDebuggable true
            minifyEnabled false
            zipAlignEnabled false
        }
    }
    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

因为依赖也宣布

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:multidex:1.0.0'
}

项目的根依赖性。

dependencies {
    classpath 'com.android.tools.build:gradle:1.0.1'
}

我的应用程序类按要求扩展了MultiDexApplication。

public class MundoDescuentoApplication extends MultiDexApplication {
    ...
}

在Android 4.0.3设备上运行项目时我得到的堆栈跟踪:

java.lang.ExceptionInInitializerError
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.Instrumentation.newApplication(Instrumentation.java:957)
            at android.app.Instrumentation.newApplication(Instrumentation.java:942)
            at android.app.LoadedApk.makeApplication(LoadedApk.java:477)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
            at android.app.ActivityThread.access$1300(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            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:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: com/mundodescuento/model/request_params/LoginForm
            at java.lang.Class.getDeclaredMethods(Native Method)
            at java.lang.Class.getDeclaredMethods(Class.java:703)
            at com.google.inject.spi.InjectionPoint.getInjectionPoints(InjectionPoint.java:662)
            at com.google.inject.spi.InjectionPoint.forInstanceMethodsAndFields(InjectionPoint.java:356)
            at com.google.inject.internal.ConstructorBindingImpl.getInternalDependencies(ConstructorBindingImpl.java:151)
            at com.google.inject.internal.InjectorImpl.getInternalDependencies(InjectorImpl.java:585)
            at com.google.inject.internal.InjectorImpl.cleanup(InjectorImpl.java:543)
            at com.google.inject.internal.InjectorImpl.initializeJitBinding(InjectorImpl.java:529)
            at com.google.inject.internal.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:847)
            at com.google.inject.internal.InjectorImpl.createJustInTimeBindingRecursive(InjectorImpl.java:772)
            at com.google.inject.internal.InjectorImpl.getJustInTimeBinding(InjectorImpl.java:256)
            at com.google.inject.internal.InjectorImpl.getBindingOrThrow(InjectorImpl.java:205)
            at com.google.inject.internal.InjectorImpl.getInternalFactory(InjectorImpl.java:853)
            at com.google.inject.internal.FactoryProxy.notify(FactoryProxy.java:46)
            at com.google.inject.internal.ProcessedBindingData.runCreationListeners(ProcessedBindingData.java:50)
            at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:133)
            at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
            at com.google.inject.Guice.createInjector(Guice.java:95)
            at com.google.inject.Guice.createInjector(Guice.java:72)
            at com.google.inject.Guice.createInjector(Guice.java:62)
            at com.mundodescuento.MundoDescuentoApplication.<clinit>(MundoDescuentoApplication.java:22)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.Instrumentation.newApplication(Instrumentation.java:957)
            at android.app.Instrumentation.newApplication(Instrumentation.java:942)
            at android.app.LoadedApk.makeApplication(LoadedApk.java:477)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
            at android.app.ActivityThread.access$1300(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            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:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: com.mundodescuento.model.request_params.LoginForm
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
            at java.lang.Class.getDeclaredMethods(Native Method)
            at java.lang.Class.getDeclaredMethods(Class.java:703)
            at com.google.inject.spi.InjectionPoint.getInjectionPoints(InjectionPoint.java:662)
            at com.google.inject.spi.InjectionPoint.forInstanceMethodsAndFields(InjectionPoint.java:356)
            at com.google.inject.internal.ConstructorBindingImpl.getInternalDependencies(ConstructorBindingImpl.java:151)
            at com.google.inject.internal.InjectorImpl.getInternalDependencies(InjectorImpl.java:585)
            at com.google.inject.internal.InjectorImpl.cleanup(InjectorImpl.java:543)
            at com.google.inject.internal.InjectorImpl.initializeJitBinding(InjectorImpl.java:529)
            at com.google.inject.internal.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:847)
            at com.google.inject.internal.InjectorImpl.createJustInTimeBindingRecursive(InjectorImpl.java:772)
            at com.google.inject.internal.InjectorImpl.getJustInTimeBinding(InjectorImpl.java:256)
            at com.google.inject.internal.InjectorImpl.getBindingOrThrow(InjectorImpl.java:205)
            at com.google.inject.internal.InjectorImpl.getInternalFactory(InjectorImpl.java:853)
            at com.google.inject.internal.FactoryProxy.notify(FactoryProxy.java:46)
            at com.google.inject.internal.ProcessedBindingData.runCreationListeners(ProcessedBindingData.java:50)
            at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:133)
            at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
            at com.google.inject.Guice.createInjector(Guice.java:95)
            at com.google.inject.Guice.createInjector(Guice.java:72)
            at com.google.inject.Guice.createInjector(Guice.java:62)
            at com.mundodescuento.MundoDescuentoApplication.<clinit>(MundoDescuentoApplication.java:22)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.Instrumentation.newApplication(Instrumentation.java:957)
            at android.app.Instrumentation.newApplication(Instrumentation.java:942)
            at android.app.LoadedApk.makeApplication(LoadedApk.java:477)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
            at android.app.ActivityThread.access$1300(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            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:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

编辑02/10/15:添加了MundoDescuentoApplication.java和LoginForm.java

出现在堆栈跟踪中的类的行是private static final Injector injector = Guice.createInjector(new ApplicationGuiceModule(), new GsonModule());

package com.mundodescuento;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.multidex.MultiDexApplication;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.mundodescuento.module.ApplicationGuiceModule;
import com.mundodescuento.module.GsonModule;
import com.parse.Parse;
import com.parse.ParseFacebookUtils;

/**
 * Created by gmuniz on 6/30/14.
 */
public class MundoDescuentoApplication extends MultiDexApplication {

    private static MundoDescuentoApplication mInstance;
    private static final String SHARED_PREFFERENCES = "com.mundodescuento.Prefferences";
    private static final Injector injector = Guice.createInjector(new ApplicationGuiceModule(), new GsonModule());

    private static SharedPreferences sharedPreferences;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        Parse.initialize( ... );
        ParseFacebookUtils.initialize(getString(R.string.facebook_app_id));

        sharedPreferences = getSharedPreferences(SHARED_PREFFERENCES, Context.MODE_PRIVATE);
    }

    /**
     * Retrieves an instance from the {@link SharedPreferences}
     *
     * @return
     */
    public static SharedPreferences getSharedPreferences() {
        return MundoDescuentoApplication.sharedPreferences;
    }

    /**
     * This is a convenience for {@link com.google.inject.Injector#injectMembers(Object) Injector.injectMembers(Object object)}
     */
    public static void injectMembers(final Object object) {
        injector.injectMembers(object);
    }

    /**
     * Retrieve the application in a static way.
     *
     * @return the context.
     */
    public static Context getAppContext() {
        return mInstance;
    }
}

LoginForm.java

package com.mundodescuento.model.request_params;

/**
 * Created by gmuniz on 2/4/15.
 */
public class LoginForm {

    private final String email;
    private final String password;

    private LoginForm(final Builder builder) {
        this.email = builder.email;
        this.password = builder.password;
    }

    public static class Builder {
        private String email;
        private String password;

        public Builder email(final String email) {
            this.email = email;

            return this;
        }

        public Builder password(final String password) {
            this.password = password;

            return this;
        }

        public LoginForm build() {
            return new LoginForm(this);
        }
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

}

我已经失去了关于此的所有线索,任何帮助都将不胜感激!

3 个答案:

答案 0 :(得分:2)

对我来说,问题是通过改变和添加一些东西来解决的。 首先需要通过添加

来启用multidex
defaultConfig {
    multiDexEnabled true
}

然后添加此依赖项

dependencies {
    compile 'com.google.inject:guice:4.0-beta5:no_aop'
    compile 'com.android.support:multidex:1.0.0'  
}

更改清单的应用程序

<application
    ...
    android:name="android.support.multidex.MultiDexApplication">
    ...
</application>

***或者如果您有自己的应用程序类,那么android:name =“。yourApllication”必须扩展MultiDexApplication

了解更多信息 https://developer.android.com/tools/building/multidex.html

答案 1 :(得分:0)

确保具有以下设置:

Setup Multi Dex with resources in Android Studio

更新

看起来真正的问题是LoginForm抛出的初始化 java.lang.ExceptionInInitializerError导致LoginForm上出现空指针,该指针发生在第22行MundoDescuentoApplication.java:22

拜托,您可以为此课程发布此代码吗?

答案 2 :(得分:0)

嗯......最后我提出了解决方案。

我无法详细解释,似乎内部存在处理注释和google-guice依赖的问题。

在将版本移至:

后,我的应用程序在整个Android API级别完全可运行
compile 'com.google.inject:guice:4.0-beta5:no_aop'

它与multidex配置没有任何关系。