Skip to content
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

Migrate codebase from Dagger to Hilt #505

Merged
merged 21 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// Top-level build file. Configuration specified here is inherited by all sub-projects/modules.

buildscript {
ext {
hiltVersion = "2.28-alpha"
}
repositories {
google()
jcenter()
Expand All @@ -26,6 +29,7 @@ buildscript {
}
dependencies {
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hiltVersion"
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "com.google.gms:google-services:4.3.3"
classpath "com.pascalwelsch.gitversioner:gitversioner:0.5.0"
Expand Down
24 changes: 16 additions & 8 deletions gnd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

apply plugin: 'com.android.application'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'com.google.firebase.firebase-perf'
apply plugin: 'com.google.firebase.crashlytics'
apply from: '../config/checkstyle/checkstyle.gradle'
Expand All @@ -29,6 +30,7 @@ project.ext {
daggerVersion = "2.27"
dturner marked this conversation as resolved.
Show resolved Hide resolved
firebaseCoreVersion = "17.4.3"
gmsVersion = "17.0.0"
hiltJetpackVersion = "1.0.0-alpha01"
lifecycleVersion = "2.2.0"
navigationVersion = "1.0.0"
roomVersion = "2.2.5"
Expand Down Expand Up @@ -91,6 +93,9 @@ android {
includeAndroidResources = true
}
}
hilt {
enableTransformForLocalTests = true
}
}


Expand Down Expand Up @@ -136,14 +141,15 @@ dependencies {
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'com.google.firebase:firebase-crashlytics:17.0.1'

// Dagger
implementation "com.google.dagger:dagger:$project.daggerVersion"
implementation "com.google.dagger:dagger-android:$project.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$project.daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$project.daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$project.daggerVersion"
testAnnotationProcessor "com.google.dagger:dagger-compiler:$project.daggerVersion"
androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:$project.daggerVersion"
// Hilt
implementation("com.google.dagger:hilt-android:$project.hiltVersion")
implementation("androidx.hilt:hilt-lifecycle-viewmodel:$project.hiltJetpackVersion")
implementation("androidx.hilt:hilt-work:$project.hiltJetpackVersion")
androidTestImplementation("com.google.dagger:hilt-android-testing:$project.hiltVersion")
annotationProcessor("com.google.dagger:hilt-android-compiler:$project.hiltVersion")
annotationProcessor("androidx.hilt:hilt-compiler:$project.hiltJetpackVersion")
androidTestAnnotationProcessor("com.google.dagger:hilt-android-compiler:$project.hiltVersion")
androidTestAnnotationProcessor("androidx.hilt:hilt-compiler:$project.hiltJetpackVersion")
dturner marked this conversation as resolved.
Show resolved Hide resolved

// ButterKnife
implementation "com.jakewharton:butterknife:$project.butterKnifeVersion"
Expand Down Expand Up @@ -206,6 +212,8 @@ dependencies {
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'android.arch.core:core-testing:1.1.1'
testImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
testAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'
dturner marked this conversation as resolved.
Show resolved Hide resolved

androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
exclude group: 'com.android.support', module: 'support-annotations'
Expand Down
13 changes: 3 additions & 10 deletions gnd/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,11 @@
android:name="org.apache.http.legacy"
android:required="false" />

<!--
Disable WorkManagerInitializer so that we can initialize ourselves in GndApplication setting
custom GndWorkerFactory to perform Dagger injection.
TODO(github.com/google/dagger/issues/1183): Remove this if and when Worker inject is supported
natively.
-->
<!-- Remove the default WorkManager initializer since we are providing our own -->
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="com.google.android.gnd.workmanager-init"
android:enabled="false"
android:exported="false"
tools:replace="android:authorities" />
android:authorities="${applicationId}.workmanager-init"
tools:node="remove" />
dturner marked this conversation as resolved.
Show resolved Hide resolved
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.gnd.ui.common.TwoLineToolbar;
import com.google.android.gnd.ui.util.DrawableUtil;
import dagger.android.support.DaggerAppCompatActivity;
import javax.annotation.OverridingMethodsMustInvokeSuper;

/** Base activity class containing common helper methods. */
public abstract class AbstractActivity extends DaggerAppCompatActivity {
public abstract class AbstractActivity extends AppCompatActivity {

private DrawableUtil drawableUtil;

Expand Down
27 changes: 11 additions & 16 deletions gnd/src/main/java/com/google/android/gnd/GndApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@

package com.google.android.gnd;

import android.app.Application;
import android.content.Context;
import android.os.StrictMode;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.hilt.work.HiltWorkerFactory;
import androidx.multidex.MultiDex;
import androidx.work.Configuration;
import androidx.work.WorkManager;
import com.akaita.java.rxjava2debug.RxJava2Debug;
import com.facebook.stetho.Stetho;
import com.google.android.gnd.inject.GndWorkerFactory;
import com.google.android.gnd.rx.RxDebug;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import dagger.android.AndroidInjector;
import dagger.android.support.DaggerApplication;
import dagger.hilt.android.HiltAndroidApp;
import io.reactivex.plugins.RxJavaPlugins;
import javax.inject.Inject;
import org.jetbrains.annotations.NotNull;
import timber.log.Timber;

// TODO: When implementing background data sync service, we'll need to inject a Service here; we
// should then extend DaggerApplication instead. If MultiDex is still needed, we can install it
// without extending MultiDexApplication.
dturner marked this conversation as resolved.
Show resolved Hide resolved
public class GndApplication extends DaggerApplication {
@HiltAndroidApp
public class GndApplication extends Application implements Configuration.Provider {

@Inject GndWorkerFactory workerFactory;
@Inject HiltWorkerFactory workerFactory;

@Override
protected void attachBaseContext(Context base) {
Expand All @@ -49,6 +50,7 @@ protected void attachBaseContext(Context base) {

@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Timber.d("DEBUG build config active; enabling debug tooling");

Expand All @@ -61,19 +63,12 @@ public void onCreate() {
setStrictMode();
}

super.onCreate();

// Enable RxJava assembly stack collection for more useful stack traces.
RxJava2Debug.enableRxJava2AssemblyTracking(new String[] {getClass().getPackage().getName()});

// Prevent RxJava from force-quitting on unhandled errors.
RxJavaPlugins.setErrorHandler(RxDebug::logEnhancedStackTrace);

// Set custom worker factory that allow Workers to use Dagger injection.
// TODO(github.com/google/dagger/issues/1183): Remove once Workers support injection.
WorkManager.initialize(
this, new Configuration.Builder().setWorkerFactory(workerFactory).build());

if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
} else {
Expand All @@ -82,9 +77,9 @@ public void onCreate() {
}

@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
// Root of dependency injection.
return DaggerGndApplicationComponent.factory().create(this);
@NotNull
public Configuration getWorkManagerConfiguration() {
return new Configuration.Builder().setWorkerFactory(workerFactory).build();
}

private void setStrictMode() {
Expand Down

This file was deleted.

44 changes: 5 additions & 39 deletions gnd/src/main/java/com/google/android/gnd/GndApplicationModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.room.Room;
import androidx.work.WorkManager;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gnd.inject.ActivityScoped;
import com.google.android.gnd.persistence.local.room.LocalDatabase;
import com.google.android.gnd.persistence.remote.RemoteDataStore;
import com.google.android.gnd.persistence.remote.RemoteStorageManager;
import com.google.android.gnd.persistence.remote.firestore.FirestoreDataStore;
import com.google.android.gnd.persistence.remote.firestore.FirestoreStorageManager;
import com.google.android.gnd.persistence.remote.firestore.FirestoreUuidGenerator;
import com.google.android.gnd.persistence.uuid.OfflineUuidGenerator;
import com.google.android.gnd.rx.RxSchedulers;
import com.google.android.gnd.rx.Schedulers;
import com.google.android.gnd.ui.common.ViewModelModule;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreSettings;
Expand All @@ -40,45 +35,25 @@
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import dagger.android.ContributesAndroidInjector;
import dagger.android.support.AndroidSupportInjectionModule;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import javax.inject.Singleton;

@Module(includes = {AndroidSupportInjectionModule.class, ViewModelModule.class})
@InstallIn(ApplicationComponent.class)
@Module(includes = {ViewModelModule.class})
abstract class GndApplicationModule {
private static final String SHARED_PREFERENCES_NAME = "shared_prefs";

/** Causes Dagger Android to generate a sub-component for the MainActivity. */
@ActivityScoped
@ContributesAndroidInjector(modules = MainActivityModule.class)
abstract MainActivity mainActivityInjector();

/** Causes Dagger Android to generate a sub-component for the SettingsActivity. */
@ActivityScoped
@ContributesAndroidInjector(modules = SettingsActivityModule.class)
abstract SettingsActivity settingsActivityInjector();

/** Provides the Firestore implementation of remote data store. */
@Binds
@Singleton
abstract RemoteDataStore remoteDataStore(FirestoreDataStore ds);

@Binds
@Singleton
abstract Schedulers schedulers(RxSchedulers rxSchedulers);

/** Provides the Firestore implementation of offline unique id generation. */
@Binds
@Singleton
abstract OfflineUuidGenerator offlineUuidGenerator(FirestoreUuidGenerator uuidGenerator);

@Binds
@Singleton
abstract Application application(GndApplication app);

@Binds
abstract Context context(GndApplication application);

@Provides
@Singleton
static GoogleApiAvailability googleApiAvailability() {
Expand All @@ -93,7 +68,7 @@ static WorkManager workManager() {

@Provides
@Singleton
static SharedPreferences sharedPreferences(GndApplication application) {
static SharedPreferences sharedPreferences(Application application) {
return application
.getApplicationContext()
.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
Expand Down Expand Up @@ -126,13 +101,4 @@ static FirebaseFirestore firebaseFirestore(FirebaseFirestoreSettings settings) {
static StorageReference firebaseStorageReference() {
return FirebaseStorage.getInstance().getReference();
}

@Provides
@Singleton
static LocalDatabase localDatabase(Context context) {
return Room.databaseBuilder(context, LocalDatabase.class, Config.DB_NAME)
// TODO(#128): Disable before official release.
.fallbackToDestructiveMigration()
.build();
}
}
4 changes: 2 additions & 2 deletions gnd/src/main/java/com/google/android/gnd/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
import com.google.android.gnd.ui.common.EphemeralPopups;
import com.google.android.gnd.ui.common.Navigator;
import com.google.android.gnd.ui.common.ViewModelFactory;
import dagger.hilt.android.AndroidEntryPoint;
import javax.inject.Inject;
import javax.inject.Singleton;
import timber.log.Timber;

/**
* The app's main activity. The app consists of multiples Fragments that live under this activity.
*/
@Singleton
@AndroidEntryPoint
public class MainActivity extends AbstractActivity {

@Inject ActivityStreams activityStreams;
Expand Down
Loading