启用MultiDex会导致Gradle同步失败

时间:2018-07-29 02:44:29

标签: android android-gradle multidex android-multidex

我关注了Android开发者网站instructions,以启用MultiDex。但是当我添加这些行时,gradle同步失败。这是我的build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.vvaezian.multilingovocabularypractice"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true    // <----- added ------
    }
    buildTypes {
        debug {
            buildConfigField 'String', "ApiKey", MultiLingo_ApiKey
        }
        release {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField 'String', "ApiKey", MultiLingo_ApiKey
        }
    }

    packagingOptions {
        exclude 'project.properties'
        exclude 'META-INF/INDEX.LIST'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2')

    implementation 'com.android.support:appcompat-v7:26.+'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.mcxiaoke.volley:library:1.0.19'
    testImplementation 'junit:junit:4.12'

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    implementation 'com.squareup.okhttp3:okhttp:3.8.0'

    implementation('com.google.cloud:google-cloud-translate:1.3.1') {
        exclude group: 'org.apache.httpcomponents'
        exclude group: 'org.json', module: 'json'
    }
    annotationProcessor 'com.google.cloud:google-cloud-translate:1.3.1'

    implementation 'com.android.support:multidex:1.0.3'  // <----- added ----
}

另外,当我将android:name="android.support.multidex.MultiDexApplication添加到manifest.xml时,它显示unresolved class 'MultiDexApplication',我猜这是因为gradle同步失败。

我在日志中找不到gradle同步失败的原因:

