您当前的位置: 首页 >  kotlin

蓝不蓝编程

暂无认证

  • 5浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

集成Facebook Stetho,区分Debug和Release版本(Kotlin)

蓝不蓝编程 发布时间:2018-10-11 18:27:28 ,浏览量:5

背景:

Stetho是Facebook出品的基于Chrome浏览器的安卓调测工具,可以监控网络交互,方便修改数据库和SP文件等。但是这种方式可能因网络原因导致无法打开chrome的调测界面,此种情况下可以参考《打印完整的okhttp网络请求和响应消息》

问题描述:

集成Stetho有一个问题,未区分debug和release提供版本,如果集成进来,就会导致debug和release版本都带有此功能。如果要release版本不带有,则需要在编译release版本时,删除掉集成的代码。这个事情非常伤脑筋。

解决方案: 方式一
  1. 添加release依赖(net.igenius:stetho-no-op:1.1)
    debugImplementation 'com.facebook.stetho:stetho:1.5.1'
    debugImplementation 'com.facebook.stetho:stetho-okhttp3:1.5.1'
    releaseImplementation "net.igenius:stetho-no-op:1.1"
  1. Application启动stetho
if (BuildConfig.DEBUG) {
            Stetho.initialize(
                    newInitializerBuilder(this)
                            .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                            .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
                            .build())
        }
  1. 设置okhttp过滤器
if (BuildConfig.DEBUG) {
    addNetworkInterceptor(StethoInterceptor())
}
  1. 添加混淆配置文件:
-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.**{*;}

# okhttp
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

# okio
-keep class sun.misc.Unsafe { *; }
-dontwarn java.nio.file.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-keep class okio.**{*;}
-dontwarn okio.**
方式二

将对Stetho的依赖放到debug编译方式特有的代码中。这个可以通过为debug和release版本指定不同的代码目录来实现。

  • build.gradle文件增加如下配置:
  1. android节点下增加:
    sourceSets {
        debug {
            java.srcDirs = ['src/main/debug/java']
        }
        release {
            java.srcDirs = ['src/main/release/java']
        }
    }
  1. dependencies节点下增加:
debugImplementation 'com.facebook.stetho:stetho:1.5.1'
debugImplementation 'com.facebook.stetho:stetho-okhttp3:1.5.1'
  • 在src/main目录下增加给debug和release版本分别提供的代码文件,目录结构样例如下: image.png
  • 在debug和release相同目录下放置相同的类,一个保持正常的实现,一个保持空实现。 debug相应目录下防止的ThirdPartyUtil.java类:
import android.content.Context
import com.facebook.stetho.okhttp3.StethoInterceptor
import okhttp3.OkHttpClient

object ThirdPartyUtil {
    /**
     * 初始化Stetho,为okhttp设置拦截。下方代码仅为示例,具体拦截设置请自行查找。
     */
    fun initStetho(context: Context, okHttpBuilder: OkHttpClient.Builder) {
        com.facebook.stetho.Stetho.initializeWithDefaults(context)
        okHttpBuilder.addNetworkInterceptor(StethoInterceptor())
    }
}

注意是addNetworkInterceptor而不是addInterceptor. release相应目录下防止的ThirdPartyUtil.java类(实现为空):

import android.app.Application
import okhttp3.OkHttpClient

object ThirdPartyUtil {
    /**
     * 保持为空实现,不依赖Stetho。
     */
    fun initStetho(application: Application, okHttpBuilder: OkHttpClient.Builder) {}
}
  • Application代码中调用stetho初始化方法: ThirdPartyUtil.initStetho(this, okHttpBuilder);
  • 同上添加混淆配置文件
源代码:

https://gitee.com/cxyzy1/StethoReleaseApplication.git

附录:

参考:https://www.jianshu.com/p/38d8324b126a

安卓开发技术分享: https://blog.csdn.net/yinxing2008/article/details/84555061

关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0625s