我正在用SugarORM库构建一个应用程序,但当我尝试为API 17构建项目时(没有检查其他人),它显示构建错误。

    Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2330Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72330Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72330Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2330Library UP-TO-DATE
:app:prepareComAndroidSupportMediarouterV72300Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72330Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42330Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2330Library UP-TO-DATE
:app:prepareComAndroidVolleyVolley100Library UP-TO-DATE
:app:prepareComGithubSatyanSugar14Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServices840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAds840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAnalytics840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppinvite840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppstate840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAuth840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesCast840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesDrive840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesFitness840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesGames840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesGcm840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesIdentity840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesLocation840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesMaps840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesMeasurement840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesNearby840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesPanorama840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesPlus840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesSafetynet840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesVision840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesWallet840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesWearable840Library UP-TO-DATE
:app:prepareMeDrakeetMaterialdialogLibrary131Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:prePackageMarkerForDebug
:app:transformClassesWithDexForDebug
Error:The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html
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.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 21.663 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console

但是当我为android v5.0或更高版本构建这个项目时,它工作得很好。如果我删除SugarORM gradle依赖项,它可以在v4.2.2和v5.0设备上正常工作。


你的方法太多了。dex只能有65536个方法。

正如建议的那样,您可以使用multidex支持。

只需在/build.gradle模块中添加这些行:

android {
   
    defaultConfig {
        ...
        
        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  implementation 'androidx.multidex:multidex:2.0.1'  //with androidx libraries
  //implementation 'com.android.support:multidex:1.0.3'  //with support libraries
  
}

或者如果使用module/build.gradle.kts:

android {
    // other properties

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled = true
    }
    ...
}

dependencies {
    implementation("androidx.multidex:multidex:2.0.1")  // with androidx libraries
    // implementation("com.android.support:multidex:1.0.3")  // with support libraries
}

同样在清单中,将multidex支持库中的MultiDexApplication类添加到应用程序元素中

    <?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="androidx.multidex.MultiDexApplication">

            <!-- If you are using support libraries use android:name="android.support.multidex.MultiDexApplication" -->

            <!--If you are using your own custom Application class then extend -->
            <!--MultiDexApplication and change above line as-->
            <!--android:name=".YourCustomApplicationClass"> -->

            ...
        </application>
    </manifest>

如果您正在使用自己的Application类,请将父类从Application更改为MultiDexApplication。 如果你不能这样做,在你的Application类重写attachBaseContext方法:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(newBase);
    MultiDex.install(this);
}

另一种解决方案是尝试使用ProGuard删除未使用的代码-为应用程序配置ProGuard设置以运行ProGuard,并确保您已为发布版本启用收缩。


我得到这个错误消息,因为在编码我的项目自动更新编译版本在我的构建。Gradle文件:

android {
    ...
    buildToolsVersion "23.0.2"
    ...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0' }

通过修正版本来解决:

android {
        ...
        buildToolsVersion "23.0.2"
        ...
    }

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
}

在 android/app/build.gradle 中

android {

compileSdkVersion 23

 buildToolsVersion '23.0.0'

    defaultConfig {
        applicationId "com.dkm.example"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

把这个放在defaultConfig里面:

multiDexEnabled true 

这对我很有用


这样做,它会起作用:

defaultConfig {
    applicationId "com.example.maps"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}

这对我来说很管用:

这是因为有太多未使用的方法。 这些方法大多来自build.gradle中包含的库

使用缩小和缩小资源来修复gradle,同时清理你的代码。

buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            shrinkResources true
        }
    }

对我来说,升级Gradle很管用。在Android网站上查找更新 然后将其添加到构建中。gradle (Project)像这样

 dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0-alpha4'   
          ....
    }

然后同步项目与gradle文件加上它可能会发生有时因为java.exe(在我的情况下)只是强制杀死java.exe从任务管理器在Windows然后重新运行程序


我一直面临着同样的问题,对于多索引支持,你必须记住你的应用程序的minSdkVersion。如果你使用minSdkVersion 21或以上版本,那么只需要像这样写multiDexEnabled true

