Android sdk

From G1Joshi/Agent-Skills · View interactive version

Android sdk is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Android SDK assistance covering Android architecture components, Activity/Fragment lifecycles, permissions, background work with WorkManager, and Gradle build optimizations. Use when building native Android applications, configuring ProGuard/R8, or interacting with platform SDK APIs.

How to use this skill

Skill file

Android SDK

The traditional Android development toolkit (Views, Activities, Fragments, XML) using Kotlin/Java. Essential for maintaining the vast ecosystem of pre-Compose applications.

When to Use

Quick Start

// build.gradle.kts: viewBinding = true

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.myButton.setOnClickListener {
            Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show()
        }
    }
}

Core Concepts

Activity & Fragment Lifecycles

Activities represent single screens with user interfaces; lifecycle callbacks manage resource acquisition and release to prevent memory leaks during configuration changes:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
        // Connect sensors, start location listener
    }

    override fun onStop() {
        super.onStop()
        // Release heavy sensor streams to preserve battery
    }
}

Dependency Injection with Hilt

Hilt standardizes Dagger dependency injection across Android components with predefined scopes tied to Android lifecycles:

@HiltAndroidApp
class MainApplication : Application()

@AndroidEntryPoint
class UserProfileFragment : Fragment() {
    @Inject lateinit var analyticsTracker: AnalyticsTracker
    private val viewModel: UserViewModel by viewModels()
}

Permissions Architecture & Runtime Requests

Modern Android requires fine-grained runtime permission requests before accessing sensitive hardware or user records:

val requestPermissionLauncher = registerForActivityResult(
    ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
    if (isGranted) {
        openCamera()
    } else {
        showPermissionRationaleDialog()
    }
}

// Request permission when user triggers action
requestPermissionLauncher.launch(Manifest.permission.CAMERA)

Common Patterns

Background Processing with WorkManager

Problem: Long-running background sync operations get terminated by modern Android battery optimization (Doze mode).

Solution: Schedule persistent background jobs with WorkManager and constraints.

class SyncWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx, params) {
    override suspend fun doWork(): Result {
        return try {
            repository.syncData()
            Result.success()
        } catch (e: Exception) {
            Result.retry()
        }
    }
}

// Enqueue with network constraint
val constraints = Constraints.Builder()
    .setRequiredNetworkType(NetworkType.CONNECTED)
    .setRequiresBatteryNotLow(true)
    .build()

val syncRequest = PeriodicWorkRequestBuilder<SyncWorker>(1, TimeUnit.HOURS)
    .setConstraints(constraints)
    .build()

WorkManager.getInstance(context).enqueueUniquePeriodicWork(
    "DataSync", ExistingPeriodicWorkPolicy.KEEP, syncRequest
)

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------------------------- | :-------------------------------------------------- | :------------------------------------------ |

| ANR (App Not Responding) | Blocking main thread for >5s. | Move work to Dispatchers.IO (Coroutines). |

| IllegalStateException: Fragment not attached | Accessing context after detachment. | Check isAdded or use MainScope safely. |

| Memory Leak | Holding Activity reference in Singleton/Background. | Use WeakReference or Application Context. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.