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
的体积变大。而这些完全是没有必要的。
当然,也有朋友会说:我发版的时候,把相关的代码删掉就行了。这样虽然可行,但是偶尔也会忘记,并且相对麻烦。下面我就给出两种方式来解决这一问题:
方法一:
- 修改
Stetho
的依赖方式为debugCompile
dependencies {
debugCompile 'com.facebook.stetho:stetho:1.3.1'
debugCompile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
}
- 写一个接口
StethoHelper
public interface StethoHelper {
void init(Context context);
OkHttpClient configureInterceptor(OkHttpClient httpClient);
}
StethoHelper
的实现类ReleaseStethoHelper
public class ReleaseStethoHelper implements StethoHelper {
@Override
public void init(Context context) {
}
@Override
public OkHttpClient configureInterceptor(OkHttpClient httpClient) {
return httpClient;
}
}
- 新建一个
debug
文件夹,如下图:

debug folder

debug folder

debug folder
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();
}
}
- 修改
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()'
}
}
}
- 使用姿势
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
BuildConfig.STETHO.init(this);
}
}
方法二:
- 修改
Stetho
的依赖方式为debugCompile
dependencies {
debugCompile 'com.facebook.stetho:stetho:1.3.1'
debugCompile 'com.facebook.stetho:stetho-okhttp3:1.3.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;
}
}
- 使用姿势
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
if(BuildConfig.DEBUG) {
StethoUtils.init(this);
}
}
}
作者:WaitingAnd
链接:https://www.jianshu.com/p/b4571fa3b001
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库