3

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?

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89
Delphian
  • 1,650
  • 3
  • 15
  • 31

1 Answers1

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'
Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89
  • Thank you, this is what I was looking for! – Delphian Jul 13 '17 at 11:17
  • Even if it doesn't create additional classes it still ships with release .apk – Farid Nov 24 '19 at 11:31
  • 1
    this way the non used imports still afect the apk size... if you dont care about it okay but if you target low end devices it makes a huge difference – Rafael Lima May 11 '20 at 14:56
  • This is not the right way to do it. code will still ship with the build. Also compile is not deprecated and should be used as implementation – Neo Wakeup Mar 21 '22 at 18:02