Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the structure of Android APK

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand the structure of Android APK". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the structure of Android APK".

Catalogue

I. Analysis of APK composition

1.1 Apk Analysis tool

1.2 expansion of Dex knowledge points

Second, build the source code guide

2.1 Source code introduction

2.2 BuildConfig Task detailed explanation

2.3 get all the class names corresponding to task

Third, carding the construction process.

Fourth, manually build APK

Author: hockeyli, client developer for Tencent PCG

I. Analysis of APK composition

Before we begin to analyze the Android build process, let's take a look at the overall composition of the built end product APK:

APK mainly consists of five parts, namely:

The product of Dex:.class file processing, the executable file of Android system

Resource: resource files, including layout, drawable, animator, referenced by R.XXX.id

Assets: resource files, loaded through AssetManager

Library:so inventory catalogue

Information about META-INF:APK signature

1.1 Apk Analysis tool

If you want to do good work, you must first sharpen its tools. Since you want to analyze APK, you must have useful tools.

APK Analyzer included with ① Android Studio

With the APK parser, we can do this:

View the absolute and relative sizes of files in APK, such as DEX and Android resource files

Understand the composition of DEX files

Quickly view the final version of a file (such as AndroidManifest.xml) in APK

Compare two APK side by side

② ClassyShark can be used as a supplement to AS's built-in APK analyzer to help us analyze the detailed data in dex and view the total number of methods in APK and the distribution of methods in each module.

1.2 expansion of Dex knowledge points

When we look at an APK in Android, we can see that there are Defined Methods and Referenced Methods in the upper right corner, but most people may not know the difference between the two. Here is a brief explanation:

Defined Methods: the methods defined in this Dex; the methods referenced by Referenced Methods:Defined Methods and Defined Methods.

Android has a 64K citation limit. When type_ids, method_ids or field_ids exceeds 65536 (64 * 1024), dex subcontracting is required. In order to minimize the number of Dex, we need to improve "Dex information efficiency" as much as possible.

Dex message efficiency = number of Defined Methods / number of Referenced Methods

Second, build the source code guide

When we use Android Studio to build the installation package, we will find that it is actually running a series of Task, that is, the cooperation of these task, and finally build our APK.

2.1 Source code introduction

If we want to know more about the building process of Android, we must know something about the relevant source code. So how do we see the source code related to Task? we know that Android is built with Gradle, which means that these task are actually placed in Gradle. If we want to see the source code in Gradle, we can compile Gradle in build.gradle.

CompileOnly "com.android.tools.build:gradle:3.0.1"

After compiling, you can find the code that creates these Task in ApplicationTaskManager#createTasksForVariantScope, which means that you can find the real implementation logic of these Task.

2.2 BuildConfig Task detailed explanation

Here, take the generation of the BuildConfig file as an example to sort out how to view the code logic of a task.

To generate the BuildConfig file, the corresponding task is created through createBuildConfigTask in ApplicationTaskManager.

Following the code logic, we find that the task,GenerateBuildConfig of GenerateBuildConfig is inherited from BaseTask. Here is a tip: the real execution logic in Task is on the method with @ TaskAction annotation, so we can quickly find the corresponding generate () method. You can see that the logic of generating the overall BuildConfig is relatively simple, in fact, it is to read the attributes that come with build.gradle and our custom attributes, and then generate the corresponding BuildConfig file through JavaWriter.

2.3 get all the class names corresponding to task

Seeing the above example, some people may ask a question: how do we determine which class the task executed in the build corresponds to? here is a tip: we can print all the task name and the corresponding class after the taskGraph build is complete. For example, after adding this code to build.gradle, we will print out the corresponding class names of task at run time.

Third, carding the construction process.

You can see that multiple tools are involved in the Android build, and we can view the relevant build tools through open $ANDROID_HOME/build-tools.

Fourth, manually build APK

Finally, we manually package an executable APK through the command line, which gives us a deeper understanding of the APK build. First of all, you need to prepare the code, resource files, AndroidManifest, and the necessary files to build APK.

① compiles res resources into .flat binaries through aapt2 compile:

Aapt2 compile-o build/res.zip-- dir res

② connects .flat and AndroidManifest through aapt2 link and converts them into apk and R.java that do not contain dex:

Aapt2 link build/res.zip-I $ANDROID_HOME/platforms/android-30/android.jar-- java build-- manifest AndroidManifest.xml-o build/app-debug.apk

③ compiles the Java file into a .class file through javac:

Javac-d build-cp $ANDROID_HOME/platforms/android-30/android.jar com/**/*.java

④ converts .class files to dex files through d8:

D8-- output build/-- lib $ANDROID_HOME/platforms/android-30/android.jar build/com/tencent/hockeyli/androidbuild/*.class

⑤ merges dex and resource components:

Zip-j build/app-debug.apk build/classes.dex

⑥ signs apk via apksigner:

Apksigner sign-ks ~ / .android/debug.keystore build/appdebug.apk Thank you for your reading, the above is the content of "how to understand the structure of Android APK". After the study of this article, I believe you have a deeper understanding of how to understand the structure of Android APK, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report