如果使用播放服务编译 NavigationDrawer 失败

NavigationDrawer fails if compiled with play-services

我能够编译 运行 没有任何问题,一个 NavigationDrawer 示例。

接下来,我包含了一个模块,该模块又包含 google 播放服务库(此模块 'map' 已经过测试并在另一个项目中使用并且运行良好)

但是当我将此模块作为依赖项 build.gradle 添加到 (compile proyect (':map')) 时,构建失败。

消息是:

"Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536"

我理解这个错误是因为方法的数量超过了 64K 的最大值。所以我使用 'multiDexEnabled true' 选项。

现在它编译完美,但是 运行在调试模式下,它抛出一个 运行 时间异常,此时它正在尝试加载 CoordinatorLayout 布局

这是控制台抛出的内容

java.lang.RuntimeException: Unable to start activity ComponentInfo{net.simplifiedcoding.navigationdrawerexample/net.simplifiedcoding.navigationdrawerexample.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at net.simplifiedcoding.navigationdrawerexample.MainActivity.onCreate(MainActivity.java:24)

我也尝试将库的声明限制为仅我打算使用的内容,替换

    compile 'com.google.android.gms:play-services:+'

    compile 'com.google.android.gms:play-services-maps:+'
    compile 'com.google.android.gms:play-services-location:+'
    compile 'com.google.android.gms:play-services-gcm:+'
    compile 'com.google.android.gms:play-services-plus:+'                  

同样的问题。已构建,但在加载协调器布局时失败。

问题是:

是否可以 运行 一个 NavigationDrawer 和一个包含播放服务库的项目,以及 multiDexEnabled 选项集(无论如何我都需要设置这个选项,因为我有很多方法在根项目中)

希望够清楚

这是 NavigationDrawer 的build.grade:

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"

        defaultConfig {
            applicationId "net.simplifiedcoding.navigationdrawerexample"
            minSdkVersion 16
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:24.2.0'
        compile 'com.android.support:design:24.2.0'
        compile project (':map')
    }

这是地图项目的 build.gradle 作为 NavigationDrawer

的依赖项
    apply plugin: 'com.android.library'

    android {
        compileSdkVersion 'Google Inc.:Google APIs:24'
        buildToolsVersion "24.0.2"

        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 25

        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt')
            }
        }
    }

    dependencies {
        //compile 'com.google.android.gms:play-services:+'
        compile 'com.google.android.gms:play-services-maps:+'
        compile 'com.google.android.gms:play-services-location:+'
        compile 'com.google.android.gms:play-services-gcm:+'
        compile 'com.google.android.gms:play-services-plus:+'
    }

除了 cricket_007 的评论外,您可能还需要查看 Adding Support Libraries 中给出的警告:

Using dynamic dependencies (for example, palette-v7:23.0.+) can cause unexpected version updates and regression incompatibilities. We recommend that you explicitly specify a library version (for example, palette-v7:25.2.0).

此外,检查使用的版本。正如 Manifest Declaration Changes

中所述

If you are including several support libraries, the minimum SDK version must be the highest version required by any of the specified libraries. For example, if your app includes both the v14 Preference Support library and the v17 Leanback library, your minimum SDK version must be 17 or higher.

最后,您可能还想查看此 article 以获得有关设计支持库的更多见解。