Skip to main content

“SDK not initialized” Error

Problem: Loading an ad before the SDK has been initialized, or loading ads before initialization has completed. Solution 1 — Implement the listener and wait:
val initRequest = VelocityAdsInitRequest.Builder("YOUR_APPLICATION_KEY").build()
VelocityAds.initSDK(applicationContext, initRequest, object : VelocityAdsInitListener {
    override fun onInitSuccess() {
        // SDK is ready — enable ad loading in your UI or trigger first ad load
        enableAdLoading()
    }

    override fun onInitFailure(error: VelocityAdsError) {
        Log.e("SDK", "SDK init failed: $error")
    }
})
Solution 2 — Check before loading:
if (VelocityAds.isInitialized()) {
    val nativeAd = VelocityNativeAd(VelocityNativeAdRequest.Builder().withPrompt("...").build())
    nativeAd.loadAd(listener)
} else {
    // SDK not ready — init was not called, failed, or hasn't completed yet
}

Init Failure Handling (Connectivity + Retry)

Initialization can fail due to transient network conditions. A resilient integration should retry with exponential backoff:
class SDKInitCoordinator(private val context: Context) : VelocityAdsInitListener {
    private val handler = Handler(Looper.getMainLooper())
    private var retryAttempt = 0
    private val maxRetries = 3
    private val baseDelayMs = 1_000L
    private val maxDelayMs = 30_000L

    fun start() { initSDK() }

    private fun initSDK() {
        val request = VelocityAdsInitRequest.Builder("YOUR_APPLICATION_KEY").build()
        VelocityAds.initSDK(context, request, this)
    }

    override fun onInitSuccess() { retryAttempt = 0 }

    override fun onInitFailure(error: VelocityAdsError) {
        if (retryAttempt >= maxRetries) return
        if (!isOnline()) return

        val exponential = min(maxDelayMs, baseDelayMs * 2.0.pow(retryAttempt).toLong())
        val delay = exponential + Random.nextLong(0, 500)
        retryAttempt++
        handler.postDelayed({ initSDK() }, delay)
    }

    private fun isOnline(): Boolean {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val caps = cm.getNetworkCapabilities(cm.activeNetwork ?: return false) ?: return false
        return caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    }
}

Ads Not Loading

Checklist:
  • ✅ SDK initialized in Application class
  • ✅ Internet permission added to manifest
  • ✅ Network connectivity available
  • ✅ Publisher key is correct
  • ✅ Debug mode enabled to see logs
Enable Debug Mode:
val initRequest = VelocityAdsInitRequest.Builder("YOUR_APPLICATION_KEY")
    .withDebug(true)
    .build()
VelocityAds.initSDK(applicationContext, initRequest, listener)

Memory Leaks

Problem: Using Activity context instead of Application context.
// ✅ Correct — always pass applicationContext
VelocityAds.initSDK(applicationContext, initRequest, listener)

// ❌ Wrong — never pass Activity context to initSDK
VelocityAds.initSDK(this, initRequest, listener)

No Ads Returned

Possible causes:
  • No ads available for the given context
  • Network issues
  • Invalid publisher key
  • Ad unit not configured
Solution: Check logs with debug mode enabled. The SDK will continue to function even if no ads are available.