Dagger 2和Multidex应用程序

时间:2017-08-22 14:21:53

标签: android dagger-2 multidex android-multidex dagger

我正在使用支持Android注入的Dagger 2.11。 添加大量依赖项后,我被迫启用了MultiDex。

添加了对Multidex的支持,在Android 4.4按预期工作,在Android> = 6上效果很好。

问题仅出现在Android 5和5.1上,我收到以下错误:

Caused by: java.lang.ClassNotFoundException: Didn't find class "dagger.internal.Preconditions" on path: DexPathList[[zip file...

我尝试将"dagger.internal.Preconditions"添加到multiDexKeepProguard中使用的文件中,但没有运气。

build.gradle文件

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

repositories {
    maven { url 'https://maven.fabric.io/public' }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'com.tmiyamon.config'

apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.tmiyamon.config'
apply plugin: 'realm-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "appId"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true

    }
    dexOptions {
        preDexLibraries false
        javaMaxHeapSize "4g"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        debug {
            storeFile file("path")
            keyAlias "alias"
            storePassword "password"
            keyPassword "password"
        }

        release {
            storeFile file("path")
            keyAlias "alias"
            storePassword "password"
            keyPassword "password"
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            minifyEnabled false
            zipAlignEnabled false
            testCoverageEnabled false
            multiDexKeepProguard file('multidex-config.pro')
            proguardFiles fileTree(dir: 'proguard').asList().toArray()
        }
        release {
            minifyEnabled true
            multiDexKeepProguard file('multidex-config.pro')
            proguardFiles fileTree(dir: 'proguard').asList().toArray()
            signingConfig signingConfigs.release
        }
    }

    productFlavors {
        local {
            applicationIdSuffix ".local"
            versionNameSuffix " Local " + calculateVersionNameSuffix()
        }

        staging {
            applicationIdSuffix ".staging"
            versionNameSuffix " Staging " + calculateVersionNameSuffix()
        }

        production {

        }
    }
}
dependencies {
long list of dependencies
}

和清单文件:

<manifest package="package"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">

<<   permissions >>
<!--${applicationId}-->
<application
    android:name=".application.MyApplication"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="${launcherAppName}"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:label">
    <activity
        android:name=".ui.startup.StartupActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>

rest of the Manifest....

1 个答案:

答案 0 :(得分:1)

嗯,最后,我找到了解决方案。

我还添加了

multiDexKeepFile file('multidex-config.txt')

进入 buiild.gradle 文件的buildTypes部分。

现在看起来如下:

 release {
        minifyEnabled true
        zipAlignEnabled true
        multiDexKeepFile file('multidex-config.txt')
        multiDexKeepProguard file('multidex-config.pro')
        proguardFiles fileTree(dir: 'proguard').asList().toArray()
        signingConfig signingConfigs.release
        debuggable false
    }

并且 multidex-config.txt 具有以下内容:

dagger/internal/Preconditions.class