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

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'

当前回答

dex文件是什么:Android应用程序(APK)文件包含Dalvik可执行(dex)文件形式的可执行字节码文件,其中包含用于运行应用程序的编译代码。

出现这种异常的原因:DEX规范限制了在单个DEX文件中可以引用的方法总数为65,536 (64K引用限制)——包括Android框架方法、库方法和你自己代码中的方法。

步骤1。添加如下依赖项

对于非androidx用户,

dependencies {
  implementation 'com.android.support:multidex:1.0.3'
}
defaultConfig {
    minSdkVersion 16
    targetSdkVersion 28
    multiDexEnabled true  //ADD THIS LINE
}

对于android用户,

dependencies {
  implementation 'androidx.multidex:multidex:2.0.1'
}
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        multiDexEnabled true  //ADD THIS LINE
    }

步骤2:

在添加这些同步项目之后,在运行项目之前,请确保在运行之前构建一个项目。否则,您将得到一个异常。

其他回答

如果你在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
    }
}

我所要做的就是在文件>项目结构>应用>口味中将最低SDK版本设置为21

你可以跟着这个走。

Android 5.0 (API级别21)之前的平台版本使用Dalvik运行时执行应用程序代码。默认情况下,Dalvik限制每个APK只能使用一个classes.dex字节码文件。为了绕过这个限制,你可以在你的项目中添加multidex支持库:

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

如果你的minSdkVersion设置为21或更高,你所需要做的就是在你的模块级构建中将multiDexEnabled设置为true。Gradle文件,如下所示:

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

在痛苦了两个小时后找到了解决办法。

文件路径:android/build.gradle

为我工作

defaultConfig {
    applicationId "com.something"
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    versionCode 1
    versionName "1.0"
    multiDexEnabled true --> This works for me
}

如果您的minSdkVersion设置为21或更高,则multidex默认启用,您不需要multidex支持库。-参考:https://developer.android.com/studio/build/multidex#mdex-gradle

我用下面的解决方案解决了我的问题:

在Gradle构建文件中添加依赖项:

实现“com.android.support: multidex: 1.0.3”

然后在“defaultConfig”部分,添加:

multiDexEnabled真实