限制Android方法数量的原因是:

Android应用以DEX文件的形式存储字节码文件,在Dalvik字节码规范里,方法引用索引method
referenceindex只有16位,即65536个。

Op & Format

Mnemonic / Syntax

Arguments

6e..72 35c

invoke-kind {vC, vD, vE, vF, vG}, meth@BBBB

B: method reference index (16 bits)

注意是method reference,这里限制的是自己代码、Android框架、第三方库三者方法数量的总和。

dex里方法只能索引65536个,但是报错不在这里。Android打包dex的过程如下:

//Main.Java

-> main() -> run() ->不分包执行runMonoDex()(或者分包执行runMultiDex())->
writeDex()

// DexFile

->toDex() -> toDex0()

//
MethodIdsSection extends MemberIdsSection extends UniformItemSection extends  Section

->Section 的prepare() -> UniformItemSection的prepare0() ->MemberIdsSection的orderItems() -> getTooManyMembersMessage()

//Main.java

->getTooManyIdsErrorMessage()

MemberIdsSection里执行了这么一段代码

protected void orderItems() {

int idx = 0;

if (items().size() >DexFormat.MAX_MEMBER_IDX + 1) {

throw newDexIndexOverflowException(getTooManyMembersMessage());

}

for (Object i : items()) {

((MemberIdItem) i).setIndex(idx);

idx++;

}

}

getTooManyMembersMessage核心代码如下:构建了错误信息字符串。

private String getTooManyMembersMessage() {

try {

String memberType = this instanceofMethodIdsSection ? "method" : "field";

formatter.format("Too many %s references:%d; max is %d.%n" +

Main.getTooManyIdsErrorMessage() + "%n" +

"References bypackage:",

memberType, items().size(),DexFormat.MAX_MEMBER_IDX + 1);

return formatter.toString();

}

}

}

可见代码里检测了方法数量的上限,超过就会报错。这里的限制DexFormat.MAX_MEMBER_IDX

出自

public final classDexFormat {

/**

* Maximum addressable field or methodindex.

* The largest addressable member is0xffff, in the "instruction formats" spec as field@CCCC or

* meth@CCCC.

*/

public static final int MAX_MEMBER_IDX =0xFFFF;

}

因此方法不能超过65536。这里的根源还是在dex字节码规范里方法引用索引的限制,但是强制报错是在这里限制的。

另外,域的数量也有此限制。

错误信息:

Error:Errorconverting bytecode to dex:

Cause:com.android.dex.DexIndexOverflowException:field ID not in [0, 0xffff]: 65536(笔者开发时遇到的是域数量问题,方法数量问题错误信息于此类似)

:XXXXXX:transformClassesWithDexForDebug FAILED

Error:Executionfailed for task ':XXXXXX: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:\ProgramFiles\Java\jdk1.8.0_73\bin\java.exe'' finished withnon-zero exit value 2

解决方法:

Google官方出的分包方案,采用MultiDex支持库

1,配置building.gradle,开启MultiDex

android {

defaultConfig {

multiDexEnabled true

}

}

dependencies{

compile'com.android.support:multidex:1.0.0'

}

2,配置应用

方法1:在AndroidManifest.xml的application中声明android.support.multidex.MultiDexApplication;

<?xmlversion="1.0" encoding="utf-8"?>

<manifestxmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.android.multidex.myapplication">

<application

...

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

...

</application>

</manifest>

方法2:让你自己的Application类继承MultiDexApplication

方法3:让你自己的Application类重写attachBaseContext
方法,

@Override

protected voidattachBaseContext(Context base) {

super.attachBaseContext(base);

MultiDex.install(this);

}

其他错误情况:

Error:java.lang.OutOfMemoryError: Java heap space.

Please assign morememory to Gradle in the project's gradle.properties file.

For example, thefollowing line, in the gradle.properties file, sets the maximum Java heap sizeto 1,024 MB:

<em>org.gradle.jvmargs=-Xmx1024m</em>

:XXXXXX:transformClassesWithDexForDebugFAILED

Error:Executionfailed for task ':XXXXXX:transformClassesWithDexForDebug'.

>com.android.build.api.transform.TransformException:

java.lang.RuntimeException: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_73\bin\java.exe''finished with non-zero exit value 3

解决方法:

Building.gradle里

dexOptions {

javaMaxHeapSize "4g"

}

javaMaxHeapSize对应正则表达式是"\\d+[kKmMgGtT]?"4g指4G字节。

DexOptions类包含了获取JavaMaxHeapSize的方法,对应Building.gradle里dexOptions设置的javaMaxHeapSize。

参考:

http://source.android.com/devices/tech/dalvik/dalvik-bytecode.html

