Android debugging tool and android debugging

Source: Internet
Author: User

Android debugging tool and android debugging

StethoIt is an open-source debugging tool for Facebook's Android platform. Stetho can view the App layout, Sqlite, SharedPreference, and Network through Chrome without the root phone. It also supports creating Dump files.

It is very important to use Stetho to understand that Stetho is a debugging tool. In theory, it can only be used for debugging packages. If it is applied to the Release package, all your app data will be exposed. Our code is implemented with this as the center.


Core code implementation: First add Stetho reference

Add the dependency of Stetho directly to the Gradle file.

compile 'com.facebook.stetho:stetho-okhttp:1.1.1'debugCompile 'com.facebook.stetho:stetho:1.1.1'

The dependency of Stetho only needs to be packaged in Debug mode, but not in Release mode.
Stetho-okhttp is the dependency required to obtain Network data. Because there is dependency in the project, only dependencies of all modes can be added. See the following code.

Create an AndroidManifest and Application for the Debug package in a specific location.

As shown in, create the debug folder under the src directory of the main project. If you do not need the Dump function, you only need to create the AndroidManifest file and Application file.
In AndroidManifest, specify that Debug starts the app from DebugApplication.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    package="com.debug.xsldebug">    <application        tools:replace="android:name"        android:name=".activity.message.XSLDebugApplication" /></manifest>

DebugApplication inherits the original Application of the Application and adds the Stetho initialization code.

<pre name="code" class="java">public class XSLDebugApplication extends XSLApplication {    @Override    public void onCreate() {        super.onCreate();        Stetho.initialize(                Stetho.newInitializerBuilder(this)                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))                        .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))                        .build());    }    private static class XSLDumperPluginsProvider implements DumperPluginsProvider {        private final Context mContext;        public XSLDumperPluginsProvider(Context context) {            mContext = context;        }        @Override        public Iterable<DumperPlugin> get() {            ArrayList<DumperPlugin> plugins = new ArrayList<DumperPlugin>();            for (DumperPlugin defaultPlugin : Stetho.defaultDumperPluginsProvider(mContext).get()) {                plugins.add(defaultPlugin);            }            plugins.add(new XSLDumperPlugin());            return plugins;        }    }}


 

After the preceding settings, the app supports the basic functions of Stetho. You can use Chrome's device check or directly access chrome: // inspect/# devices to find the currently connected device.

To support viewing Network information, perform the following additional configurations:

Using the Okhttp network framework, you can easily integrate Stetho. If HttpURLConnection is used, it will be a lot of trouble. Our network framework is Okhttp2.2 +, so we can easily integrate Stetho.

Public String okHttpPost (String url, List <BasicNameValuePair> nameValuePairs) {String result = StringUtils. EMPTY_STRING; OkHttpClient client = OkHttpClientFactory. createDefault (); if (BuildConfig. DEBUG) {// Add Stetho-okhttp support client in the debug package. networkInterceptors (). add (new com. facebook. stetho. okhttp. stethoInterceptor ();} Request request = new Request. builder (). url (url ). post (getFormEncodingBuilder (nameValuePairs ). build ()). build (); try {Response response = client.newcall(requestcmd.exe cute (); if (response. isSuccessful () {return response. body (). string () ;}} catch (Exception e) {e. printStackTrace ();} return result ;}
Only the Debug package must support Stetho and the Release package is not required. But even the Release package will Build this line of code, so in the initial dependency, Stetho-Okhttp depends on Compile instead of debugCompile.


Preview:

1. view the Layout

2. Check Sqlite and execute SQL statements. Note: The command line is not in the Console window.


3. View Network requests.


4. Create Dump. In fact, this function has not been tried, because there is no need for dump.




Liang Jianbin of Apricot Forest R & D

Copyright Disclaimer: all of the above content is shared by xingshu Lin every week. We hope to share with a wider audience of technical staff the experience that xingshu loves work and technology and grow with everyone. If you have any questions, you can use the QQ Group apricot forest development space (246078103) to communicate with the technical staff. This article is the original article of the blogger and cannot be reproduced without the support of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.