iBotPeaches / Apktool

A tool for reverse engineering Android apk files

Home Page:https://apktool.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Error while recompiling because of exceed the method references limitation

hoangnd2002 opened this issue · comments

Information

  1. Apktool Version (apktool -version) - 2.9.3
  2. Operating System (Mac, Linux, Windows) - MacOs
  3. APK From? (Playstore, ROM, Other) - TestApp
  4. Java Version (java --version) - Java17

Stacktrace/Logcat

Exception in thread "main" com.android.tools.smali.util.ExceptionWithContext: Exception occurred while writing code_item for method Landroidx/viewpager2/adapter/FragmentStateAdapter;->h(Landroidx/recyclerview/widget/RecyclerView$ViewHolder;I)V
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeDebugAndCodeItems(DexWriter.java:1119)
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeTo(DexWriter.java:415)
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeTo(DexWriter.java:370)
	at brut.androlib.src.SmaliBuilder.build(SmaliBuilder.java:56)
	at brut.androlib.src.SmaliBuilder.build(SmaliBuilder.java:35)
	at brut.androlib.ApkBuilder.buildSourcesSmali(ApkBuilder.java:211)
	at brut.androlib.ApkBuilder.buildSources(ApkBuilder.java:145)
	at brut.androlib.ApkBuilder.build(ApkBuilder.java:99)
	at brut.apktool.Main.cmdBuild(Main.java:296)
	at brut.apktool.Main.main(Main.java:95)
Caused by: com.android.tools.smali.util.ExceptionWithContext: Error while writing instruction at code offset 0x3f
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeCodeItem(DexWriter.java:1392)
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeDebugAndCodeItems(DexWriter.java:1115)
	... 9 more
Caused by: com.android.tools.smali.util.ExceptionWithContext: Unsigned short value out of range: 65540
	at com.android.tools.smali.dexlib2.writer.DexDataWriter.writeUshort(DexDataWriter.java:115)
	at com.android.tools.smali.dexlib2.writer.InstructionWriter.write(InstructionWriter.java:285)
	at com.android.tools.smali.dexlib2.writer.DexWriter.writeCodeItem(DexWriter.java:1319)
	... 10 more

Steps to Reproduce

  1. I use apktool to decompile an apk. Then, I want to inject some new classes into the first dex file classes.dex and recompile the apk file but caught the above error.
  2. Using APKAnalyzer, the classes.dex only have about 50000 method references, and the injected classes has less than 20 method references. According to the specification, It can't exceed the limitation of the dex file.
    How could I solve it?

In small situations the process of disassembly and reassembly may make more classes than originally there. Either way this isn't a bug - if you exceed the method limit of a dex you must move methods to a secondary dex file or introduce one.