0

I am using OkHttp in my Application and add OkHttp interceptor to print log. As you see, I only want to import OkHttp interceptor for debug apk not for release.

    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i(TAG, message);
            }
        });
        logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        mOkHttpClient = new OkHttpClient.Builder().addInterceptor(logging).build();
    }else {
        mOkHttpClient = new OkHttpClient();
    }

I have used debugImplementation 'com.squareup.okhttp3:logging-interceptor:3.9.1' however when I invoke ./gradlew assembleRelease it failed because import import okhttp3.logging.HttpLoggingInterceptor;

Is there any way to do that? Thanks in advance.

Edit

The question Import library only for debugging does not work for me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
CoXier
  • 2,523
  • 8
  • 33
  • 60

1 Answers1

1

In your gradle replace

compile 'library'

for

compileDebug 'library'

In Gradle 3.0 not sure but think it is implementationDebug/apiDebug

But on release the code will not compile.

A solution that may work if it doesnt conflict is to create a interface in 'main/java' that will delegate what you want without being library specific, then create a folder 'debug/java' and create a File that implements the interface with the Library, call it from your code instead this debug will be specific for the flavor debug compile. Here to compile as release without errors you may specify a class with the same name in 'release/java' with a empty implementation (if this doesnt works you will have to lookup with Class#forName and it will only work on debug)

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167