2018-07-28 19:25:09,721 [e-1024-b01]   INFO - e.project.sync.GradleSyncState - Started sync with Gradle for project 'MultiLingoVocabularyPractice'. 
2018-07-28 19:25:09,723 [e-1024-b01]   INFO - idea.project.IndexingSuspender - Consuming IndexingSuspender activation event: SYNC_STARTED 
2018-07-28 19:25:09,723 [e-1024-b01]   INFO - idea.project.IndexingSuspender - Starting batch update for project: Project '/local-scratch/vvaezian/AndroidStudioProjects/MultiLingoVocabularyPractice' MultiLingoVocabularyPractice 
2018-07-28 19:25:09,732 [ thread 26]   INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from /local-scratch/vvaezian/android-studio/jre 
2018-07-28 19:25:09,734 [ thread 26]   INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from /local-scratch/vvaezian/android-studio/jre 
2018-07-28 19:25:09,746 [ thread 26]   INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: -Didea.version=3.1 -Djava.awt.headless=true -Pandroid.injected.build.model.only=true -Pandroid.injected.build.model.only.advanced=true -Pandroid.injected.invoked.from.ide=true -Pandroid.injected.build.model.only.versioned=3 -Pandroid.injected.studio.version=3.1.3.0 -Pandroid.builder.sdkDownload=false --init-script /tmp/ijinit.gradle 
2018-07-28 19:25:12,471 [ thread 26]   INFO - e.project.sync.GradleSyncState - Started setup of project 'MultiLingoVocabularyPractice'. 
2018-07-28 19:25:12,472 [e-1024-b01]   INFO - idea.project.IndexingSuspender - Consuming IndexingSuspender activation event: SETUP_STARTED 
2018-07-28 19:25:12,494 [ thread 36]   INFO - idea.project.IndexingSuspender - Indexing suspended (context: Project Setup) 
2018-07-28 19:25:12,907 [e-1024-b01]   INFO - ule.android.SdkModuleSetupStep - Set Android SDK 'Android API 26 Platform (1)' (/local-scratch/vvaezian/SDK) to module 'app' 
2018-07-28 19:25:12,925 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/build-tools/26.0.1/package.xml 
2018-07-28 19:25:12,933 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/build-tools/27.0.3/package.xml 
2018-07-28 19:25:12,941 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/emulator/package.xml 
2018-07-28 19:25:12,947 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/extras/android/m2repository/package.xml 
2018-07-28 19:25:12,954 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/extras/google/m2repository/package.xml 
2018-07-28 19:25:12,960 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/extras/m2repository/com/android/support/constraint/constraint-layout-solver/1.0.2/package.xml 
2018-07-28 19:25:12,967 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/extras/m2repository/com/android/support/constraint/constraint-layout/1.0.2/package.xml 
2018-07-28 19:25:12,974 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/patcher/v4/package.xml 
2018-07-28 19:25:12,980 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/platform-tools/package.xml 
2018-07-28 19:25:12,989 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/platforms/android-23/package.xml 
2018-07-28 19:25:12,995 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/platforms/android-24/package.xml 
2018-07-28 19:25:13,001 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/platforms/android-25/package.xml 
2018-07-28 19:25:13,007 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/platforms/android-26/package.xml 
2018-07-28 19:25:13,012 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/sources/android-26/package.xml 
2018-07-28 19:25:13,017 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/system-images/android-24/google_apis_playstore/x86/package.xml 
2018-07-28 19:25:13,022 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/system-images/android-26/google_apis/x86/package.xml 
2018-07-28 19:25:13,033 [ thread 37]   INFO - s.RepoProgressIndicatorAdapter - Parsing /local-scratch/vvaezian/SDK/tools/package.xml 
2018-07-28 19:25:13,157 [ thread 35]   INFO - e.project.sync.GradleSyncState - Gradle sync failed (3s 435ms) 
2018-07-28 19:25:13,167 [e-1024-b01]   INFO - idea.project.IndexingSuspender - Consuming IndexingSuspender deactivation event: SYNC_FINISHED 
2018-07-28 19:25:13,167 [e-1024-b01]   INFO - idea.project.IndexingSuspender - Finishing batch update for project: Project '/local-scratch/vvaezian/AndroidStudioProjects/MultiLingoVocabularyPractice' MultiLingoVocabularyPractice 
2018-07-28 19:25:13,173 [e-1024-b01]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
2018-07-28 19:25:13,242 [ thread 36]   INFO - idea.project.IndexingSuspender - Indexing released (context: Project Setup) 
2018-07-28 19:25:13,246 [e-1024-b01]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
2018-07-28 19:25:13,371 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Clearing symbols finished in 0 s. 
2018-07-28 19:25:13,371 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Building symbols in FAST mode, 0 source files from total 0 project files 
2018-07-28 19:25:13,413 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Loading Module Maps finished in 0 s. 
2018-07-28 19:25:13,416 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Saving Module Maps finished in 0 s. 
2018-07-28 19:25:13,416 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Saving Module Maps finished in 0 s. 
2018-07-28 19:25:13,416 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Loaded 0 tables for 0 files (0 project files) 
2018-07-28 19:25:13,418 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Building symbols for 0 source files 
2018-07-28 19:25:13,422 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Building symbols for 0 unused headers 
2018-07-28 19:25:13,434 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Building symbols finished in 0 s. 
2018-07-28 19:25:13,434 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Saving modified symbols for 0 files (0 tables of total 0) 
2018-07-28 19:25:13,435 [ thread 36]   INFO -       #com.jetbrains.cidr.lang - Saving symbols finished in 0 s. 
2018-07-28 19:25:13,505 [ thread 36]   INFO - .diagnostic.PerformanceWatcher - Pushing properties took 6ms; general responsiveness: ok; EDT responsiveness: ok 
2018-07-28 19:25:13,521 [ thread 36]   INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 15ms; general responsiveness: ok; EDT responsiveness: ok 
2018-07-28 19:25:13,521 [ thread 36]   INFO - indexing.UnindexedFilesUpdater - Unindexed files update started: 2 files to update 
2018-07-28 19:25:13,571 [ thread 36]   INFO - .diagnostic.PerformanceWatcher - Unindexed files update took 50ms; general responsiveness: ok; EDT responsiveness: ok 

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

似乎您的gradle文件没有问题。您必须在您的Application类中包括Multidex:

public class YourApplicationClass extends Application {

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

}

AndroidManifest.xml文件中

 <application
    android:name=".YourApplicationClass"
    android:allowBackup="true"
    android:debuggable="true"
    android:icon="@drawable/ic_launcher"
    android:label="xyz"
    android:theme="@style/AppTheme">