-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add proper Stetho debugging setup documentation #12232
Closed
Closed
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eae8779
Update Debugging.md
louisgv 868d9f2
Update Stetho guide based on comment from @AndrewJack
louisgv 083934a
Update Debugging.md
louisgv 04579f7
Update Debugging.md
louisgv afa4759
Merge branch 'master' into patch-1
louisgv 1153232
Update Debugging.md
hramos 8056530
Update Debugging.md
hramos 23ad79f
Merge branch 'master' into patch-1
hramos 83e1975
Merge branch 'master' into patch-1
hramos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,43 +96,83 @@ The debugger will receive a list of all project roots, separated by a space. For | |
|
||
### Debugging with [Stetho](http://facebook.github.io/stetho/) on Android | ||
|
||
Following this guide to enable Stetho only for Debug mode: | ||
|
||
1. In ```android/app/build.gradle```, add these lines in the `dependencies` section: | ||
|
||
```gradle | ||
compile 'com.facebook.stetho:stetho:1.3.1' | ||
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1' | ||
debugCompile 'com.facebook.stetho:stetho:1.x.x' | ||
debugCompile 'com.facebook.stetho:stetho-okhttp3:1.x.x' | ||
``` | ||
|
||
2. In ```android/app/src/main/java/com/{yourAppName}/MainApplication.java```, add the following imports: | ||
|
||
```java | ||
import com.facebook.react.modules.network.ReactCookieJarContainer; | ||
import com.facebook.stetho.Stetho; | ||
import okhttp3.OkHttpClient; | ||
import com.facebook.react.modules.network.OkHttpClientProvider; | ||
import com.facebook.stetho.okhttp3.StethoInterceptor; | ||
import java.util.concurrent.TimeUnit; | ||
Go to http://facebook.github.io/stetho/ and replace 1.x.x with the latest version. For example: | ||
|
||
```gradle | ||
debugCompile 'com.facebook.stetho:stetho:1.4.2' | ||
debugCompile 'com.facebook.stetho:stetho-okhttp3:1.4.2' | ||
``` | ||
|
||
3. In ```android/app/src/main/java/com/{yourAppName}/MainApplication.java``` add the function: | ||
```java | ||
public void onCreate() { | ||
super.onCreate(); | ||
Stetho.initializeWithDefaults(this); | ||
OkHttpClient client = new OkHttpClient.Builder() | ||
.connectTimeout(0, TimeUnit.MILLISECONDS) | ||
.readTimeout(0, TimeUnit.MILLISECONDS) | ||
.writeTimeout(0, TimeUnit.MILLISECONDS) | ||
.cookieJar(new ReactCookieJarContainer()) | ||
.addNetworkInterceptor(new StethoInterceptor()) | ||
.build(); | ||
OkHttpClientProvider.replaceOkHttpClient(client); | ||
} | ||
2. From project root, create 2 Java class that will wrap the Stetho call, one for release and one for debug. With the terminal, these cmds should get it done: | ||
|
||
```bash | ||
touch android/app/src/debug/java/com/snap/StethoWrapper.java | ||
touch android/app/src/release/java/com/snap/StethoWrapper.java | ||
``` | ||
|
||
4. Run ```react-native run-android ``` | ||
For release: | ||
```java | ||
public class StethoWrapper { | ||
|
||
public static void initialize(Context context) { | ||
// NO_OP | ||
} | ||
|
||
public static void addInterceptor() { | ||
// NO_OP | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can change to public static void addInterceptor() {
// NO_OP
} |
||
} | ||
``` | ||
|
||
For debug: | ||
```java | ||
public class StethoWrapper { | ||
public static void initialize(Context context) { | ||
Stetho.initializeWithDefaults(context); | ||
} | ||
|
||
public static void addInterceptor() { | ||
OkHttpClient client = OkHttpClientProvider.getOkHttpClient() | ||
.newBuilder() | ||
.addNetworkInterceptor(new StethoInterceptor()) | ||
.build(); | ||
|
||
OkHttpClientProvider.replaceOkHttpClient(client); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be changed to: public static void addInterceptor() {
OkHttpClient client = OkHttpClientProvider.getOkHttpClient()
.newBuilder()
.addNetworkInterceptor(new StethoInterceptor())
.build();
OkHttpClientProvider.replaceOkHttpClient(client);
} |
||
} | ||
``` | ||
|
||
3. In ```android/app/src/main/java/com/{yourAppName}/MainApplication.java```, replace the original onCreate function with: | ||
|
||
```java | ||
public void onCreate() { | ||
super.onCreate(); | ||
|
||
if (BuildConfig.DEBUG) { | ||
|
||
StethoWrapper.initialize(this); | ||
|
||
StethoWrapper.addInterceptor(); | ||
} | ||
|
||
SoLoader.init(this, /* native exopackage */ false); | ||
} | ||
``` | ||
|
||
4. Open the android project in Android Studio and resolve all dependency issue. (Hover over the red marked lines and play around with the IDE so that it import all the correct library for you!) | ||
|
||
5. Run ```react-native run-android ``` | ||
|
||
5. In a new chrome tab, open : ```chrome://inspect```, click on 'Inspect device' (the one followed by "Powered by Stetho") | ||
6. In a new chrome tab, open : ```chrome://inspect```, click on 'Inspect device' (the one followed by "Powered by Stetho") | ||
|
||
## Debugging native code | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could remove this bit and specify the version to use.
The version is dependant on the version of OkHttp React native is compatible with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this PR prior to seeing this comment. Would you prefer we recommend a specific version that we know is compatible with RN, or is there a process we can document for people to figure out what version of OkHttp they should be using here?