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
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
LocationManagerto 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).
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).
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_Phoneand save.
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:
| 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.
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' }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 intoapp-debug.apk.app/build/outputs/apk/debug/Install via ADB adb install app-debug.apk→ App appears on emulator./data/app/(device storage)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.
- If the app crashes during payment, check Logcat for errors:
ADB: The Swiss Army Knife for Developers
ADB (Android Debug Bridge) lets you control devices/emulators from the command line. Key commands:
| 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:
- Use ADB to force close the app on an emulator running API 23:
adb shell am force-stop com.ntc.billpay - Reinstall the debug APK:
adb install -r app-debug.apk - Check logs for crashes:
adb logcat | grep "NTC" - Fix: Optimize the app’s
Bitmaphandling (NTC’s app loads high-res images withoutinJustDecodeBounds).
Exam Tip: What to Expect
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).
Toolchain Questions:
- Practical: "How would you debug a crash in Pathao’s app?" → Use Logcat + ADB.
- Config: "Where do you add Firebase dependencies?" →
build.gradleunderdependencies. - Build process: Trace the steps from
.ktto.apk(compile → dex → assemble).
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:
- Use Profiler to check CPU/memory usage.
- Run
adb shell dumpsys meminfo com.darazto check memory leaks. - Enable Lint to find inefficient code (e.g., unused
Bitmapobjects).
- Answer:
- Example: "You’re developing a Daraz order-tracking app. The app is slow on API 21. What tools would you use to diagnose this?"
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
- Memorize 5 key commands (
In the Real World
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 AuthandStripe SDKto ensure secure transactions across Android versions.
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:
They also use Firebase Test Lab (cloud-based emulators) to catch crashes on rare devices.adb shell am broadcast -a android.location.PROVIDERS_CHANGED
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…