由于本人能力有限浪腐,文中若有錯誤之處来颤,歡迎指正忿项。
轉(zhuǎn)載請注明出處:http://www.reibang.com/p/b4571fa3b001
Stetho簡化Android調(diào)試(一) 一文中講述了如何使用Stetho
結合Chrome
遠程調(diào)試Android App
陡鹃。
Stetho
給我們調(diào)試帶來很大的便利沮峡,效率顯著提升的同時也產(chǎn)生一個問題:如果release版本中依然使用Stetho就會造成應用程序數(shù)據(jù)的泄露算撮。因此我們只需在調(diào)試階段(debug)
中使用双肤。因此有了下面這段代碼:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
if(BuildConfig.DEBUG){
// Debug模式下才初始化
Stetho.initializeWithDefaults(this);
}
}
}
是的,這樣確實可以解決release
版本中造成的應用程序數(shù)據(jù)泄露的問題钮惠。但是茅糜,對于 ‘只在調(diào)試階段(debug)
中使用’ 這個問題,依然沒有很好的解決素挽。Stetho
相關的代碼蔑赘,jar
包會被打包進我們最終的apk
中,造成apk
的體積變大。而這些完全是沒有必要的缩赛。
當然耙箍,也有朋友會說:我發(fā)版的時候,把相關的代碼刪掉就行了酥馍。這樣雖然可行辩昆,但是偶爾也會忘記,并且相對麻煩旨袒。下面我就給出兩種方式來解決這一問題:
方法一:
- 修改
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
的實現(xiàn)類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
的實現(xiàn)類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);
}
}
}