question:
(1) What are the benefits of understanding the APK installation process
(2) Understand what problems the APK installation process can solve
1. What can be done in the installation process
Installation is divided into the following three stages. What can be done in each stage to help us optimize the installation process and solve some problems after installation?
(1) Before and during installation: In these two stages, third-party applications can't do much. Generally, applications such as application distribution APP store, game center, browser, and application treasure will pay attention to these two states.
(2) After installation: At this stage, whether it is a built-in application or a third-party application, you will more or less encounter some problems, such as the so file cannot be found, the image storage, cached data, etc. are abnormal...
2. Before installation
Before installation, it is nothing more than choosing an installation method that can be used according to your own application.
2.1 pm command installation method
For vendor applications with system signatures, silent installation capabilities can be achieved by using the pm command.
String cmd = "pm install -r -d /data/data/android.apk"
Runtime run = Runtime.getRuntime();
Process process = run.exec(cmd);
2.2 Package Installation Manager Installation
Applications that are not signed by the system can only be installed using the package installation manager.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android" + ".package-archive");
mContext.startActivity(intent);
2.3 session command installation
The reason for using session installation is because starts from Android 8.0, and the pm command cannot realize the silent installation of , otherwise it will directly display the installation failure. However, no adaptation scheme for the silent method has been found on the Internet. Perhaps this is because this compatibility is only concerned by manufacturers, and third-party applications do not pay attention to compatibility with this method. The compatibility scheme is given below:
int sessionId = packageInstaller.createSession(params);
InstallLog.d(TAG, "doPackageStage creat sessionId is : " + sessionId);
final byte[] buffer = new byte[65536];
session = packageInstaller.openSession(sessionId);
final InputStream in = new FileInputStream(file);
final long sizeBytes = file.length();
final OutputStream out = session.openWrite("PackageInstaller", 0, sizeBytes);
try {
int c;
while ((c = in.read(buffer)) != -1) {
out.write(buffer, 0, c);
}
session.fsync(out);
} catch (IOException ex) {
InstallLog.e(TAG, "doPackageStage ioException : " + ex.getMessage(), ex);
} finally {
InstallUtils.closeQuietly(in);
InstallUtils.closeQuietly(out);
}
This method is the code extracted from the packageManager, which can realize the silent installation of the application.
3. Installing
3.1 APK structure
The APK file is actually in zip format, and generally contains one or more dex files, resources.arsc, AndroidManifest.xml, res directory, META-INF directory and the lib directory containing the so library:
3.2 Storage directories involved in the installation process
- system/app——The built-in application, that is, the application that comes with the system, cannot be deleted.
- data/app——The directory where the user program is installed, with delete permission. Copy the apk file to this directory during installation.
- data/data-store application data, such as some sp cache data.
- data/dalvik-cache-Install the dex file in the APK into the dalvik-cache directory (the dex file is the executable file of the dalvik virtual machine, and its size is about one-fourth of the original APK file size).
3.3 Main installation process
The installation process is summarized as follows: copy the APK installation package to the /data/app directory, unzip and scan the installation package, inject APK resources into the resource manager, parse the AndroidManifest file, and create a corresponding application data directory under the /data/data directory. Then optimize the dex file for the dalvik/art environment, save it to the dalvik-cache directory, register the components and permissions parsed by the AndroidManifest file to the PackageManagerService, and send the broadcast after completion.
3.4 Points that can be optimized during installation
During installation, this process seems to have nothing to do, but for manufacturer applications, the installation speed of the application can have a lot of room for improvement. The differential package upgrade such as application update is a common incremental update method.
After a series of tests and verifications, it was found that the speed of application installation itself is related to some factors, the most important being the frequency of CPU usage.
As we all know, the current mobile phones are relatively high-end, with 8 cores, but during the application installation process, analyzing the trace file can confirm that it is not the full load of the 8-core thread to complete the installation of an application, but a part of the thread runs on the high core. Part of it is low nuclear.
3.4.1 Introduction to CPU operating frequency
cpuinfo\_max\_freq cpuinfo\_min\_freq: The maximum operating frequency and minimum operating frequency supported by the CPU hardware are given respectively,
- cpuinfo\_cur\_freq will read the current operating frequency of the CPU from the CPU hardware register.
- When the Governor selects the appropriate operating frequency, it will only select within the frequency range determined by scaling\_max\_freq and scaling\_min\_freq.
- scaling\_cur\_freq returns the current operating frequency of the CPU cached by the cpufreq module, and does not check the CPU hardware registers.
- scaling\_available\_governors will tell the user which governors are currently available for the user.
- scaling_driver will display the variable frequency driver used by the CPU.
- Scaling_governor will display the current management strategy, and other types of echo will change accordingly.
- scaling_setspeed: You need to switch the governor type to userspace to appear. Echoing the value of this file will switch the main frequency.
Based on this, it is possible to increase the operating frequency of the CPU during application installation so that the CPU can run at an appropriate frequency.
2.4.2 Increasing the operating frequency of the CPU
In addition to judging whether the installation speed has increased directly according to the installation speed, you can also judge whether the frequency increase is effective according to the following log:
adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
adb shell cat /sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_cur_freq
You can directly see the frequency change:
The installation speed of WeChat can be increased from 20s when the CPU frequency is low, to about 10s when the CPU frequency is high.
Note: frequency is not the higher the better , the higher the frequency, the higher the power consumption of the mobile phone, and it is easy to heat up.
Four, after installation
After the application is installed, you will encounter various problems, startup failure, compliance rectification (this is actually done during application development), then which problems may be encountered, and can be solved with the help of the apk installation process of
4.1 targetsdkversion=30 what you have to do
In the "Meeting Google Play Target API Level Requirements" , according to the official requirements of Google, privacy permissions need to be controlled and partitioned storage is enforced. This requires that each application must not store resources on the SD card. So the question is, what if your project uses Glide, and the Glide storage path was previously set under the sd card, but now you can’t use the external storage directory, what should you do?
After understanding the apk installation process, knowing that the application data will be stored under data/data/packagename, this provides an internal folder for Glide's resource storage. The only thing to do is to prevent data/data from occupying If it is too large, set the upper limit of the Glide storage directory.
4.2 libmmkv.so cannot be found to solve the problem
4.2.1 Phenomenon
If your application is connected to Tencent's mmkv, you may encounter such a problem:
java.lang.UnsatisfiedLinkError dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/packagename-Sxe4_uU3WXx-ckI5DyG3UA==/base.apk"],nativeLibraryDirectories=[/data/app/packagename-Sxe4_uU3WXx-ckI5DyG3UA==/lib/arm, /system/fake-libs, /data/app/packagename-Sxe4_uU3WXx-ckI5DyG3UA==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib, /system/vendor/lib/hw]]] couldn't find "libmmkv.so"
Runtime.java 1011
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/packagename-Sxe4_uU3WXx-ckI5DyG3UA==/base.apk"],nativeLibraryDirectories=[/data/app/packagename-Sxe4_uU3WXx-ckI5DyG3UA==/lib/arm, /system/fake-libs, /data/app/packagename-Sxe4_uU3WXx-ckI5DyG3UA==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib, /system/vendor/lib/hw]]] couldn't find "libmmkv.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1011)
at java.lang.System.loadLibrary(System.java:1657)
at com.tencent.mmkv.MMKV.a(SourceFile:3)
This indicates that an exception occurs when the application loads libmmkv.so.
4.2.2 Analysis of the reasons
What is the cause of the problem: According to the log, it can be confirmed that the libmmkv.so file under the application data/app/ folder cannot be found.
4.2.3 Solve the problem
As mentioned earlier, the data of the application will be stored under data/data. This path also contains the so library after the application is decompressed, so you can do one thing to solve the problem of libmmkv.so above and relink the resources under data/data. Go to data/app to realize resource sharing. Practice has proved that this scheme is completely feasible and effectively solves the problem that the so library cannot be found.
Five, questions and answers
(1) What are the benefits of understanding the APK installation process
A series of knowledge points are involved from apk initiation to installation, during installation, to the end of installation, changes in application status, CPU usage, and resource sharing. These knowledge points can be connected in series, which is helpful for improving personal knowledge system. . Of course, due to the limited space of the article, this article is just a guide to roughly explain what knowledge points exist in the installation process. PMS, file management, process pull-up, etc. security can be introduced in a separate chapter, and these aspects will be introduced later. content.
(2) Understand what problems the APK installation process can solve
Manufacturer applications pay more attention to problems encountered before and during installation, and third-party applications pay more attention to problems encountered after installation. Having mastered every link in the installation process, through the above analysis, we can know that it can quickly help locate the problem.
Author: vivo internet client team-Xu Jie
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。