Import library only for debugging

百般思念 提交于 2020-01-15 07:21:08

问题


I am using stetho lib for debugging my app.

Gradle:

debugCompile 'com.facebook.stetho:stetho:1.4.1'
debugCompile 'com.uphyca:stetho_realm:2.0.0'

Application class:

if (BuildConfig.DEBUG) {
    Stetho.initialize(..);
}

But if I need to create a release version I must comment every time:

import com.facebook.stetho.Stetho; 
import com.uphyca.stetho_realm.RealmInspectorModulesProvider;

How to show the compiler that these libs only for debugging? Can we comment on two lines without creating an additional class, using annotations or something like this?


回答1:


Just leave the unused imports as they are. Your approach of if (BuildConfig.DEBUG) is perfectly valid. And frankly the best way to implement it.

Unused imports have no impact on performance: reference. There might be a trivial increase in compile time, but no increase in runtime.

Import statements don't make it to byte code.

You will need to change Gradle:

debugCompile 'com.facebook.stetho:stetho:1.4.1'
debugCompile 'com.uphyca:stetho_realm:2.0.0'

to

compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.uphyca:stetho_realm:2.0.0'


来源:https://stackoverflow.com/questions/45076313/import-library-only-for-debugging

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