导入库仅用于调试,而不是用于发布

时间:2017-12-08 14:35:43

标签: android gradle android-gradle

我在我的应用程序中使用OkHttp并添加OkHttp interceptor来打印日志。如您所见,我只想导入OkHttp interceptor for debug apk而不是发布。

    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();
    }

我使用了debugImplementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'但是当我调用./gradlew assembleRelease时失败了因为import import okhttp3.logging.HttpLoggingInterceptor;

有没有办法做到这一点?提前致谢。

修改

问题Import library only for debugging对我不起作用。

1 个答案:

答案 0 :(得分:1)

在你的gradle替换中

compile 'library'

compileDebug 'library'

在Gradle 3.0中不确定,但认为它是implementationDebug / apiDebug

但是在发布时,代码将无法编译。

如果不冲突可能有效的解决方案是在&main; / java'中创建一个接口。这将委托你想要的东西而不是特定于库,然后创建一个文件夹' debug / java'并创建一个实现与库接口的文件,从代码中调用它,而这个调试将特定于flavor调试编译。这里要编译为没有错误的版本,你可以在' release / java'中指定一个具有相同名称的类。使用空实现(如果这不起作用,则必须使用Class#forName进行查找,它只能用于调试)