Skip to content

Latest commit

 

History

History
53 lines (36 loc) · 1.31 KB

File metadata and controls

53 lines (36 loc) · 1.31 KB

← Back to main index | ← Back to folder


42. Version Compatibility & Graceful Degradation

API Level Checks

Note

Check Build.VERSION.SDK_INT >= Build.VERSION_CODES.API_LEVEL. Use AndroidX helpers (ContextCompat). Provide fallbacks for older APIs.

Backward compatibility · API level guards · Fallback patterns

💻 Code Example
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    requestPermission(Manifest.permission.READ_MEDIA_IMAGES)  // API 33+
} else {
    requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE)  // Older fallback
}

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PERMISSION_GRANTED) {
    openCamera()  // Safe on all API levels
}

Feature Detection Over Version Checks

Tip

Don't assume all API N+ devices have feature X. Use PackageManager.hasSystemFeature() to verify feature exists.

Feature detection · More robust · Handles device variations

💻 Code Example
// ✅ GOOD: Check feature availability
if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    openCamera()
} else {
    showUnavailableDialog()  // Device lacks camera
}