android - 我该如何解决这个错误?方法 ID 不在 [0, 0xffff] : 65536

标签 android android-studio android-activity build.gradle android-multidex

我使用 MapsActivity 创建了一个新的 Android Studio 项目,如果我尝试在我的华为 P8 Lite 设备上启动该应用程序,则会出现此错误:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

我的 Build.Gradle(模块:应用程序):

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 25
        buildToolsVersion "24.0.2"
        defaultConfig {
            applicationId "com.example.de.maptestdel"
            minSdkVersion 15
            targetSdkVersion 25
            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(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.1.0'
        compile 'com.google.android.gms:play-services:10.0.1'
        compile 'com.android.support:design:25.1.0'
        testCompile 'junit:junit:4.12'
    }

除了启用Multidex之外,还有其他方法可以解决吗? 听说不太好激活。

最佳答案

使用 Google Play 服务库中的特定/个人 API。您已使用 compile 'com.google.android.gms:play-services:10.0.1' 并且此库方法计数为 79958。

请引用此链接:https://developers.google.com/android/guides/setup

In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.

From version 6.5, you can instead selectively compile Google Play service APIs into your app.

例如

对于谷歌地图,使用:

com.google.android.gms:play-services-maps:10.0.1

方法数:17984

对于谷歌云消息:

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

方法数:15784

因此,使用单独的 API 将减少 apk 方法的数量。然后,无需启用 multidex。

关于android - 我该如何解决这个错误?方法 ID 不在 [0, 0xffff] : 65536,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42105328/

相关文章:

android - 从 Android 数组 ID 加载 ListView 中的图像

java - AsyncTask 不刷新 UI

android - OpenSL ES - 安卓工作室 - NDK 10R

java - 用户登录 Android Studio 后如何将应用程序打开到不同的 Activity

java - Box2D libgdx contactlistener

java - 允许非 ssl 连接 : javax.net.ssl.SSLHandshakeException: 握手失败

java - 使用 Sinch 即时通讯系统

Android,开始一个新的 Activity 并传递一个变量

android - 如何使 Activity (使用 Theme.dialog 显示)占据大部分屏幕 - Android

java - 由 : java. lang.IllegalStateException 引起:替换 Activity 中的任何 fragment 时,无法在 onSaveInstanceState 之后执行此操作