Skip to content

Instantly share code, notes, and snippets.

@IanField90
Last active March 24, 2016 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanField90/7b4aeca62b5fe7497c6f to your computer and use it in GitHub Desktop.
Save IanField90/7b4aeca62b5fe7497c6f to your computer and use it in GitHub Desktop.
Basic AndroidApplication class
@Singleton @Component(modules = AppModule.class) public interface AppComponent {
void inject(MainActivity activity);
}
/**
* A module for Android-specific dependencies which require a {@link Context} or
* {@link android.app.Application} to create.
*/
@Module public class AppModule {
private final DevStatApplication application;
public AppModule(DevStatApplication application) {
this.application = application;
}
/**
* Allow the application context to be injected but require that it be annotated with
* {@link ApplicationContext @Annotation} to explicitly differentiate it from an activity context.
*/
@Provides @Singleton @ApplicationContext Context provideContext() {
return application;
}
// @Provides @Singleton StatHelper provideStatHelper() {
// return new StatHelper(application);
// }
@Qualifier @Documented @Retention(RetentionPolicy.RUNTIME) public @interface ApplicationContext {
}
}
public class BaseApplication extends Application {
private static final String TAG = BaseApplication.class.getSimpleName();
private AppComponent component;
@Override public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
LeakCanary.install(this);
component = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this);
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override public void onActivityCreated(Activity activity, Bundle bundle) {
Log.d(TAG, activity.getLocalClassName() + " created. Intent extras: " + getJSONFromBundle(activity.getIntent().getExtras()));
}
@Override public void onActivityStarted(Activity activity) { }
@Override public void onActivityResumed(Activity activity) { }
@Override public void onActivityPaused(Activity activity) { }
@Override public void onActivityStopped(Activity activity) { }
@Override public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { }
@Override public void onActivityDestroyed(Activity activity) { }
});
}
}
public static String getJSONFromBundle(Bundle bundle) {
if (bundle == null || Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
return null;
}
JSONObject json = new JSONObject();
Set<String> keys = bundle.keySet();
for (String key : keys) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
json.put(key, JSONObject.wrap(bundle.get(key)));
}
} catch(JSONException ignore) { }
}
return json.toString();
}
public AppComponent component() {
return component;
}
}
// Top level gradle:
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
jcenter()
}
dependencies {
classpath 'io.fabric.tools:gradle:1.20.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'io.fabric'
apply plugin: 'com.neenbedankt.android-apt'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
// Crashlytics
compile('com.crashlytics.sdk.android:crashlytics:2.5.1@aar') {
transitive = true;
}
// OkHTTP
compile 'com.squareup.okhttp:okhttp:2.5.0'
// Stetho
compile 'com.facebook.stetho:stetho:1.2.0'
compile 'com.facebook.stetho:stetho-okhttp:1.3.1'
// Dagger
provided 'javax.annotation:jsr250-api:1.0'
compile 'com.google.dagger:dagger:2.0.2'
apt 'com.google.dagger:dagger-compiler:2.0.1'
}
// Inside init
client = new OkHttpClient();
if (BuildConfig.DEBUG) {
client.networkInterceptors().add(new StethoInterceptor());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment