Exclude class from kotlin compile path for release build type

不羁岁月 提交于 2019-12-11 16:21:53

问题


I am adding Stetho in my app. To prevent its dependency in release apk I have added it as a debug dependency only.

    debugApi 'com.facebook.stetho:stetho:1.5.1'
    debugApi 'com.facebook.stetho:stetho-okhttp:1.5.1'

I have a Initializer interface which configures Stetho.

interface IStethoInitializer {
    /**
     * Call on application create
     */
    fun initStetho(context: Context)

    fun getStethoNetworkInterceptor(): Interceptor?
}

And 2 implementations of the same: one for release other for debug.

import android.content.Context
import com.facebook.stetho.Stetho
import com.facebook.stetho.okhttp.StethoInterceptor
import com.squareup.okhttp.Interceptor

class DebugStethoInitializer : IStethoInitializer {
    override fun initStetho(context: Context) {
        Stetho.initializeWithDefaults(context)
    }

    override fun getStethoNetworkInterceptor(): Interceptor? = StethoInterceptor()
}
import android.content.Context
import android.support.annotation.Keep
import com.squareup.okhttp.Interceptor

class ReleaseStethoInitializer : IStethoInitializer {
    override fun initStetho(context: Context) = Unit // no-op

    override fun getStethoNetworkInterceptor(): Interceptor? = null
}

Now I am using BuildConfig field to pick up the desired implementation at compile time.

buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField 'com.arka.prava.stetho.IStethoInitializer', 'STETHO', 'new com.arka.prava.stetho.ReleaseStethoInitializer()'
        }

        debug {
            buildConfigField 'com.arka.prava.stetho.IStethoInitializer', 'STETHO', 'new com.arka.prava.stetho.DebugStethoInitializer()'
        }
    }

The debug variant builds fine but the release variant produces errors.

DebugStethoInitializer.kt: (4, 12): Unresolved reference: facebook
DebugStethoInitializer.kt: (13, 9): Unresolved reference: Stetho
DebugStethoInitializer.kt: (16, 64): Unresolved reference:StethoInterceptor

Well obviously since I have added a debug dependency only these are not resolved. So how can I exclude the DebugStethoInitializer.kt from compile path in release build type?

Thanks!

来源:https://stackoverflow.com/questions/56068365/exclude-class-from-kotlin-compile-path-for-release-build-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!