Building a Production-Ready Android SDK
API design, initialisation, dependency footprint, consumer R8 rules, binary compatibility and versioning — what separates a library from an SDK other teams can trust.
- #sdk
- #gradle
- #api design
- #android library
- #r8
Table of contents
An app can be refactored whenever you like. An SDK can't: the moment it ships, other teams build on its API, its dependencies and its behaviour. Designing for that constraint is most of the work.
Start with the public API
Keep the surface small and explicit. Everything public is a promise.
public object Shield {
@Volatile private var instance: ShieldClient? = null
public fun init(context: Context, config: ShieldConfig): ShieldClient =
instance ?: synchronized(this) {
instance ?: ShieldClient(context.applicationContext, config).also { instance = it }
}
}- Enable Kotlin's explicit API mode so nothing becomes public by accident.
- Prefer builders or config objects over long parameter lists — you can add fields later without breaking callers.
- Never leak internal types (OkHttp, Room) through public signatures.
kotlin {
explicitApi()
}Initialisation: explicit beats magic
Auto-initialising via a ContentProvider is convenient but surprises hosts with start-up cost. An explicit init() call makes the cost visible and lets the host decide when to pay it. If you do auto-initialise, use App Startup so hosts can disable it.
Watch your dependency footprint
Every transitive dependency you add is one your host must accept — including its version.
dependencies {
// implementation, not api: keep it off the host's compile classpath
implementation "com.squareup.okhttp3:okhttp:4.12.0"
}Ship consumer R8 rules
If your SDK uses reflection or JNI, the host's R8 will strip what it needs unless you ship keep rules with the library.
# Keep JNI entry points used by the native layer
-keepclasseswithmembernames class * {
native <methods>;
}Guard binary compatibility
Source compatibility isn't enough: hosts might compile against v1.2 and run with v1.3. Tools like the binary-compatibility-validator plugin dump your public API into a file checked into git, so any change shows up in code review.
Version with intent
- Patch: bug fixes, no API change.
- Minor: additive API only.
- Major: breaking changes, with a migration guide.
Checklist
- Explicit API mode on
- No internal types in public signatures
- Consumer R8 rules shipped
- API dump checked in CI
- Threading documented for every callback
- Sample app that integrates the SDK the way customers will