The author
Hello everyone, my name is Xiao Jia; I graduated from Guangdong University of Technology with a bachelor’s degree in 20 years. I joined the 37 Mobile Games Android team in June 2020. At present, I am working on android related development for domestic game release.
The problem background
In daily package cutting business, some channel SDKS already contain kotlin related code. Because the kotlin adaptation was not done before, the game package was flash back after the package cutting.
Daily packet cutting services are shown as follows:
Observe the resources under the normal Demo package:
Comparing the game package resources with the normal demo package, we found that the Kotlin directory is missing, and the meta-INF directory is missing a lot of resources.
Files are divided into four types based on directories and suffixes:
Kotlin_module is in the meta-INF directory. The version file is in the meta-INF directory. Kotlin_metadata is in the kotlin directory. Kotlin_builtins is in the kotlin directory
Kotlin_module file
Essentially to optimize potential package name conflicts when defining top-level functions/variables, quick lookups are achieved through a separate kotlin_module. The other is reflection use.
In Kotlin, for example, you can write code that creates a package named top-level function without specifying a class
After compiling, it is the same as the following in Java
The kotlin_module file is generated for each lib/module, and the dimension of the file is lib library granularity, not per-class fileIf you create another libkotlin, the kotlin_module will add anotherExcept for the files added by the developer’s Kotlin module, everything else is generated by the Kotlin library
In the Demo tests, the deletion didn’t cause any problems. This configuration is used at compile time to tell the compiler how to correctly generate class code. It is not used at run time and can be removed during APK package guidance. Kotlin Reflect might get an error if used.
Kotlin_metadata file
Binary file, from the decomcompiled data, kotlin class after compilation, with a MetaData annotation, and configuration information is very much, which directly leads to kotlin code generated class file, than Java code generated class larger.
The compiled class
These metadata files were deleted in the Demo and the program worked well, indicating that they were not used during the run, but other reflection calls may be involved.
Kotlin_builtins file
A binary file that is new to the kotlin base library. If deleted, the reflection instantiation runtime will report an error.
. Verizon file
Plain text file, inside is usually a version number.
Decompile demo package with apkTool:
.kotlin_module and.version are in the original/ meta-INF directory
.kotlin_metadata and.kotlin_builtins are in the kotlin directory
Since packet cutting is done in the middle tier, it is best to keep these related files
To sum up, the kotlin packet cutting adaptation steps are as follows:
1. When decompiling channel resources, retain the original directory and meta-INF directory
2. To merge channel resources, merge the original and meta-INF directories
3. Delete the signature file in meta-INF before compiling
Java -jar apktool.jar -c b XXX Add the -c parameter. The -c parameter means to save the original directory
5. Save the kotlin directory
Detailed steps:
- Keep the Kotlin directory
The directory where apktool will be decompiled: the kotlin directory will exist
This 2.4.1 version of apktool is reserved for the normal directory, so it needs to be reserved if the decompression exists in the Kotlin directory.
- Keep the Orignal directory
Contents saved in the Original directory:
Androidmanifest.xml and meta-INF (signature file) Here you only need to save the meta-INF directory. Note: If androidmanifest.xml exists in the Original directory, it will be compiled from the manifest file when it is compiled, rather than from the direct directory androidmanifest.xml
-
Keep the meta-nif directory
-
If RSA/MF/ SF signature file is not deleted, it is the signature file before compilation. Because the apK package file has been modified before compilation, an error will be reported after compilation.
The build/ APk folder is the directory used to generate apK and contains the following contents: