-
-
Save codebymikey/64ec5fa684cc9b2b0e1b54f25fdb8c7e to your computer and use it in GitHub Desktop.
This is how I enable Stetho on debug variants on my React Native app. Release variants will use the default `MainReactPackage` package
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
package com.myapp; | |
import com.facebook.react.shell.MainReactPackage; | |
// stored in android/app/src/release/java/com/myapp/DiProvider.java | |
/** | |
* Release dependency injector for myapp. | |
*/ | |
class DiProvider { | |
static MainReactPackage provideMainReactPackage(){ | |
return new MainReactPackage(); | |
} | |
static void initialize(Application application) { | |
// noop | |
} | |
} |
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
package com.myapp; | |
import android.app.Application; | |
import android.content.Context; | |
import android.util.Log; | |
import com.facebook.react.bridge.ModuleSpec; | |
import com.facebook.react.bridge.NativeModule; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.modules.network.NetworkInterceptorCreator; | |
import com.facebook.react.modules.network.NetworkingModule; | |
import com.facebook.react.shell.MainReactPackage; | |
import okhttp3.Interceptor; | |
import com.facebook.stetho.Stetho; | |
import com.facebook.stetho.okhttp3.StethoInterceptor; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.inject.Provider; | |
// stored in android/app/src/debug/java/com/myapp/DiProvider.java | |
/** | |
* Dependency injector. | |
*/ | |
class DiProvider { | |
/** | |
* Provide a react package that supports stetho http logging. | |
* Initially based off | |
* https://medium.com/@andr3wjack/stetho-with-react-native-87642e5d7131#.oc293ztbx | |
*/ | |
static MainReactPackage provideMainReactPackage(){ | |
return new MainReactPackage() { | |
@Override | |
public List<ModuleSpec> getNativeModules(final ReactApplicationContext context) { | |
List<ModuleSpec> modules = super.getNativeModules(context); | |
Class<NetworkingModule> networkModuleClazz = NetworkingModule.class; | |
int expectedIndexPos = 15; | |
if(modules.size() > expectedIndexPos) { | |
ModuleSpec expectedNetworkModule = modules.get(expectedIndexPos); | |
if (expectedNetworkModule.getType().equals(networkModuleClazz)) { | |
modules.set(expectedIndexPos, provideNetworkModuleSpec(context)); | |
} | |
return modules; | |
} | |
// Couldn't find it at the expected location, search through the rest. | |
for (int i = 0; i < modules.size(); i++) { | |
ModuleSpec module = modules.get(i); | |
if (module.getType().equals(networkModuleClazz)) { | |
modules.set(i, provideNetworkModuleSpec(context)); | |
break; | |
} | |
} | |
return modules; | |
} | |
}; | |
} | |
/** | |
* @return NetworkingModule with network interceptors (including stetho). | |
*/ | |
private static ModuleSpec provideNetworkModuleSpec(final ReactApplicationContext context){ | |
return new ModuleSpec(NetworkingModule.class, new Provider<NativeModule>() { | |
@Override | |
public NativeModule get() { | |
return new NetworkingModule(context, provideNetworkInterceptors()); | |
} | |
}); | |
} | |
private static List<NetworkInterceptorCreator> provideNetworkInterceptors(){ | |
List<NetworkInterceptorCreator> interceptors = new ArrayList<>(1); | |
interceptors.add(new NetworkInterceptorCreator() { | |
@Override | |
public Interceptor create() { | |
return new StethoInterceptor(); | |
} | |
}); | |
return interceptors; | |
} | |
static void initialize(Application application) { | |
Stetho.initializeWithDefaults(application); | |
} | |
} |
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
package com.myapp; | |
import android.app.Application; | |
import com.facebook.react.ReactApplication; | |
import com.bugsnag.BugsnagReactNative; | |
import com.apsl.versionnumber.RNVersionNumberPackage; | |
import com.learnium.RNDeviceInfo.RNDeviceInfo; | |
import com.oblador.vectoricons.VectorIconsPackage; | |
import cl.json.RNSharePackage; | |
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage; | |
import com.imagepicker.ImagePickerPackage; | |
import com.devfd.RNGeocoder.RNGeocoderPackage; | |
import com.yoloci.fileupload.FileUploadPackage; | |
import com.joshblour.reactnativepermissions.ReactNativePermissionsPackage; | |
import com.facebook.react.ReactNativeHost; | |
import com.facebook.react.ReactPackage; | |
import com.facebook.react.shell.MainReactPackage; | |
import com.facebook.soloader.SoLoader; | |
import java.util.Arrays; | |
import java.util.List; | |
// stored in android/app/src/main/java/com/myapp/MainApplication.java | |
public class MainApplication extends Application implements ReactApplication { | |
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { | |
@Override | |
public boolean getUseDeveloperSupport() { | |
return BuildConfig.DEBUG; | |
} | |
@Override | |
protected List<ReactPackage> getPackages() { | |
return Arrays.asList( | |
DiProvider.provideMainReactPackage(), | |
// other packages used, can be removed ... | |
BugsnagReactNative.getPackage(), | |
new RNVersionNumberPackage(), | |
new RNDeviceInfo(), | |
new VectorIconsPackage(), | |
new RNSharePackage(), | |
new ReactNativePushNotificationPackage(), | |
new ImagePickerPackage(), | |
new RNGeocoderPackage(), | |
new FileUploadPackage(), | |
new ReactNativePermissionsPackage() | |
); | |
} | |
}; | |
@Override | |
public ReactNativeHost getReactNativeHost() { | |
return mReactNativeHost; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
SoLoader.init(this, /* native exopackage */ false); | |
DiProvider.initialize(this); // initialize stetho when available. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to use the custom HttpClient in all variants.
Then just just place the single
DiProvider.java
file inandroid/app/src/main/java/com/myapp/DiProvider.java
and be on your way.You'll obviously need to replace
com.myapp
with the correct package name.And
com/myapp
with the correct file path to your existing code.