### Initialize JsRuntimeReplFactoryBuilder - Java
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Initializes a `JsRuntimeReplFactoryBuilder` instance using the application context. This builder is the starting point for configuring the JavaScript environment by adding classes, packages, variables, and functions before integrating it with Stetho.
```Java
// context is your application context\nJsRuntimeReplFactoryBuilder jsRuntimeBuilder = new JsRuntimeReplFactoryBuilder(context);
```
--------------------------------
### Registering Application Class in Android Manifest (XML)
Source: https://github.com/facebook/stetho/blob/main/README.md
This XML snippet demonstrates how to register your custom `Application` class in the `AndroidManifest.xml` file. This is crucial for Stetho initialization to occur when the application starts.
```xml
```
--------------------------------
### Import Java Package into JS Runtime - Java
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Imports all classes within a specified Java package (`"android.content"` in this example) into the JavaScript runtime scope. This allows access to those classes from JavaScript without needing explicit imports per class. Requires a `JsRuntimeReplFactoryBuilder` instance initialized with context.
```Java
jsRuntimeBuilder.importPackage("android.content");
```
--------------------------------
### Generated Data Structures from Protocol Definition - Java
Source: https://github.com/facebook/stetho/blob/main/build-tools/readme.md
These are example Java classes (`FunctionDetails`, `Scope`) automatically generated from a protocol definition file. They are designed to represent protocol objects and are annotated with `@JsonProperty` for JSON deserialization, typically used with libraries like Jackson.
```Java
public static class FunctionDetails {
@JsonProperty
public Location location;
@JsonProperty(required = true)
public String functionName;
@JsonProperty(required = true)
public boolean isGenerator;
@JsonProperty
public List scopeChain;
}
public static class Scope {
@JsonProperty(required = true)
public String type;
@JsonProperty(required = true)
public Runtime.RemoteObject object;
}
```
--------------------------------
### Import Java Class into JS Runtime - Java
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Imports a specific Java class (`R.class` in this example) into the JavaScript runtime scope. After importing, static members of the class become accessible directly from JavaScript. Requires a `JsRuntimeReplFactoryBuilder` instance initialized with context.
```Java
jsRuntimeBuilder.importClass(R.class);
```
--------------------------------
### Initializing Stetho with Defaults (Java)
Source: https://github.com/facebook/stetho/blob/main/README.md
This Java snippet shows the basic initialization of Stetho, typically done in the `onCreate` method of your custom `Application` class. It sets up the default configuration required for core Stetho features.
```java
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}
```
--------------------------------
### Initializing Stetho with Custom Dumpapp Plugins (Java)
Source: https://github.com/facebook/stetho/blob/main/README.md
This Java snippet demonstrates how to perform a custom initialization of Stetho to include custom `dumpapp` plugins. It uses the `Stetho.newInitializerBuilder` to configure plugins and other components.
```java
Stetho.initialize(Stetho.newInitializerBuilder(context)
.enableDumpapp(new DumperPluginsProvider() {
@Override
public Iterable get() {
return new Stetho.DefaultDumperPluginsBuilder(context)
.provide(new MyDumperPlugin())
.finish();
}
})
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context))
.build())
```
--------------------------------
### Enabling Stetho Network Inspection with OkHttp3 (Java)
Source: https://github.com/facebook/stetho/blob/main/README.md
This Java code shows how to add the `StethoInterceptor` to an OkHttp 3.x `OkHttpClient.Builder`. Adding this network interceptor enables Stetho to provide detailed network request and response information in Chrome Dev Tools.
```java
new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor())
.build()
```
--------------------------------
### Adding Stetho OkHttp3 Dependency (Gradle)
Source: https://github.com/facebook/stetho/blob/main/README.md
This Gradle dependency is needed to integrate Stetho's network inspection capabilities with OkHttp version 3.x. Add this line to your module's build.gradle `dependencies` to use the Stetho OkHttp3 interceptor.
```groovy
implementation 'com.facebook.stetho:stetho-okhttp3:1.6.0'
```
--------------------------------
### Adding Stetho Core Dependency (Maven)
Source: https://github.com/facebook/stetho/blob/main/README.md
This snippet provides the Maven dependency coordinates for the core Stetho library. Include this XML block within the `` section of your project's pom.xml file.
```xml
com.facebook.stetho
stetho
1.6.0
```
--------------------------------
### Show Android Toast - Stetho JS Console - JavaScript
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Demonstrates how to execute JavaScript code within the Stetho console to interact with the Android environment, specifically showing a `Toast` message. This requires importing necessary Android packages (`android.widget`, `android.os`) and utilizing the `context` variable available in the default JS scope.
```JavaScript
importPackage(android.widget);\nimportPackage(android.os);\nvar handler = new Handler(Looper.getMainLooper());\nhandler.post(function() { Toast.makeText(context, \"Hello from JavaScript\", Toast.LENGTH_LONG).show() });
```
--------------------------------
### Configure Stetho JavaScript Runtime - Java
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Initializes Stetho with a custom configuration that enables the WebKit inspector and provides a custom JavaScript runtime REPL with a pre-defined variable ('foo'). This allows passing custom data or objects from Java into the JavaScript console environment. Requires the application context.
```Java
Stetho.initialize(Stetho.newInitializerBuilder(context)\n .enableWebKitInspector(new InspectorModulesProvider() {\n @Override\n public Iterable get() {\n return new DefaultInspectorModulesBuilder(context).runtimeRepl(\n new JsRuntimeReplFactoryBuilder(context)\n // Pass to JavaScript: var foo = \"bar\";\n .addVariable(\"foo\", \"bar\")\n .build()\n ).finish();\n }\n })\n .build();
```
--------------------------------
### Adding Stetho Core Dependency (Gradle)
Source: https://github.com/facebook/stetho/blob/main/README.md
This snippet shows the Gradle dependency configuration required to include the core Stetho library in an Android project. Add this line to the `dependencies` block of your module-level build.gradle file.
```groovy
implementation 'com.facebook.stetho:stetho:1.6.0'
```
--------------------------------
### Add Stetho Rhino Dependency - Gradle - Groovy
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Add the `stetho-js-rhino` library as a dependency in a Gradle build script to enable the JavaScript console feature. Ensure the main `stetho` dependency is also included.
```Groovy
implementation 'com.facebook.stetho:stetho-js-rhino:1.4.2'
```
--------------------------------
### Adding Stetho URLConnection Dependency (Gradle)
Source: https://github.com/facebook/stetho/blob/main/README.md
Use this Gradle dependency if your application uses HttpURLConnection and you want to enable Stetho network inspection for those connections. It provides the necessary helper class.
```groovy
implementation 'com.facebook.stetho:stetho-urlconnection:1.6.0'
```
--------------------------------
### Add Stetho Rhino Dependency - Maven - XML
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Add the `stetho-js-rhino` library as a dependency in a Maven `pom.xml` file to include the JavaScript console feature. Ensure the main `stetho` dependency is also included.
```XML
\n com.facebook.stetho\n stetho-js-rhino\n 1.4.2\n
```
--------------------------------
### Adding Stetho JS Rhino Dependency (Gradle)
Source: https://github.com/facebook/stetho/blob/main/README.md
Include this dependency in your Gradle file to enable a JavaScript console within Stetho using the Rhino JavaScript engine. This allows executing JavaScript code directly in the Chrome Developer Tools console.
```groovy
implementation 'com.facebook.stetho:stetho-js-rhino:1.6.0'
```
--------------------------------
### Aggressive Proguard Rules for Stetho Rhino - Proguard
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Provides more aggressive Proguard rules aimed at reducing the Dex method count by specifically keeping required Rhino packages while allowing others (like "tools") to potentially be removed. Requires careful testing to ensure functionality after applying these rules.
```Proguard
# stetho\n+keep class com.facebook.stetho.** { *; }\n\n# rhino (javascript)\n-dontwarn org.mozilla.javascript.**\n-dontwarn org.mozilla.classfile.**\n-keep class org.mozilla.classfile.** { *; }\n-keep class org.mozilla.javascript.* { *; }\n-keep class org.mozilla.javascript.annotations.** { *; }\n-keep class org.mozilla.javascript.ast.** { *; }\n-keep class org.mozilla.javascript.commonjs.module.** { *; }\n-keep class org.mozilla.javascript.commonjs.module.provider.** { *; }\n-keep class org.mozilla.javascript.debug.** { *; }\n-keep class org.mozilla.javascript.jdk13.** { *; }\n-keep class org.mozilla.javascript.jdk15.** { *; }\n-keep class org.mozilla.javascript.json.** { *; }\n-keep class org.mozilla.javascript.optimizer.** { *; }\n-keep class org.mozilla.javascript.regexp.** { *; }\n-keep class org.mozilla.javascript.serialize.** { *; }\n-keep class org.mozilla.javascript.typedarrays.** { *; }\n-keep class org.mozilla.javascript.v8dtoa.** { *; }\n-keep class org.mozilla.javascript.xml.** { *; }\n-keep class org.mozilla.javascript.xmlimpl.** { *; }
```
--------------------------------
### Basic Proguard Rules for Stetho Rhino - Proguard
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Provides basic Proguard rules to prevent obfuscation and shrinking of essential Stetho and Rhino classes. These rules are necessary to ensure the Stetho JavaScript module functions correctly after Proguard processing.
```Proguard
# stetho\n+keep class com.facebook.stetho.** { *; }\n\n# rhino (javascript)\n-dontwarn org.mozilla.javascript.**\n-dontwarn org.mozilla.classfile.**\n-keep class org.mozilla.javascript.** { *; }
```
--------------------------------
### Bind Custom Java Function to JS Runtime - Java
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Defines and binds a custom Java function (`toast`) callable from the JavaScript runtime. This allows executing Java code logic directly from the Stetho console, such as interacting with the UI thread. Requires a `JsRuntimeReplFactoryBuilder` instance and careful handling of threading.
```Java
// Your application context\nfinal Context context = this;\n\nfinal Handler handler = new Handler(Looper.getMainLooper());\n\n// Add the function: void toast(String)\njsRuntimeBuilder.addFunction("toast", new BaseFunction() {\n @Override\n public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {\n // javascript passes the arguments as varags\n final String message = args[0].toString();\n handler.post(new Runnable() {\n @Override\n public void run() {\n Toast.makeText(context, message, Toast.LENGTH_LONG).show();\n }\n });\n\n // return undef in javascript\n return org.mozilla.javascript.Context.getUndefinedValue();\n }\n});
```
--------------------------------
### Bind Java Variable to JS Runtime - Java
Source: https://github.com/facebook/stetho/blob/main/stetho-js-rhino/README.md
Binds a Java object (`new AtomicBoolean(true)`) to a variable name (`"flag"`) within the JavaScript runtime scope. This makes the Java object accessible from JavaScript under the given variable name. Only Java objects can be passed, primitive types are autoboxed. Requires a `JsRuntimeReplFactoryBuilder` instance.
```Java
jsRuntimeBuilder.addVariable("flag", new AtomicBoolean(true));
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.