简体   繁体   English

Android Studio:为什么我在全新的 Google Maps API 项目上出现多 dex 错误?

[英]Android Studio: Why am i getting multi dex error on brand new Google Maps API project?

I'm relatively new to Android programming and I am trying to create a Google Maps project.我对 Android 编程比较陌生,我正在尝试创建一个 Google 地图项目。 I used the template option in Android Studio, and I added the key for the API.我在 Android Studio 中使用了模板选项,并添加了 API 的密钥。

I haven't added any of my own code and left the template code as is because I just wanted to run the code and see what it looks like, however, I keep getting a multi dex error when I try to run this on the emulator causing the build to fail.我没有添加任何我自己的代码并保留模板代码,因为我只是想运行代码并查看它的外观,但是,当我尝试在模拟器上运行它时,我不断收到多 dex 错误导致构建失败。 It's weird to me that I am getting this error because I haven't added ANY code at all and am using what the Google Maps template has from Android Studio.我收到此错误很奇怪,因为我根本没有添加任何代码,并且使用的是 Android Studio 中的 Google Maps 模板。

Anyone know why this error shows up on a brand new project?任何人都知道为什么这个错误会出现在一个全新的项目中? The error I see is pasted below.我看到的错误粘贴在下面。

Error:The number of method references in a .dex file cannot exceed 64K.错误:.dex 文件中的方法引用数不能超过 64K。 Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.htmlhttps://developer.android.com/tools/building/multidex.html了解如何解决此问题

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.错误:任务“:app:transformClassesWithDexForDebug”的执行失败。

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\\Program Files\\Java\\jdk1.8.0_91\\bin\\java.exe'' finished with non-zero exit value 2 com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:com.android.ide.common.process.ProcessException:org.gradle.process。 internal.ExecException: Process 'command 'C:\\Program Files\\Java\\jdk1.8.0_91\\bin\\java.exe'' 以非零退出值 2 结束

as quoted below:引用如下:

Your probably compiling all of the play-services API's using compile 'com.google.android.gms:play-services:9.2.0'... Now we can selectively compile API's to avoid dex limit of 64K.您可能使用 compile 'com.google.android.gms:play-services:9.2.0' 编译所有播放服务 API ......现在我们可以有选择地编译 API 以避免 64K 的 dex 限制。 For Google Maps use com.google.android.gms:play-services-maps:9.2.0... – Loki Jul 1 at 19:01对于 Google 地图,请使用 com.google.android.gms:play-services-maps:9.2.0... – Loki 7 月 1 日 19:01

Answer from Loki worked and was very simple to do. Loki 的回答很有效,而且操作起来非常简单。

setting up google play services设置谷歌播放服务

Go through this link just add the dependencies which you want in your application.通过此链接,只需在应用程序中添加所需的依赖项即可。 This will prevent 64k exceeding error.这将防止 64k 超出错误。

Happy coding.快乐编码。

For those who tried all the above methods and failed.对于那些尝试了上述所有方法但都失败的人。 Try changing the mini sdk version to 21 in the build.gradile file.尝试在 build.gradile 文件中将 mini sdk 版本更改为 21。 For me it was on 16 and when I changed it to 21 the multidex error was gone for good.对我来说,它是在 16 上,当我将其更改为 21 时,multidex 错误就消失了。

minSdkVersion 21

The issue is you currently have a high number of methods.问题是您目前有很多方法。 There can only be 65536 methods for dex. dex 只能有 65536 个方法。 You can try enabling multiDex by editing your build.gradle file.您可以尝试通过编辑 build.gradle 文件来启用 multiDex。

android {
          //stuff

defaultConfig {
    ...

       // Enable multiDex support. 
       multiDexEnabled true
    }

}

dependencies {
   compile 'com.android.support:multidex:1.0.0'
}

Also, oops as the poster above mentioned in your manifest you also need to add that you are using the multidex class from the multidex support library.此外,就像清单中上面提到的海报一样,您还需要添加您正在使用 multidex 支持库中的 multidex 类。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.dexapp">
<application
    ...
    android:name="android.support.multidex.MultiDexApplication">
    ...
</application>

Change your Gradle build configuration to enable multidex更改您的 Gradle 构建配置以启用 multidex

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.0 rc2"

    defaultConfig {
        applicationId "com.try.app"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

In your manifest add the MultiDexApplication class from the multidex support library to the application element.在您的清单中,将 MultiDexApplication 类从 multidex 支持库添加到应用程序元素。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

Note: Visit https://developer.android.com/studio/build/multidex.html for more info.注意:访问https://developer.android.com/studio/build/multidex.html了解更多信息。

Set property multiDexEnabled to true in defaultConfig.在 defaultConfig 中将属性 multiDexEnabled 设置为 true。 This error occurs when method references increase the limit of 65k.当方法引用增加 65k 的限制时会发生此错误。 Check Google Play Service APIs.检查 Google Play 服务 API。

    android{
        ...
        defaultConfig {
            ...
            multiDexEnabled true
        }
        ...
    }

Here is a good source to get started with Google Maps Android API这是开始使用Google Maps Android API的好来源

I removed the dependencies as suggested by Loki and it works.我按照 Loki 的建议删除了依赖项,并且可以正常工作。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.google.android.gms:play-services-maps:9.4.0'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将Google Maps导入Google Maps项目时,多个dex文件定义了Android Studio错误 - Multiple dex files define Android studio error when trying to import Google Maps into Google Maps project 为什么在 Android Studio 中创建新项目时出现以下错误? - Why am I getting following error while I create a new project in Android studio? 确保启用了“Google Maps Android API v2”。 尝试实施 Google 地图时出现此错误 - Ensure that the "Google Maps Android API v2" is enabled. I am getting this error when I try to Implement Google Maps 为我的项目获取gradle错误多个dex文件定义了Lcom / google / android / gms / common / api / zze - getting gradle error for my project Multiple dex files define Lcom/google/android/gms/common/api/zze 为什么会出现错误:包com.google.android.maps不存在? - Why am I getting an error: package com.google.android.maps does not exist? 为什么运行Google Maps活动时出现此错误? - Why i am getting this error while running google maps activity? 什么是android中的.dex文件? 我收到一个错误多个dex文件定义了Lorg / springframework / core / NestedRuntimeException; - What is .dex file in android? I am getting an error Multiple dex files define Lorg/springframework/core/NestedRuntimeException; 为什么我尝试在 android studio 中打开新活动时出错 - why am i getting error when trying to open new activity in android studio 我的Android Studio项目中出现以下错误 - I am getting following error in my Android Studio Project 为什么在Android Studio中出现与主题相关的错误? - why am I getting an Theme related error in android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM