我正在应用中添加Stetho。为了防止其在release
apk中的依赖性,我仅将其添加为调试依赖性。
debugApi 'com.facebook.stetho:stetho:1.5.1'
debugApi 'com.facebook.stetho:stetho-okhttp:1.5.1'
我有一个配置Stetho的Initializer接口。
interface IStethoInitializer {
/**
* Call on application create
*/
fun initStetho(context: Context)
fun getStethoNetworkInterceptor(): Interceptor?
}
和2种实现相同:一种用于发布,另一种用于调试。
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
}
现在我正在使用BuildConfig字段在编译时选择所需的实现。
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()'
}
}
debug变体构建良好,但release变体产生错误。
DebugStethoInitializer.kt: (4, 12): Unresolved reference: facebook
DebugStethoInitializer.kt: (13, 9): Unresolved reference: Stetho
DebugStethoInitializer.kt: (16, 64): Unresolved reference:StethoInterceptor
很显然,由于我添加了调试依赖项,因此只有这些问题无法解决。那么如何在发行版本类型中从编译路径中排除DebugStethoInitializer.kt
?
谢谢!