main

square/leakcanary

Last updated at: 29/12/2023 09:38

GoodAndroidVersionInterceptor.kt

TLDR

This file contains the implementation of the GoodAndroidVersionInterceptor class, which is used as an interceptor for heap analysis. It checks if the Android version is suitable for analysis and cancels the analysis job if not.

Classes

GoodAndroidVersionInterceptor

This class implements the HeapAnalysisInterceptor interface and provides the logic to check if the current Android version is supported for heap analysis. It contains a lazy-initialized errorMessage property that determines if the Android version is supported based on its SDK_INT value. If the version is not supported, the errorMessage property will be set with a corresponding error message. In the intercept method, if the errorMessage is not null, the analysis job will be cancelled with the error message; otherwise, the analysis job will be proceeded.

package leakcanary

import android.os.Build
import leakcanary.HeapAnalysisInterceptor.Chain

class GoodAndroidVersionInterceptor : HeapAnalysisInterceptor {
  private val errorMessage: String? by lazy {
    val sdkInt = Build.VERSION.SDK_INT
    if (// findObjectById() sometimes failing. See #1759
      sdkInt != 23 &&
      // findObjectById() sometimes failing. See #1759
      sdkInt != 25 &&
      // Android 11 seem to sometimes have super slow heap dumps.
      // See https://issuetracker.google.com/issues/168634429
      sdkInt < 30
    ) {
      null
    } else {
      "Build.VERSION.SDK_INT $sdkInt not supported"
    }
  }

  override fun intercept(chain: Chain): HeapAnalysisJob.Result {
    errorMessage?.let {
      chain.job.cancel(it)
    }
    return chain.proceed()
  }
}