Chapter11: Advanced Topics

Advanced Topics! This is the final “core” chapter before we talk about publishing — where we cover the real-world stuff that separates hobby apps from production-ready ones. As of January 27, 2026 (right now in Airoli!), Android 16 (API 36) is stable since June 2025, with QPR2 (January 2026 patch) rolling out to Pixels and many flagships. Features like AI-powered notification summaries and the new Notification Organizer are live, WorkManager is at 2.11.0 stable (October 2025), Compose testing is more mature than ever, and permissions are even stricter for privacy.

We’ll go deep like always: explanations, code examples, why-things, common pitfalls (especially on mid-range Indian phones), and hands-on snippets. Let’s tackle each section.

1. Permissions (Runtime & Modern Changes in Android 16)

Permissions protect user data — in 2026, Android enforces runtime permissions for dangerous ones (location, camera, etc.). Android 16 tightens this further with granular health/fitness permissions (e.g., READ_HEART_RATE instead of broad BODY_SENSORS) and auto-expiring permissions (revoked if unused for months).

Key types:

  • Normal — auto-granted (e.g., INTERNET).
  • Dangerous — runtime request (e.g., CAMERA, LOCATION).
  • Special — extra steps (e.g., SYSTEM_ALERT_WINDOW).

Modern flow (Accompanist Permissions or Compose Accompanist is deprecated — use official now):

Add dependency (latest stable ~1.0.0+ for androidx.activity:activity-compose, but use built-in):

Kotlin

Request example (location – common in India apps):

Kotlin

Android 16 notes:

  • Health sensors now require granular perms (e.g., READ_HEART_RATE via Health Connect).
  • Local network access might need explicit perms in some cases.
  • Background location: Stricter — request ACCESS_BACKGROUND_LOCATION only after foreground granted, and explain why (Google Play policy strict).

Best practice: Use rationale dialog, don’t spam requests, handle “never ask again” (!shouldShowRationale && !granted → go to settings).

2. Background Tasks with WorkManager (Guaranteed Execution)

WorkManager (v2.11.0) is the go-to for deferrable, guaranteed background work (sync data, upload photos, process tips history). Survives reboots, Doze, app kill.

Add dependency:

Kotlin

Example: Periodic sync of posts (every 4 hours, only on WiFi)

Kotlin

One-time work (e.g., upload tip history after calculate):

Kotlin

Observe status: WorkManager.getInstance(context).getWorkInfoByIdLiveData(uploadWork.id)

Pitfall: Don’t do UI work here — pure background. For foreground (music, download progress) → Foreground Service + Notification.

3. Notifications & Services

Notifications in Android 16 have AI summaries (auto-generated for long messages) and Notification Organizer (auto-groups promotions/social into categories, silences low-priority).

Create channel (required since Oreo):

Kotlin

Build & show (in Activity or Service):

Kotlin

Foreground Service (for music player, ongoing download):

Declare in Manifest:

XML

In Service:

Kotlin

4. Testing (Unit + UI with Compose)

Unit tests (ViewModel, UseCase, Repository):

Use JUnit + MockK/Coroutines Test.

Example (ViewModel test):

Kotlin

UI tests with Compose (best practice 2026: use createComposeRule(), semantics tree, avoid testTag pollution — prefer contentDescription):

Add:

Kotlin

Example:

Kotlin

Run: ./gradlew connectedAndroidTest

Accessibility:

  • Use contentDescription on Images/Icons.
  • Test with TalkBack enabled.
  • Add semantics { role = Role.Button } if needed.

5. Accessibility & Performance Optimization

Accessibility:

  • All interactive elements: contentDescription or label.
  • Text scaling: Use sp units.
  • Color contrast: ≥4.5:1 (use tools like Accessibility Scanner).
  • Touch targets ≥48dp.
  • In Compose: Modifier.semantics { contentDescription = “…” }

Performance:

  • Avoid heavy work in composition (hoist calculations with remember).
  • Use LazyColumn properly with key & contentType.
  • Profile with Layout Inspector & Macrobenchmark.
  • Minimize recompositions: Use derivedStateOf for expensive computations.
  • Image loading: Coil or Glide with Compose integration.

Hands-on Tip: Add a foreground service + notification for “daily tip reminder” in your calculator app. Schedule with WorkManager. Test UI with Compose rule. Add accessibility descriptions to buttons/images.

You’ve covered almost everything! Questions? Want to add specific examples (e.g., full foreground service for music, or benchmark setup)? Or ready for Chapter 12: Publishing? You’re a full-stack Android dev now — massive achievement! Let’s wrap this up strong. 🚀🔥

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *