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

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:

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

其他回答

他的问题在这里,移除它

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

why?

注意:不要使用组合play-services目标。它带来了几十个库,使您的应用程序变得臃肿。相反,只指定应用程序使用的特定谷歌Play服务api。

使用你所需要的https://developers.google.com/android/guides/setup

比如位置服务

com.google.android.gms:play-services-location:11.8.0

对于云消息传递

com.google.android.gms:play-services-gcm:11.8.0

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

打开你的\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

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:

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

如果你使用Dagger:

public class App extends DaggerApplication {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this); //ADD MULTIDEX HERE!!!
    }

    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        return DaggerAppComponent.builder().application(this).build();
    }
}

在应用程序构建中。gradle文件:

android {
        defaultConfig {
            ...
            multiDexEnabled true
        }
    }
 ...
 implementation 'com.android.support:multidex:1.0.3'

我知道我的答案晚了,但是,我确信这个问题主要是由一个依赖项引起的,“com.google.android.gms:play-services”。请阅读完整的答案,以便更好地理解问题和解决方案。

在我的情况下,我使用的是'com.google.android.gms:play-services-location:21.0.1',这是它的最新版本,当我写这个答案的时候。它有太多的方法计数。 我使用它的唯一动机是从用户的设备中获取位置。谷歌建议只使用play-services-location来获取位置,所以我不想替换这个依赖来完成我的工作。 经过无数次谷歌搜索,我发现,

您可以降低版本

之前的稳定版本,来完成你的工作。对于我来说,最适合我的版本是17.1.0。所以我建议你只使用这个特定的版本,如果你的用例使用play-services-location完全像我的。

做这些改变

implementation 'com.google.android.gms:play-services-location:21.0.1'

to

implementation 'com.google.android.gms:play-services-location:17.1.0'

尽管如此,在你的代码中总是有启用多索引的选项,但是,在我看来,这是解决这个问题最低效的方法。

如果你能找到导致dex文件大小变大的根本原因 method count,那么我想你应该调整或替换它 依赖。