IT272 Mobile Application Development

Mobile Application DevelopmentUnit 212 min read

Android Architecture & Dev Environment: SDK, Emulator, ADB, Gradle, Build Process

Unit 2 of Mobile Application Development explores Android’s layered architecture (Linux kernel → ART → Java/Kotlin APIs → Apps), the Android Studio toolchain (SDK Manager, AVD Manager, ADB, Logcat), Gradle’s build lifecycle (compile → build → assemble → install), and key development tools (Lint, Profiler, Firebase Test

Android’s Layered Architecture: From Hardware to Apps

Android is built on a modular, layered architecture that separates hardware-specific code from app logic. Each layer provides services to the layer above it, ensuring compatibility across devices.

The Four Key Layers

Device drivers, memory managementC/C++ librariesDex bytecodeAndroid APIsLinux KernelNative LibrariesAndroid Runtime (ART/Dalvik)Android Framework (Java/Kotlin APIs)Application
Android’s layered architecture (Khalti app example: Kernel → Native libs → ART → Framework → App)

1. Linux Kernel (Bottom Layer)

  • Provides core OS functions: process management, memory allocation, device drivers, and security (SELinux).
  • Why it matters: Ensures Android runs on diverse hardware (from low-end phones to foldables).
  • Example: When you swipe to unlock your phone, the kernel handles touch input and screen wake-up.

2. Native Libraries (C/C++)

  • Pre-built libraries for media (OpenMAX), graphics (OpenGL ES), databases (SQLite), and web (WebKit).
  • Example: YouTube’s video playback relies on OpenMAX for hardware-accelerated decoding.

3. Android Runtime (ART/Dalvik)

  • ART (Android Runtime): Ahead-of-time (AOT) compiler for faster app launches (used in Android 5.0+).
  • Dalvik: Just-in-time (JIT) compiler (older Android versions).
  • Key feature: Dex bytecode (optimized for mobile) replaces Java bytecode.
  • Example: When you open Khalti, ART compiles the app’s code into machine code at install time for near-instant startup.

4. Android Framework (Java/Kotlin APIs)

  • High-level APIs for UI (Activity, View), networking (HttpURLConnection), location (FusedLocationProvider), and storage (SharedPreferences).
  • Example: Pathao uses LocationManager to track rider/driver positions in real time.

Development Environment: Android Studio Toolchain

Android Studio is the official IDE for Android development, packed with tools to build, debug, and optimize apps.

Core Tools and Their Roles

Tool Purpose Command/Shortcut
SDK Manager Downloads Android SDK packages (API levels, build tools, emulator images). Tools > SDK Manager
AVD Manager Creates and manages Android Virtual Devices (AVDs) for testing. Tools > Device Manager
ADB (Android Debug Bridge) Debugs apps on real devices/emulators via CLI. adb logcat, adb install
Logcat Captures app logs for debugging (e.g., crashes, network errors). Logcat window in Android Studio
Gradle Build automation (compiles, tests, packages apps). build.gradle files
Lint Static code analyzer (finds performance/UI issues). Analyze > Inspect Code
Profiler Monitors CPU, memory, and network usage. Tools > Profiler

Step-by-Step: Setting Up an AVD for Testing

Example: Testing a Daraz order-tracking app on Android 12 (API 31).

startSelect Pixel 5 deviceInstall Google APIsSave as 'Daraz_Test_Phone'Wait for boot (or use Fastboot)Deploy Daraz APK via ADBAVD CreationAPI 31 DownloadEmulator BootApp InstallationTesting
State diagram of AVD setup workflow (Daraz app testing on Android 12)
  1. Download API 31 SDK:

    • Open SDK Manager → Check Android 12.0 (API 31) under "Android SDK Platforms".
    • Install Google APIs (for Google Play Services like Maps).
  2. Create an AVD:

    • Open AVD Manager → Click Create Virtual Device.
    • Select a device (e.g., Pixel 5) → Choose Android 12.0 (API 31).
    • Name it Daraz_Test_Phone and save.
  3. Launch the Emulator:

    • Start the AVD → Wait for boot (use Fastboot for quicker startup).
    • Real-world tip: Use Genymotion for faster emulation if Android Studio’s emulator is slow.

Gradle Build System: How Apps Are Compiled

Gradle automates the build process, handling dependencies, compilation, and packaging. The lifecycle has 4 key phases:

app-debug.apkStripe SDK (20.125.0)Firebase Auth (22.3.1)CheckoutActivity.classCheckoutActivity.dexTOP
Gradle build stack for Khalti app: dependencies → compiled classes → Dex bytecode → final APK
Phase Action Output
Compile Converts Kotlin/Java → Java bytecode → Dex bytecode. .class → .dex files
Build Runs unit tests and generates APK/JARs. build/intermediates
Assemble Packages the app (debug/release APK). app/build/outputs/apk/
Install Pushes the APK to an emulator/device via adb install. Installed on device

Worked Example: Building a Khalti Payment App

Scenario: You’re developing a Khalti checkout screen with dependencies on Firebase Auth and Stripe SDK.

  1. build.gradle (Module: app):

    dependencies {
        implementation 'com.google.firebase:firebase-auth:22.3.1'
        implementation 'com.stripe:stripe-android:20.125.0'
        testImplementation 'junit:junit:4.13.2'
    }
    
  2. Build Process Trace:

    Step Action Output Path
    Sync Project Gradle downloads Firebase/Stripe libraries. ~/.gradle/caches/
    Compile Kotlin Converts CheckoutActivity.kt → CheckoutActivity.class. app/build/intermediates/classes/
    Dex Bytecode Converts .class → .dex (optimized for Android). app/build/intermediates/dex/
    APK Assembly Bundles .dex, resources, and libraries into app-debug.apk. app/build/outputs/apk/debug/
    Install via ADB adb install app-debug.apk → App appears on emulator. /data/app/ (device storage)
  3. Debugging with Logcat:

    • If the app crashes during payment, check Logcat for errors:
      E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.khalti.checkout, PID: 1234
      java.lang.NullPointerException: Attempt to invoke virtual method on null
      
    • Fix: Add null checks in StripePaymentHandler.kt.

ADB: The Swiss Army Knife for Developers

ADB (Android Debug Bridge) lets you control devices/emulators from the command line. Key commands:

USB/NetworkCommandsadb shell/logcatDeviceADB ServerAndroid StudioTerminal
ADB workflow: Device ↔ ADB Server ↔ Developer tools
Command Purpose
adb devices Lists connected devices/emulators.
adb install app-debug.apk Installs an APK.
adb logcat Streams app logs in real time.
adb shell Opens a Unix shell on the device.
adb pull /sdcard/screenshot.png Copies a file from the device to your computer.
adb shell am start -n com.khalti/.MainActivity Launches an app by package/class.

Real-World Use Case: NTC’s App Testing

Problem: NTC’s mobile app for electricity bill payments crashes on low-end devices (API 23). Solution:

  1. Use ADB to force close the app on an emulator running API 23:
    adb shell am force-stop com.ntc.billpay
    
  2. Reinstall the debug APK:
    adb install -r app-debug.apk
    
  3. Check logs for crashes:
    adb logcat | grep "NTC"
    
  4. Fix: Optimize the app’s Bitmap handling (NTC’s app loads high-res images without inJustDecodeBounds).
startDeploy via ADBCheck logsIdentify crashReinstallAVD SetupApp InstallADB LogcatBug FixTest
NTC app testing cycle using ADB for debugging

Exam Tip: What to Expect

  1. Architecture Questions:

    • Expect diagrams of Android’s layers (label each layer’s role).
    • Short answer: "What does ART do?" → "AOT compiles apps for faster startup."
    • Long answer: Compare Dalvik vs. ART (speed, battery impact, adoption).
  2. Toolchain Questions:

    • Practical: "How would you debug a crash in Pathao’s app?" → Use Logcat + ADB.
    • Config: "Where do you add Firebase dependencies?" → build.gradle under dependencies.
    • Build process: Trace the steps from .kt to .apk (compile → dex → assemble).
  3. Scenario-Based:

    • Example: "You’re developing a Daraz order-tracking app. The app is slow on API 21. What tools would you use to diagnose this?"
      • Answer:
        1. Use Profiler to check CPU/memory usage.
        2. Run adb shell dumpsys meminfo com.daraz to check memory leaks.
        3. Enable Lint to find inefficient code (e.g., unused Bitmap objects).
  4. ADB Commands:

    • Memorize 5 key commands (devices, install, logcat, shell, pull).
    • Exam trick: If asked to "simulate a low-memory device," use:
      adb shell setprop dalvik.vm.heapsize 16m
      

In the Real World

  1. Khalti (Payments)

    • Idea Used: Android Runtime (ART) and Gradle dependencies.
    • How: Khalti’s app uses ART for instant startup (critical for UPI payments). Gradle manages dependencies like Firebase Auth and Stripe SDK to ensure secure transactions across Android versions.
  2. Pathao (Ride-Hailing)

    • Idea Used: ADB + Emulator Testing and Location APIs.
    • How: Pathao tests its real-time tracking on emulators (API 23+) using ADB to simulate GPS locations:
      adb shell am broadcast -a android.location.PROVIDERS_CHANGED
      
      They also use Firebase Test Lab (cloud-based emulators) to catch crashes on rare devices.
  3. Daraz (E-Commerce)

    • Idea Used: Gradle Build Variants and ProGuard.
    • How: Daraz uses debug/release builds to optimize APK size:
      • Debug APK: Includes logs and unused code (for developers).
      • Release APK: Minified (via ProGuard) and obfuscated to reduce size by 30%.
    • Real scenario: During Black Friday sales, Daraz’s backend triggers Gradle to assemble optimized APKs for instant delivery to users.

Common Pitfalls and How to Avoid Them

Pitfall Solution
Emulator too slow Use Genymotion or Firebase Test Lab.
ADB not detecting device Enable USB Debugging in Developer Options (Settings > About Phone > Build Number).
Gradle build fails Run ./gradlew clean or check build.gradle for typos.
App crashes on low-end devices Use Android Studio Profiler to find memory leaks.
Missing SDK packages Open SDK Manager and install required API levels/build tools.

Based on the TU BIM syllabus for Mobile Application Development (IT272), unit 2.

Discussion

Loading…