我想添加融合的位置服务,但它告诉我一些错误。 帮助我。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "27.0.1"
    defaultConfig {
        applicationId "com.example.adil.bloodbankapplication"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-auth:11.8.0'
    compile 'com.google.firebase:firebase-database:11.8.0'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'junit:junit:4.12'
    compile 'com.android.support:design:26.1.0'
    compile 'com.github.joielechong:countrycodepicker:2.1.5'
    compile 'com.jaredrummler:material-spinner:1.2.4'
    compile 'hanks.xyz:htextview-library:0.1.5'
    compile 'com.firebaseui:firebase-ui-database:1.2.0'
    compile 'com.google.android.gms:play-services:11.8.0'
}


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

当前回答

我不知道为什么,也许是因为我在Kotlin开发,但要修复这个错误 我最后必须创建一个扩展MultiDexApplication的类,就像这样:

class MyApplication : MultiDexApplication() {
}

然后在manifest。xml中进行设置

<application
        ...
        android:name=".MyApplication">

为了不让任何人感到困惑,我也这样做:

multiDexEnabled true
implementation 'com.android.support:multidex:1.0.3'

对于androidx,这也适用于我:

implementation 'androidx.multidex:multidex:2.0.0'

...

<应用android: name = " android.support.multidex.MultiDexApplication " >

对我没用吗

其他回答

如果你在Proguard中使用minifyEnabled,你可能不需要启用multi-dex。我同意MG Developer的观点,如果可能的话,你应该尽量避免使用多重索引。我的解决方案是只对调试版本启用multi-dex。minifyEnabled解决了发布版本的问题

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        // getting error: Cannot fit requested classes in a single dex file.  # methods: 65537 > 65536
        // https://developer.android.com/studio/build/multidex
        // minifyEnabled true (used with release) will fix this by getting rid of unused method calls, but this will hide debugging info on crash
        multiDexEnabled true
    }
}

Goto构建。Gradle的应用程序文件夹, 设置minSdkVersion为21

保存并运行您的应用程序,它应该没有dex错误现在运行

修改应用程序或模块的build.gradle

android {
    defaultConfig {
        ...
        minSdkVersion 21 <----- *here
        targetSdkVersion 26
        multiDexEnabled true <------ *here
    }
    ...
}

根据官方文件

Multidex support for Android 5.0 and higher Android 5.0 (API level 21) and higher uses a runtime called ART which natively supports loading multiple DEX files from APK files. ART performs pre-compilation at app install time which scans for classesN.dex files and compiles them into a single .oat file for execution by the Android device. Therefore, if your minSdkVersion is 21 or higher, you do not need the multidex support library. For more information on the Android 5.0 runtime, read ART and Dalvik.

https://developer.android.com/studio/build/multidex

对于颤振项目,您也可以解决此问题

打开你的\android\app\build.gradle

你应该有以下内容:

android {
    defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 28

    }

}

如果你的minSdkVersion设置为21或更高,你所需要做的就是在defaultConfig中将multiDexEnabled设置为true。像这样

android {
    defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 28
        multiDexEnabled true 

    }

}

如果你的minSdkVersion设置为20或更低,在defaultConfig中添加multiDexEnabled true

并定义实现

dependencies {
  implementation 'com.android.support:multidex:1.0.3'// or 2.0.1
}

最后,你应该:

android {
    defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 28
        multiDexEnabled true  // added this
    }

}

dependencies {
  implementation 'com.android.support:multidex:1.0.3'
}

欲了解更多信息,请阅读以下内容: https://developer.android.com/studio/build/multidex

他们给你的答案都不详尽。 问题出在Multidex上。 你必须在应用程序gradle中添加这个库:

implementation 'com.android.support:multidex:1.0.3'

之后,在应用gradle的defaultConfig中添加:

multiDexEnabled true

您的应用程序必须是Multidex类型.. 你必须把它写在清单上:

android:name=".MyApplication"

“MyApplication”必须是Multidex类,或者必须扩展它。