65K问题相信不少人都遇到过,65K即65536,关于这个值,是怎么来的?本文进行探究!
1
|
Unable to execute dex: method ID not in [0, 0xffff]: 65536
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/* * Direct-mapped "header_item" struct. */ struct DexHeader { u1 magic[8]; u4 checksum; u1 signature[kSHA1DigestLen]; u4 fileSize; u4 headerSize; u4 endianTag; u4 linkSize; u4 linkOff; u4 mapOff; u4 stringIdsSize; u4 stringIdsOff; u4 typeIdsSize; u4 typeIdsOff; u4 protoIdsSize; u4 protoIdsOff; u4 fieldIdsSize; u4 fieldIdsOff; u4 methodIdsSize; // 这里存放了方法字段索引的大小,methodIdsSize的类型为u4 u4 methodIdsOff; u4 classDefsSize; u4 classDefsOff; u4 dataSize; u4 dataOff; }; |
1 2 3 4 5 6 7 8 9 10 11 |
/* * These match the definitions in the VM specification. */ typedef uint8_t u1; typedef uint16_t u2; typedef uint32_t u4; typedef uint64_t u8; typedef int8_t s1; typedef int16_t s2; typedef int32_t s4; typedef int64_t s8; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * Combine two dex files into one. */ public final class DexMerger { private void mergeMethodIds() { new IdMerger |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * Maps the index offsets from one dex file to those in another. For example, if * you have string #5 in the old dex file, its position in the new dex file is * {@code strings[5]}. */ public final class IndexMap { private final Dex target; public final int[] stringIds; public final short[] typeIds; public final short[] protoIds; public final short[] fieldIds; public final short[] methodIds; // ... ... } |
1 2 3 4 5 6 7 8 9 |
// 源码目录:dalvik/dx // Main.java -> main() -> run() -> runMonoDex()(或者runMultiDex()) -> writeDex() // DexFile -> toDex() -> toDex0() // MethodIdsSection extends MemberIdsSection extends UniformItemSection extends Section -> prepare() -> prepare0() -> orderItems() -> getTooManyMembersMessage() // Main.java -> getTooManyIdsErrorMessage() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.android.dx.dex.file; import com.android.dex.DexException; import com.android.dex.DexFormat; import com.android.dex.DexIndexOverflowException; import com.android.dx.command.dexer.Main; import java.util.Formatter; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.atomic.AtomicInteger; /** * Member (field or method) refs list section of a {@code .dex} file. */ public abstract class MemberIdsSection extends UniformItemSection { /** * Constructs an instance. The file offset is initially unknown. * * @param name {@code null-ok;} the name of this instance, for annotation * purposes * @param file {@code non-null;} file that this instance is part of */ public MemberIdsSection(String name, DexFile file) { super(name, file, 4); } /** {@inheritDoc} */ protected void orderItems() { int idx = 0; if (items().size() > DexFormat.MAX_MEMBER_IDX + 1) { throw new DexIndexOverflowException(getTooManyMembersMessage()); } for (Object i : items()) { ((MemberIdItem) i).setIndex(idx); idx++; } } private String getTooManyMembersMessage() { Map |
1 2 3 4 5 6 7 8 9 10 11 12 |
// 如果方法数大于0xffff就提示65k错误 if (items().size() > DexFormat.MAX_MEMBER_IDX + 1) { throw new DexIndexOverflowException(getTooManyMembersMessage()); } // 这个DexFormat.MAX_MEMBER_IDX就是0xFFFF /** * Maximum addressable field or method index. * The largest addressable member is 0xffff, in the "instruction formats" spec as field@CCCC or * meth@CCCC. */ public static final int MAX_MEMBER_IDX = 0xFFFF; |
5. 根本原因
6. 回顾
本文转载:http://jayfeng.com/2016/03/10/由ANdroid-65k方法限制引发的思考