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

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'

当前回答

我遇到过这个错误两次,解决方案是;检查你的应用程序gradle文件来查看你的目标SDk,如果它是20或更高,只需添加一行到你的defaultconfig {multiDexEnabled true}

否则,如果targetSDK小于20,则将该行添加到defaultConfig中,并添加一个依赖项

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

点击这个链接了解更多。

https://developer.android.com/studio/build/multidex#mdex-gradle

其他回答

修改应用程序或模块的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 dex文件有65536个方法的限制。

对于初学者来说,如果你不需要谷歌播放服务API,只需要位置,请替换

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

with

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

你可以参考本页的方法来避免它,或者如果需要允许更多: https://developer.android.com/studio/build/multidex.html

只需要做以下几点:

build.gradle

(module:app)


android {
            ....
      defaultConfig {
              multiDexEnabled true // enable mun
      }
}

并在构建中添加以下依赖项。Gradle应用程序级别文件

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

我为我的项目找到了这个解决方案 我只是将最低SDK版本设置为21,这就解决了我的问题

android {
    defaultConfig {
        ...
        minSdkVersion 21 //set the minimum sdk version 21
        targetSdkVersion 29
    }
    ...
}

如果您的minSdkVersion为21或更高,则默认启用multidex,并且您不需要multidex支持库。 阅读更多关于multidex https://developer.android.com/studio/build/multidex.html

如果你使用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'