Stetho简化Android调试(二)

Stetho简化Android调试(一) 一文中讲述了如何使用Stetho结合Chrome远程调试Android App

Stetho给我们调试带来很大的便利,效率显著提升的同时也产生一个问题:如果release版本中依然使用Stetho就会造成应用程序数据的泄露。因此我们只需在调试阶段(debug)中使用。因此有了下面这段代码:

public class App extends Application { @Override public void onCreate() { super.onCreate(); if(BuildConfig.DEBUG){ // Debug模式下才初始化 Stetho.initializeWithDefaults(this); } } } 

是的,这样确实可以解决release版本中造成的应用程序数据泄露的问题。但是,对于 ‘只在调试阶段(debug)中使用’ 这个问题,依然没有很好的解决。Stetho相关的代码,jar包会被打包进我们最终的apk中,造成apk的体积变大。而这些完全是没有必要的。

当然,也有朋友会说:我发版的时候,把相关的代码删掉就行了。这样虽然可行,但是偶尔也会忘记,并且相对麻烦。下面我就给出两种方式来解决这一问题:

方法一:
  1. 修改Stetho的依赖方式为debugCompile
dependencies {   
    debugCompile 'com.facebook.stetho:stetho:1.3.1'
    debugCompile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
}

  1. 写一个接口StethoHelper
public interface StethoHelper { void init(Context context); OkHttpClient configureInterceptor(OkHttpClient httpClient); } 
  1. StethoHelper的实现类ReleaseStethoHelper
public class ReleaseStethoHelper implements StethoHelper { @Override public void init(Context context) { } @Override public OkHttpClient configureInterceptor(OkHttpClient httpClient) { return httpClient; } } 
  1. 新建一个debug文件夹,如下图:
 
debug folder
 
debug folder
 
debug folder
  1. StethoHelper的实现类DebugStethoHelper(位于新建的debug文件夹下)
public class DebugStethoHelper implements StethoHelper { @Override public void init(Context context) { Stetho.initializeWithDefaults(context); } @Override public OkHttpClient configureInterceptor(OkHttpClient httpClient) { return httpClient.newBuilder().addNetworkInterceptor(new StethoInterceptor()).build(); } } 
  1. 修改build.gradle文件
android {

    // ...
    
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField 'com.upd.stethosample.common.stetho.StethoHelper', 'STETHO', 'new com.upd.stethosample.common.stetho.ReleaseStethoHelper()' } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField 'com.upd.stethosample.common.stetho.StethoHelper', 'STETHO', 'new com.upd.stethosample.DebugStethoHelper()' } } } 
  1. 使用姿势
public class App extends Application { @Override public void onCreate() { super.onCreate(); BuildConfig.STETHO.init(this); } } 
方法二:
  1. 修改Stetho的依赖方式为debugCompile
dependencies {   
    debugCompile 'com.facebook.stetho:stetho:1.3.1'
    debugCompile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
}

  1. 利用反射机制编写StethoUtils
public class StethoUtils { public static void init(Context context) { try { Class<?> stethoClass = Class.forName("com.facebook.stetho.Stetho"); Method initializeWithDefaults = stethoClass.getMethod("initializeWithDefaults", Context.class); initializeWithDefaults.invoke(null, context); } catch (Exception e) { e.printStackTrace(); } } public static OkHttpClient configureInterceptor(OkHttpClient httpClient) { try { Class<?> aClass = Class.forName("com.facebook.stetho.okhttp3.StethoInterceptor"); return httpClient.newBuilder().addNetworkInterceptor((Interceptor) aClass.newInstance()).build(); } catch (Exception e) { e.printStackTrace(); } return httpClient; } } 
  1. 使用姿势
public class App extends Application { @Override public void onCreate() { super.onCreate(); if(BuildConfig.DEBUG) { StethoUtils.init(this); } } }


作者:WaitingAnd
链接:https://www.jianshu.com/p/b4571fa3b001
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

转载于:https://www.cnblogs.com/Im-Victor/p/9673528.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值