问题
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