Rohan Hasabe

Table of Contents

  1. Introduction to APKs
  2. APK Structure
  3. Compilation Process
  4. Android Runtime Evolution
  5. APK Analysis
  6. Size Optimization

Introduction

This guide provides a high-level overview of the Android APK structure, compilation process, and analysis techniques. It has been compiled from various sources, including official Android documentation and community resources.

What is an APK

An APK (Android Package) is the package file format used by the Android operating system for the distribution and installation of mobile applications. Think of it as a container that holds all the elements necessary for your Android app to function.

Key Characteristics

APK Structure

An APK file is essentially a ZIP archive that contains multiple files and directories, each serving specific purposes.

Core Components

1. META-INF/

2. lib/

3. res/

4. assets/

5. AndroidManifest.xml

6. classes.dex

7. resources.arsc

Compilation Process

From Source to APK

compilation process

1. Source Code Compilation

Source Files (.java, .kotlin)
        ↓
Java Bytecode (.class)
        ↓
DEX Files (.dex)

2. Resource Processing

Resource Files
        ↓
AAPT2 Processing
        ↓
Compiled Resources

3. Final Packaging

DEX Files + Compiled Resources + Native Libraries
        ↓
APK Packaging
        ↓
APK Signing
        ↓
ZIP Alignment

Android Runtime Evolution

Historical Progression

1. Dalvik Virtual Machine

2. Android Runtime (ART)

3. Modern ART (Android 7.0+)

Runtime Characteristics

runtime comparision

Dalvik vs. ART Comparison

Format vs Runtime Separation:

Platform Architecture:

APK Analysis

Using APK Analyzer

image

There are three ways to access the APK Analyzer when a project is open:

1. Basic Analysis

2. Advanced Analysis

3. Size Analysis

Size Optimization

Optimization Strategies

1. Code Optimization

2. Resource Optimization

3. Native Library Optimization

Real-World Example: Photo Filter App

Initial APK Structure:

After Optimization:

  1. Implemented resource shrinking
    • Removed unused sample images
    • Compressed remaining images
  2. Split native libraries by architecture
  3. Implemented dynamic feature delivery for filters

Final Result:

Real-World Example: Language Resource Organization (Not a real app, just for Understanding)

Common Structure Issue:
/res
  /drawable
    - app_icon.png (1MB - high res)
    - background.jpg (2MB)
  /values
    - strings.xml (English)
  /values-es
    - strings.xml (Spanish)
  /values-fr
    - strings.xml (French)
  /values-de
    - strings.xml (German)

Optimized Structure:

  1. Implemented drawable density splits
  2. Used language packs for non-primary languages
  3. Optimized image assets

Results:

After Optimization:

  1. Implemented resource shrinking
    • Removed unused sample images
    • Compressed remaining images
  2. Split native libraries by architecture
  3. Implemented dynamic feature delivery for filters

Final Result:

Best Practices for your app

Development Guidelines

1. Size Management

2. Performance Optimization

3. Security Considerations

Maintenance Guidelines

1. Regular Monitoring

Conclusion

Understanding APK structure, compilation, and analysis is crucial for Android development. This knowledge enables:

Citation

[1]: Android Developers. "APK Analyzer". Android Documentation. Retrieved January 26, 2025, from https://developer.android.com/tools/apkanalyzer
[2]: Android Developers. "Analyze your build with APK Analyzer". Android Documentation. Retrieved January 26, 2025, from https://developer.android.com/studio/debug/apk-analyzer
[3]: Android Developers. "App resources overview". Android Documentation. Retrieved January 26, 2025, from https://developer.android.com/guide/topics/resources/providing-resources
[4]: Android Developers. "Application Fundamentals". Android Documentation. Retrieved January 26, 2025, from https://developer.android.com/guide/components/fundamentals
[5]: Android Developers. "App Manifest Overview". Android Documentation. Retrieved January 26, 2025, from https://developer.android.com/guide/topics/manifest/manifest-intro
[6]: Android Developers. "AAPT2". Android Documentation. Retrieved January 26, 2025, from https://developer.android.com/tools/aapt2