如何防止 apktool 对全局变量进行排序?

How prevent apktool sort global variables?

您好,请原谅我的英语不好。我的 android 应用程序中有一个名为 Class A 的 class,其内容如下:

public class A {


public static final SomeClass1 variable_C = new SomeClass1();
public static final SomeClass1 variable_A = new SomeClass1();
public static final SomeClass1 variable_D = new SomeClass1();
public static final SomeClass1 variable_B = new SomeClass1();


}

当我编译我的项目,然后将我的项目 apk 提供给 apktool 进行反编译时,apktool 反编译了 Class A Like below :

public class A {


public static final SomeClass1 variable_A = new SomeClass1();
public static final SomeClass1 variable_B = new SomeClass1();
public static final SomeClass1 variable_C = new SomeClass1();
public static final SomeClass1 variable_D = new SomeClass1();

}

apktool 在反编译我的项目 apk 时将全局变量排序更改为字母顺序。 我如何强制 apktool 不按字母顺序对全局变量进行排序,并在反编译我的 apk 时保持主要全局变量的顺序?

感谢您的回答。

这不是 apktool 的错。与 Java 类文件格式不同,Android dex 格式要求字段按排序顺序出现,这意味着一旦编译代码,源代码级别的顺序就会丢失。

来自https://source.android.com/devices/tech/dalvik/dex-format#class-data-item

the defined static fields, represented as a sequence of encoded elements. The fields must be sorted by field_idx in increasing order.

field identifiers list. These are identifiers for all fields referred to by this file, whether defined in the file or not. This list must be sorted, where the defining type (by type_id index) is the major order, field name (by string_id index) is the intermediate order, and type (by type_id index) is the minor order. The list must not contain any duplicate entries.