http://jayfeng.com/2016/03/10/%E7%94%B1Android-65K%E6%96%B9%E6%B3%95%E6%95%B0%E9%99%90%E5%88%B6%E5%BC%95%E5%8F%91%E7%9A%84%E6%80%9D%E8%80%83/

http://developer.android.com/intl/es/tools/building/multidex.html

关于Android方法数量限制的问题的更多相关文章

  1. Android方法引用超过65535的解决方式

    //在app/build.gradle android { compileSdkVersion buildToolsVersion "24.0.1" defaultConfig { ...

  2. Android方法引用数超过65535优雅解决

    随着应用不断迭代更新,业务线的扩展,应用越来越大(比如:集成了各种第三方SDK或者公共开源的Library文件.jar文件)这样一来,项目耦合性就很高,重复作用的类就越来越多了,SO:问题就来了.相信 ...

  3. 统计apk或jar中方法数量

    一.apk中方法数量 ./android-sdk-linux/build-tools/23.0.2/dexdump -f  apk路径 | grep method_ids_size 二.jar中方法数 ...

  4. IDA 调试 Android 方法及简单的脱壳实现

    IDA 调试 Android 方法及简单的脱壳实现 标签: android原创逆向调试dalvik 2016-05-24 14:24 9286人阅读 评论(3) 收藏 举报 分类: 原创(25) An ...

  5. 如何处理 android 方法总数超过 65536 . the number of method references in a .dex file exceed 64k

    一:问题描述:     应用中的Dex 文件方法数超过了最大值65536的上限,简单来说,应用爆棚了. 二.解决方案:      方案1:使用插件化框架  比如: https://github.com ...

  6. 极简Unity调用Android方法

    简介 之前写了篇unity和Android交互的教程,由于代码里面有些公司的代码,导致很多网友看不懂,并且确实有点小复杂,这里弄一个极简的版本 步骤 废话不多说,直接来步骤吧 1.创建工程,弄大概像这 ...

  7. JNI学习2:android 调用C语言方法与C语言调用android方法

    #include <jni.h> #include <stdio.h> #include <stdlib.h> #include <jni.h> #in ...

  8. 使用混淆ProGuard压缩代码和资源/减少方法数量

    ProGuard介绍 ProGuard是一个Java类文件压缩器,优化器,混淆器和预先文件验证器. 压缩步骤检测和删除未使用的类,字段,方法和属性. 优化步骤分析和优化方法的字节码. 混淆步骤使用短无 ...

  9. Unity3D调用android方法(非插件方式)

    关于Unity3Dproject与androidproject的转换与合并,请參考我的另外一篇博客.假设你对Unity3Dproject增加到androidproject的过程不熟悉.也请先看完以下这 ...

随机推荐

  1. 【Luogu P2781】 传教

    这题是可以用线段树做的. 虽然$n\leq 10^9$ 可以发现,真正需要用到的节点很少,故动态开点,只有需要用到的时候才新建节点. 这里我在下放标记的时候新建节点,因为每操作/查询一个节点都需要先下 ...

  2. ogre3D学习基础15 -- 创建BSP Scene Manager

    BSP(binary-space partitioning) Scene Manager(二叉空间分割)场景管理器比较适合用于室内场景. 第一,添加框架代码如下 #include "Exam ...

  3. CentOS7 'Username' is not in the sudoers file. This incident will be reported

    新装的 CentOS 需要安装许多软件,但是如果一开始你不是以 root 登入的话,就需要使用 sudo 进行切换,但是通常会报错如下图: 解决方法: 先用 root 用户登入系统, 打开文件 vi ...

  4. X-UA-Compatible设置IE浏览器兼容模式

    文件兼容性用来告诉IE,让它如何来编译你的网页. 指定 文件兼容性模式 以下是指定为Emulate IE7 mode 兼容性范例. <html> <head> <!-- ...

  5. hdu 4176

    Class Statistics Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. rsync 使用小记

    工作中遇到了有关rsync使用的问题,在这里记录下供有同样需求的人参考一下 先说下环境 服务端配置 pid file = /rsyncdata/rsyncd.pid port = 873 addres ...

  7. python在windows下UnicodeDecodeError的解决方法

    之前在windows下使用python调用某些模块时都会报错,像这样: C:\Documents and Settings\Administrator>python -m CGIHTTPServ ...

  8. Python之数据结构:字符串中的方法

    一.过滤字符串 1.strip() (1)去掉行收尾不可见字符 a = ' wejifrow ' print a print a.strip() 结果: wejifrow wejifrow (2)st ...

  9. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  10. 2721: [Violet 5]樱花

    2721: [Violet 5]樱花 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 547  Solved: 322[Submit][Status][D ...