Например, так:
В build.gradle
описать зависимость, как
debugImplementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
Создать файл src/debug/java/com/example/logging/LoggingInterceptor
:
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import static okhttp3.logging.HttpLoggingInterceptor.Level.BODY;
public class LoggingInterceptor implements Interceptor {
private final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor().setLevel(BODY);
@Override
public Response intercept(Chain chain) throws IOException {
return interceptor.intercept(chain);
}
}
Создать файл src/release/java/com/example/logging/LoggingInterceptor
:
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;
public class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
return chain.proceed(chain.request());
}
}
И использовать этот интерсептор в проекте:
.client(new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor()).build())// Logging
Тогда, при сборке assembleDebug
будет использоваться интерсептор с логгером, а при сборке assembleRelease
будет использоваться пустой интерсептор, а библиотека с логгером не попадет в classpath.