defaultConfig {
    applicationId *******************
    minSdkVersion 21
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}

它适用于我,如果你使用minSdkVersion低于21(低于棒棒糖),那么你必须做两个额外简单的事情

1. 首先添加这个依赖项

compile’com。android支持:multidex: 1。0。1

在你的房子里,gradle。

2. 最后和第二在清单中添加下面这一行到您的应用程序

android: name = " android.support.multidex.MultiDexApplication "

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:name="android.support.multidex.MultiDexApplication"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

宾果,那么它将在较低的版本也工作..:) 快乐的编码


你可以在Android Studio上启用“即时运行”来获得多索引支持。


也可以试试这个:

android{
    .
    .
    // to avoid DexIndexOverflowException
    dexOptions {
       jumboMode true
    }
}

希望它能帮助到别人。谢谢


这个错误也会发生当你加载所有谷歌播放服务api时,你只使用几个。

正如谷歌所说:“在6.5之前的谷歌Play服务版本中,你必须将整个api包编译到你的应用程序中。在某些情况下,这样做会使你更难将应用程序中的方法数量(包括框架api、库方法和你自己的代码)控制在65,536个限制之下。

从6.5版开始,你可以选择性地将谷歌Play服务api编译到你的应用程序中。”

例如,当你的应用需要play-services-maps,play-services-location时,你只需要在你的构建中添加这两个api。Gradle文件在应用程序级别如下所示:

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

而不是:

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

有关谷歌播放服务api的完整文档和列表,请单击这里


当你的应用引用超过65,536个方法时,你会遇到一个构建错误,表明你的应用已经达到了Android构建架构的限制

Android 5.0之前的Multidex支持

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

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

Android 5.0及更高版本的Multidex支持

Android 5.0 (API级别21)及更高版本使用了一个名为ART的运行时,它支持从APK文件加载多个DEX文件。因此,如果您的minSdkVersion是21或更高,则不需要multidex支持库。

避免64K的限制

使用ProGuard删除未使用的代码-启用代码收缩

在app for中配置multidex

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

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

如果你的minSdkVersion设置为20或更低,那么你必须使用multidex支持库

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

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

重写Application类,将其更改为扩展MultiDexApplication(如果可能的话),如下所示:

public class MyApplication extends MultiDexApplication { ... }

添加到清单文件

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

在添加对multidex的支持之前,请确保您没有添加不必要的依赖项。

比如在脸书官方分析指南中

它们清楚地说明你应该添加以下依赖项:

implementation 'com.facebook.android:facebook-android-sdk:[4,5)'

这实际上是整个FacebookSDK,所以如果你只需要分析,你需要用:

implementation 'com.facebook.android:facebook-core:5.+'

Facebook部分SDK选项


更改应用程序级别构建。gradle:

android {

compileSdkVersion 23

 buildToolsVersion '23.0.0'

    defaultConfig {
        applicationId "com.dkm.example"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

这对我很管用。


**

针对Unity游戏开发者

**

如果有人来这里是因为这个错误出现在他们的Unity项目,去文件->构建设置->播放器设置->播放器。进入“发布设置”,在“构建”选项卡下,启用“自定义启动器Gradle模板”。一个路径将显示在该文本下。进入路径并添加multiDexEnabled true,如下所示:

defaultConfig {
    minSdkVersion **MINSDKVERSION**
    targetSdkVersion **TARGETSDKVERSION**
    applicationId '**APPLICATIONID**'
    ndk {
        abiFilters **ABIFILTERS**
    }
    versionCode **VERSIONCODE**
    versionName '**VERSIONNAME**'
    multiDexEnabled true
}

添加这个可以避免react native或任何android项目的multidex问题

android {

defaultConfig {
    ...

    // Enabling multidex support.
    multiDexEnabled true
}

}

dependencies {
  implementation 'com.android.support:multidex:1.0.3'  //with support libraries
  //implementation 'androidx.multidex:multidex:2.0.1'  //with androidx libraries

我把minSdkVersion改成了20,这对我很有效。更多详情请登录https://developer.android.com/studio/build/multidex#mdex-gradle