Creating an Android SDK in Single-Activity Style

You will not surprise anyone with a single activity approach when creating a final application for Android. But we went further and used No-Activity when developing the SDK. Now let's figure out why it was needed, the difficulties encountered and how they were solved.

Standard 3rd party SDKs in Android

How do external SDKs usually work in Android? Open Activitylibrary, performed some work, the result is returned if necessaryonActivityResult.

Standard SDK workflow.
Standard SDK workflow.

, SDK . SDK, :

Desired stack of app screens and SDK
SDK

, SDK . , , , MapFragment Google. , , .

SDK

  • SDK , - Activity SDK -.

  • , SDK. (: , ).

  • SDK Lock Screen. , Lock Activity.

No-Activity SDK

, , , (Activity) SDK . - SDK . .

No-Activity SDK on Fragments
No-Activity SDK

, . ?

No-Activity SDK

  • SDK , .. .

  • , SDK - childFragmentManager.

  • , .. .

No-ActivitySDK

  • , Single-Activity.

  • SDK , dagger - ( ).

  • SDK Activity .. requireActivity . SDK.

  • Activity onActivityResult, , , .

  • SDK, .. Activity .

3rd party SDK

SDK. . , dagger2 .

Dagger2 SDK

dagger Application. SDK , Application, , .

, .

internal object ComponentHolder {

    lateinit var appComponent: SdkAppComponent
        private set

    @Synchronized
    fun init(ctx: Context) {
        if (this::appComponent.isInitialized) return

        appComponent = DaggerSdkAppComponent
            .builder()
            .sdkAppModule(SdkAppModule(ctx))
            .build()
    }

}

, init, , SDK , . SDK. EntryPointFragment. SDK. SDK childFragmentManager.

EntryPointFragment ComponentHolder Dagger.

override fun onCreate(savedInstanceState: Bundle?) {
        ComponentHolder.init(requireActivity())
        ComponentHolder.appComponent.inject(this)
        super.onCreate(savedInstanceState)
    }

, ComponentHolder, SDK .

okhttp3 major 4.+. Kotlin, , , code() . SDK, 3, 4 SDK, .

, . 2 flavor:

    flavorDimensions("okhttpVersion")
    productFlavors {
        v3 {
            dimension = "okhttpVersion"
        }
        v4 {
            dimension = "okhttpVersion"
        }
    }
    
    dependencies {
        v3Api okhttp3.core
        v3Api okhttp3.logging

        v4Api okhttp4.core
        v4Api okhttp4.logging
		}

, flavor , code() code.

// Code in v3 folder
class ResponseWrapper(private val response: Response) {

    val code : Int
        get() = response.code()

}
// Code in v4 folder
class ResponseWrapper(private val response: Response) {

    val code : Int
        get() = response.code

}

.

: , :

defaultConfig {
	...
	missingDimensionStrategy 'okhttpVersion', 'v4'
}

In this case, you get rid of the build conflict. Otherwise, there simply won't be a version.

Conclusion

SDK development, when compared to a simple Android application, is much more difficult, but sometimes more interesting. Also, the requirements for the quality of the final product are higher - if something falls, it will not fall for you, but for your client, which is very bad.




All Articles