Mobile Development 12 min read

Mastering Android APK Installation: From Pre‑install to Post‑install Optimization

This article breaks down the Android APK installation lifecycle—pre‑install, install, and post‑install—explaining each stage, offering command‑line methods, code examples, performance tweaks, and troubleshooting tips to help developers understand and optimize the whole process.

vivo Internet Technology
vivo Internet Technology
vivo Internet Technology
Mastering Android APK Installation: From Pre‑install to Post‑install Optimization

1. What Can Be Done in the Installation Process

The APK installation is divided into three stages—pre‑install, installing, and post‑install—each offering opportunities for optimization and problem solving.

2. Pre‑install

Choose an installation method that matches the app’s signing status.

2.1 Silent install with pm command (system‑signed apps)

String cmd = "pm install -r -d /data/data/android.apk";
Runtime run = Runtime.getRuntime();
Process process = run.exec(cmd);

2.2 Package installer (non‑system‑signed apps)

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 API (Android 8.0+ where pm silent install is blocked)

From Android 8.0 onward, pm cannot perform silent installs, so the Session API is used as a compatible workaround.

int sessionId = packageInstaller.createSession(params);
InstallLog.d(TAG, "doPackageStage create sessionId is : " + sessionId);
byte[] buffer = new byte[65536];
session = packageInstaller.openSession(sessionId);
InputStream in = new FileInputStream(file);
long sizeBytes = file.length();
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 code extracts the necessary steps from PackageManager to achieve silent installation.

3. Installing

3.1 APK Structure

An APK is a ZIP archive containing dex files, resources.arsc, AndroidManifest.xml, the res directory, META-INF, and native libraries under lib.

3.2 Storage Directories Involved

system/app – built‑in system apps, cannot be removed.

data/app – user‑installed apps; APK files are copied here.

data/data – per‑app data such as SharedPreferences and caches.

data/dalvik-cache – optimized dex files, typically ~¼ of the original APK size.

3.3 Main Installation Flow

The process copies the APK to /data/app, extracts it, registers resources, parses AndroidManifest.xml, creates the app’s data directory under /data/data, optimizes dex files into /data/dalvik-cache, registers components and permissions with PackageManagerService, and finally broadcasts the install completion.

3.4 Optimization Opportunities During Install

Manufacturers can reduce install time by raising CPU frequency or using differential (delta) updates. Tests show CPU frequency is the dominant factor; high‑performance cores are under‑utilized while low‑power cores handle most work.

3.4.1 CPU Frequency Details

Relevant sysfs files include: cpuinfo_max_freq – maximum supported frequency. cpuinfo_min_freq – minimum supported frequency. cpuinfo_cur_freq – current frequency read from hardware registers. scaling_cur_freq – frequency reported by the cpufreq driver. scaling_available_governors – list of available governors. scaling_setspeed – writable when the governor is set to userspace to force a specific frequency.

Raising the CPU frequency during install can roughly halve the duration (e.g., from ~20 s to ~10 s on a typical 8‑core device).

Measuring Frequency Changes

adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
adb shell cat /sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_cur_freq

Higher frequencies increase power consumption and heat.

4. Post‑install

4.1 Dealing with targetSdkVersion 30 Storage Restrictions

Google Play requires scoped storage, prohibiting arbitrary writes to external SD cards. Libraries that previously stored files on external storage (e.g., Glide) should switch to an internal directory under /data/data/your.package.name and optionally enforce a size limit to avoid excessive storage use.

4.2 Resolving libmmkv.so Not Found Errors

Problem : The native library cannot be loaded because it is missing from the data/app native library directory.

Analysis : The loader searches data/app/.../lib but the library resides only under data/data after extraction.

Solution : Create a symbolic link or copy from data/data/your.package.name/lib to data/app/your.package.name/lib so that the runtime can locate libmmkv.so. This approach has been verified to resolve the loading failure.

5. Q&A

Why understand the APK installation flow? It connects concepts such as process states, CPU usage, and resource sharing, providing a solid foundation for Android development.

What problems can this knowledge solve? It enables rapid diagnosis of pre‑install, install‑time, and post‑install issues, helping both OEM‑level and third‑party apps pinpoint and fix failures.

mobile developmentPerformance optimizationAndroidAPKInstallationPackage InstallerSession API
vivo Internet Technology
Written by

vivo Internet Technology

Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.