Android KitKat multidex应用程序崩溃

时间:2017-08-09 08:22:32

标签: android crash android-4.4-kitkat multidex

我仔细阅读:https://developer.android.com/studio/build/multidex.html#mdex-gradle

所以我已经添加到我的build.gradle:

compile 'com.android.support:multidex:1.0.1'

我在Android.manifest中的应用程序定义:

android:name=".App"

最后,App类定义如下:

public class App extends Application
{
    private static Context mContext;

    @Override
    protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
        MultiDex.install(this);

    }

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


        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .defaultDisplayImageOptions(new DisplayImageOptions.Builder()
                        .imageScaleType(ImageScaleType.EXACTLY)
                        .build())
                .build();
        ImageLoader.getInstance().init(config);
    }

    public static Context getContext()
    {
        return mContext;
    }
}

不幸的是,这绝对没有帮助。我的应用程序仍然在KitKat上崩溃,并显示以下消息:

  

java.lang.RuntimeException:无法实例化应用程序   com.example.App:java.lang.ClassNotFoundException:没找到类   “com.example.App”

我做了Google建议的所有事情。还试图扩展MultiDexApplication,仍然是一样的。 你有什么想法我可以试试吗?

[编辑] 我的build.gradle:

apply plugin: 'com.android.application'

android {

    signingConfigs {
        general {
            keyAlias 'key'
            keyPassword '123abc'
            storeFile file('../store.jks')
            storePassword '123abc'
        }
    }

    compileSdkVersion 25
    buildToolsVersion '26.0.0'

    defaultConfig {
        applicationId "(removed)"
        minSdkVersion 19
        targetSdkVersion 25
        dataBinding.enabled = true
        multiDexEnabled = true
        versionCode 21
        versionName '2.2'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        jackOptions {
            enabled true
        }
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        incremental true
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }

    dexOptions {
        javaMaxHeapSize '4096m'
    }

    lintOptions {
        tasks.lint.enabled = false
    }

    productFlavors {
        general {
            applicationId "(removed)"
            signingConfig signingConfigs.general
        }
        lomza {
            applicationId "(removed)"
            signingConfig signingConfigs.general
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.4.0'
    compile 'com.android.support:design:25.4.0'
    compile 'com.android.support:customtabs:25.4.0'
    compile 'com.android.support:cardview-v7:25.4.0'
    compile 'com.jakewharton:butterknife:8.5.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.yarolegovich:lovely-dialog:1.0.5'
    compile 'com.kaopiz:kprogresshud:1.0.5'
    compile 'com.android.support:support-v4:25.4.0'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    compile 'com.google.android.gms:play-services-maps:10.2.1'
    compile 'com.google.android.gms:play-services-places:10.2.1'
    compile 'com.github.jaydeepw:poly-picker:v1.0.22'
    compile 'com.android.support:support-v13:25.4.0'
    compile 'gun0912.ted:tedpermission:1.0.3'
    compile 'io.tus.android.client:tus-android-client:0.1.4'
    compile 'io.tus.java.client:tus-java-client:0.3.1'
    compile 'com.google.firebase:firebase-messaging:10.2.1'
    compile 'com.google.firebase:firebase-auth:10.2.1'
    compile 'com.google.android.gms:play-services-auth:10.2.1'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.android.support:support-vector-drawable:25.4.0'
    compile 'com.google.firebase:firebase-crash:10.2.1'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'org.greenrobot:eventbus:3.0.0'
    testCompile 'junit:junit:4.12'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    compile 'com.google.maps.android:android-maps-utils:0.5'
    compile 'com.google.guava:guava-collections:r03'
    compile 'com.jcodecraeer:xrecyclerview:1.3.2'
    compile 'com.android.support:cardview-v7:25.4.0'
    compile 'com.hendraanggrian:collapsingtoolbarlayout-subtitle:0.7.0'
    compile 'io.reactivex.rxjava2:rxjava:2.0.2'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
    compile 'com.android.support:multidex:1.0.1'

}

apply plugin: 'com.google.gms.google-services'

Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="(removed)">

    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false"/>

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:fullBackupContent="@xml/backup_descriptor">

        <!-- [START fcm_default_icon] -->
        <!--
             Set custom default icon. This is used when no icon is set for incoming notification messages.

        -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/ic_launcher"/>
        <!--
             Set color used with incoming notification messages. This is used when no color is set for the incoming
             notification message.
        -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent"/>
        <!-- [END fcm_default_icon] -->

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name"
            android:noHistory="true"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

            <intent-filter>
                <action android:name="SHOW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

            <meta-data android:name="android.app.shortcuts"
                       android:resource="@xml/shortcuts" />

        </activity>
        <activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
        <activity android:name=".LoginActivity">
            <!-- android:noHistory="true" -->

        </activity>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="(removed)"/>

        <activity
            android:name=".DetailsActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/activity_details"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity">
        </activity>
        <activity
            android:name=".MapActivity"
            android:label="@string/activity_map"
            android:parentActivityName=".DetailsActivity">
        </activity>
        <activity
            android:name=".AddEventActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/activity_add_event"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity">
        </activity>
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"/>
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data android:scheme="@string/fb_login_protocol_scheme"/>
            </intent-filter>
        </activity>

        <service
            android:name=".Tus.TusUploadService"
            android:exported="false"/>

        <!-- [START firebase_service] -->
        <service android:name=".Firebase.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <!-- [END firebase_service] -->
        <!-- [START firebase_iid_service] -->
        <service android:name=".Firebase.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
        <!-- [END firebase_iid_service] -->

        <activity
            android:name=".Preferences.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="(removed).MainActivity"/>
        </activity>
        <activity
            android:name="nl.changer.polypicker.ImagePickerActivity"
            android:label = "@string/activity_img_picker_title"
            />
        <activity
            android:name=".ImagePreviewActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_image_preview"
            android:parentActivityName=".DetailsActivity"
            android:theme="@style/FullscreenTheme">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="(removed).DetailsActivity"/>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

禁用即时运行。

注意: - &gt; 如果您的项目配置为minSdkVersion 20或更低版本的multidex,并且您部署到运行Android 4.4(API级别20)或更低版本的目标设备,则Android Studio会禁用Instant Run

read more from doc

启用multidex,如下所示:

android {    
defaultConfig {
    // Enabling multidex support.
    multiDexEnabled true
}  
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}

像这样创建一个类

public class Multi_Dex extends Application {
@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}
}

现在在你最明显的文件中添加

<application
    android:name=".Multi_Dex"
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

答案 1 :(得分:1)

我遇到了与multidex相同的问题,我发现26.0.0设计支持库不应包含multidex支持(问题enter link description here)。

解决方案是将支持库修订更改为26.0.1。 如果使用26. +版本,则应将其更改为26.0.1,因为26. +等于26.0.0

修订版26.0.1发布中修复的错误

https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-1