错误:# Cannot fit requested classes in a single dex file (# methods: 74519 > 65536)

最近开发安卓程序遇到以下错误:

Cannot fit requested classes in a single dex file (# methods: 74519 > 65536)

翻译:
无法将请求的类放入单个dex文件(#方法:74519>65536)

大致意思是Android App中的方法数超过65535,如果往下兼容到低版本设备时,就会报编译错误,尤其在引入一些jar包之后容易出现这个错误,因为一些Android系统定义单个dex的总方法数的最大值是65535,当你引入一些jar包后方法超过65535个方法。一个dex已经装不下了,需要个多个dex,也就是multidex。

解决方案

1.配置依赖

在app目录下的build.gradle文件里的dependencies下添加如下依赖:

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

然后在android下的defaultConfig添加以下配置:

multiDexEnabled true

配置好后我的文件内容如下:

build.gradle

plugins {
id 'com.android.application'
} android {
compileSdkVersion 30
buildToolsVersion "30.0.2" defaultConfig {
applicationId "minuy.android.nlecloud"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true
} buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
} dependencies { implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation project(path: ':nlecloud-sdk')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation("com.squareup.okhttp3:okhttp:4.9.0") implementation 'com.android.support:multidex:1.0.3'
}

2.自定义Application子类

新建一个Java类并继承自Application

覆盖onCreate方法。

onCreate方法里加入如下语句:

MultiDex.install(this);

我的Java类内容如下:

MainApplication.java

package minuy.android.nlecloud;  

import android.app.Application;
import androidx.multidex.MultiDex; public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
}
}

3.配置AndroidManifest.xml

application下加入如下语句:

android:name=".{$刚刚新建的类名}"

例如我的:

android:name=".MainApplication"

配置好后,我的AndroidManifest.xml文件内容如下:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="minuy.android.nlecloud"> <uses-permission android:name="android.permission.INTERNET" /> <application
android:name=".MainApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher_icon"
android:label="@string/app_name"
android:roundIcon="@drawable/ic_launcher_icon"
android:supportsRtl="true"
android:theme="@style/Theme.NleCloud">
<activity android:name=".StartActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

问题解决!

参考:

https://blog.csdn.net/m0_37707561/article/details/84983184

【错误解决】Android APK 方法数量限制的更多相关文章

  1. android studio 更新 Gradle错误解决方法(Gradle sync failed)

    android studio 更新 Gradle错误解决方法   Android Studio每次更新版本都会更新Gradle这个插件,但由于长城的问题每次更新都是失败,又是停止在Refreshing ...

  2. 安卓安装提示:Android SDK requires Android Developer Toolkit version 21.1.0 or above. (错误解决方法)

    安卓安装提示:Android SDK requires Android Developer Toolkit version 21.1.0 or above.  (错误解决方法) 主要是因为版本号不正确 ...

  3. Android Studio Eclipse运行时出现 finished with non-zero exit value 2 错误解决方法

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 微博:http://weibo.com/mcxiaobing Error:Ex ...

  4. android.util.AndroidRuntimeException: requestFeature() must be called before adding content 错误解决方法

    Activity全屏,网上的代码如下:protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance ...

  5. Android layoutInflate.inflate 方法具体解释,removeView()错误解决

    错误: The specified child already has a parent. You must call removeView(). 解答: 这个错误非常直白,就是你viewGroup. ...

  6. android打包方法超过65k错误

    近日,Android Developers在Google+上宣布了新的Multidex支持库,为方法总数超过65K的Android应用提供了官方支持. 如果你是一名幸运的Android应用开发者,正在 ...

  7. 关于Android方法数量限制的问题

    限制Android方法数量的原因是: Android应用以DEX文件的形式存储字节码文件,在Dalvik字节码规范里,方法引用索引method referenceindex只有16位,即65536个. ...

  8. react-native学习(RN)--之Window环境下搭建环境配置,以及初始化建立react-native项目,(真机和模拟器运行的相关错误解决办法,android打包报错)

    react-native以后会更火的 一.安装java 二.安装Android Studio 三.安装react-native需要的Android studio额外部分 四.安装nodeJS  五.安 ...

  9. jack server 常见错误解决方法【转】

    本文转载自:https://blog.csdn.net/qq_27061049/article/details/70156200 jack 服务常见错误解决方法 当你编译Android时,你不需要修改 ...

  10. 解决Android单个dex文件不能超过65535个方法问题

    一.找坑:谷歌规定单个dex文件中的方法不能超过65536的限制 我们编写项目过程中在工程的lib文件夹下引用的第三方插件jar包太多或者项目过大,编译运行时就有可能报出com.android.dex ...

随机推荐

  1. 关于动态使用keepAlive不生效的问题

    首先,我想实现在返回页面时,页面不进行刷新,比如我原先选择的第四页,返回后显示了第一页 想到使用keepAlive缓存组件,大部分推荐的方法为这样,但是不生效 <keep-alive v-if= ...

  2. Solution Set - “让季节停止哽咽”

    目录 0.「CTT 2017」「洛谷 P4004」Hello world! 1.「CTT 2017」「洛谷 P4006」小 Y 和二叉树 2.「CTT 2017」「洛谷 P4226」避难所 3.「AG ...

  3. JAVA8 函数式编程(1)- Lambda表达式

    1 简介 简洁的代码就能处理大型数据集合,让复杂的集合处理算法高效的运行在多核CPU上. 面向对象编程是对数据进行抽象,而函数式编程是对行为进行抽象,能编写出更易读的代码--这种代码更多地表达了业务逻 ...

  4. 为了解决服务启动慢的问题,我为什么要给Apollo和Spring提交PR?

    最近在整理之前记录的工作笔记时,看到之前给团队内一组服务优化启动耗时记录的笔记,简单整理了一下分享出来.问题原因并不复杂,主要是如何精准测量和分析,优化后如何定量测量优化效果,说人话就是用实际数据证明 ...

  5. Spring Cloud的5大核心组件详解

    Spring Cloud Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而 ...

  6. jdk 5.0 新增的foreach循环(用于遍历集合、数组)

    使用 foreach 循环遍历集合元素 Java 5.0 提供了 foreach 循环迭代访问 Collection和数组. 遍历操作不需获取Collection或数组的长度,无需使用索引访问元素 ...

  7. rust体验感受,Rust标准库需要还需加强

    了解到Rust的跨平台编译和安全性,尝试用rust写一个http调用的程序,换了几个http client库都失败了,感觉rust语言还有较大的进步空间. 环境 OS: windows 11 rust ...

  8. 0101-JDK和tomcat的安装配置

    一.JDK8安装与配置 分别配置如下三个系统变量 JAVA_HOME设置变量值为java JDK的安装目录例如: C:\Program Files\Java\jdk1.8.0 PATH添加变量值 %J ...

  9. VScode导入Vue项目

    VScode导入Vue项目 1.使用VScode打开文件夹 2.找到运行按钮 3.判断有没有默认的配置文件存在 4.若文件夹存在就检查配置文件是否存在 (1)打开launch.json,把如下代码粘贴 ...

  10. 并发编程 - 线程同步(五)之原子操作Interlocked详解二

    上一章我们学习了原子操作Interlocked类的几个常用方法,今天我们将继续学习该类的其他方法. 01.Exchange方法 该方法用于原子的将变量的值设置为新值,并返回变量的原始值.该方法共有14 ...