Skip to content

Commit

Permalink
Updates to ViewModel support to use the new CreationExtras.
Browse files Browse the repository at this point in the history
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
  • Loading branch information
Chang-Eric authored and Dagger Team committed Jul 11, 2022
1 parent a7f8dac commit f8bea0c
Show file tree
Hide file tree
Showing 41 changed files with 388 additions and 103 deletions.
2 changes: 1 addition & 1 deletion BUILD
Expand Up @@ -101,7 +101,7 @@ android_library(
name = "android_local_test_exports",
exports = [
# TODO(bcorso): see if we can remove jsr250 dep from autovalue to prevent this.
"@javax_annotation_jsr250_api", # For @Generated
"@maven//:javax_annotation_jsr250_api", # For @Generated
"@maven//:org_robolectric_shadows_framework", # For ActivityController
"@maven//:androidx_lifecycle_lifecycle_common", # For Lifecycle.State
"@maven//:androidx_activity_activity", # For ComponentActivity
Expand Down
25 changes: 14 additions & 11 deletions WORKSPACE
Expand Up @@ -64,9 +64,9 @@ local_repository(

http_archive(
name = "google_bazel_common",
sha256 = "8b6aebdc095c8448b2f6a72bb8eae4a563891467e2d20c943f21940b1c444e38",
strip_prefix = "bazel-common-3d0e5005cfcbee836e31695d4ab91b5328ccc506",
urls = ["https://github.com/google/bazel-common/archive/3d0e5005cfcbee836e31695d4ab91b5328ccc506.zip"],
sha256 = "60a9aebe25f476646f61c041d1679a9b21076deffbd51526838c7f24d6468ac0",
strip_prefix = "bazel-common-227a23a508a2fab0fa67ffe2d9332ae536a40edc",
urls = ["https://github.com/google/bazel-common/archive/227a23a508a2fab0fa67ffe2d9332ae536a40edc.zip"],
)

load("@google_bazel_common//:workspace_defs.bzl", "google_common_workspace_rules")
Expand Down Expand Up @@ -161,9 +161,9 @@ kt_register_toolchains()
# Load Maven dependencies
#############################

RULES_JVM_EXTERNAL_TAG = "2.7"
RULES_JVM_EXTERNAL_TAG = "4.2"

RULES_JVM_EXTERNAL_SHA = "f04b1466a00a2845106801e0c5cec96841f49ea4e7d1df88dc8e4bf31523df74"
RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca"

http_archive(
name = "rules_jvm_external",
Expand Down Expand Up @@ -199,13 +199,16 @@ maven_install(
artifacts = [
"androidx.annotation:annotation:1.1.0",
"androidx.appcompat:appcompat:1.3.1",
"androidx.activity:activity:1.3.1",
"androidx.fragment:fragment:1.3.6",
"androidx.lifecycle:lifecycle-common:2.3.1",
"androidx.lifecycle:lifecycle-viewmodel:2.3.1",
"androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.1",
"androidx.activity:activity:1.5.0",
"androidx.fragment:fragment:1.5.0",
"androidx.lifecycle:lifecycle-common:2.5.0",
"androidx.lifecycle:lifecycle-viewmodel:2.5.0",
"androidx.lifecycle:lifecycle-viewmodel-savedstate:2.5.0",
"androidx.multidex:multidex:2.0.1",
"androidx.savedstate:savedstate:1.0.0",
"androidx.navigation:navigation-common:2.5.0",
"androidx.navigation:navigation-fragment:2.5.0",
"androidx.navigation:navigation-runtime:2.5.0",
"androidx.savedstate:savedstate:1.2.0",
"androidx.test:monitor:1.4.0",
"androidx.test:core:1.4.0",
"androidx.test.ext:junit:1.1.3",
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/hilt/android/BUILD
Expand Up @@ -216,7 +216,7 @@ gen_maven_artifact(
"com.google.guava:guava",
"javax.annotation:jsr250-api",
],
javadoc_android_api_level = 30,
javadoc_android_api_level = 32,
javadoc_exclude_packages = [
"dagger.hilt.android.internal",
],
Expand Down
Expand Up @@ -24,6 +24,7 @@
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.viewmodel.CreationExtras;
import androidx.savedstate.SavedStateRegistryOwner;
import dagger.Module;
import dagger.hilt.EntryPoint;
Expand Down Expand Up @@ -78,7 +79,7 @@ public HiltViewModelFactory(
this.hiltViewModelKeys = hiltViewModelKeys;
this.delegateFactory = delegateFactory;
this.hiltViewModelFactory =
new AbstractSavedStateViewModelFactory(owner, defaultArgs) {
new AbstractSavedStateViewModelFactory() {
@NonNull
@Override
@SuppressWarnings("unchecked")
Expand All @@ -102,6 +103,17 @@ protected <T extends ViewModel> T create(
};
}

@NonNull
@Override
public <T extends ViewModel> T create(
@NonNull Class<T> modelClass, @NonNull CreationExtras extras) {
if (hiltViewModelKeys.contains(modelClass.getName())) {
return hiltViewModelFactory.create(modelClass, extras);
} else {
return delegateFactory.create(modelClass, extras);
}
}

@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
Expand Down
Expand Up @@ -4,12 +4,12 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

defaultConfig {
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
versionCode 1
versionName "1.0"
}
Expand All @@ -25,4 +25,4 @@ dependencies {

implementation project(':libraryB')
implementation project(':libraryC')
}
}
Expand Up @@ -4,12 +4,12 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

defaultConfig {
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
versionCode 1
versionName "1.0"
}
Expand All @@ -22,4 +22,4 @@ android {
dependencies {
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'
}
}
Expand Up @@ -20,8 +20,8 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

flavorDimensions 'api', 'version'
productFlavors {
Expand All @@ -46,7 +46,7 @@ android {
defaultConfig {
applicationId "simple.app"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
Expand All @@ -63,4 +63,4 @@ dependencies {

hilt {
enableAggregatingTask = true
}
}
Expand Up @@ -20,8 +20,8 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

flavorDimensions 'api', 'version'
productFlavors {
Expand All @@ -45,7 +45,7 @@ android {

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
Expand All @@ -57,4 +57,4 @@ android {
dependencies {
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'
}
}
Expand Up @@ -20,8 +20,8 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

flavorDimensions 'api', 'version'
productFlavors {
Expand All @@ -46,7 +46,7 @@ android {
defaultConfig {
applicationId "simple.app"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
Expand All @@ -63,4 +63,4 @@ dependencies {

hilt {
enableAggregatingTask = true
}
}
Expand Up @@ -20,8 +20,8 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

flavorDimensions 'api', 'version'
productFlavors {
Expand All @@ -45,7 +45,7 @@ android {

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
Expand All @@ -57,4 +57,4 @@ android {
dependencies {
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'
}
}
Expand Up @@ -130,13 +130,13 @@ class GradleTestRunner(val tempFolder: TemporaryFolder) {
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"
defaultConfig {
applicationId "plugin.test"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
}
compileOptions {
Expand Down
Expand Up @@ -146,13 +146,13 @@ class IncrementalProcessorTest(private val incapMode: String) {
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"
defaultConfig {
applicationId "hilt.simple"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
javaCompileOptions {
annotationProcessorOptions {
arguments += ["dagger.hilt.shareTestComponents" : "true"]
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/hilt/android/processor/BUILD
Expand Up @@ -90,7 +90,7 @@ gen_maven_artifact(
"org.jetbrains.kotlin:kotlin-stdlib",
"org.jetbrains.kotlinx:kotlinx-metadata-jvm",
],
javadoc_android_api_level = 30,
javadoc_android_api_level = 32,
javadoc_root_packages = [
# Java 11 javadocs requires non-empty root package so use ".internal" as the root package.
"dagger.hilt.processor.internal",
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/hilt/android/testing/BUILD
Expand Up @@ -237,7 +237,7 @@ gen_maven_artifact(
"com.google.guava:guava",
"javax.annotation:jsr250-api",
],
javadoc_android_api_level = 30,
javadoc_android_api_level = 32,
javadoc_exclude_packages = [
"dagger.hilt.internal",
"dagger.hilt.android.internal",
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/hilt/processor/BUILD
Expand Up @@ -112,7 +112,7 @@ gen_maven_artifact(
"org.jetbrains.kotlin:kotlin-stdlib",
"org.jetbrains.kotlinx:kotlinx-metadata-jvm",
],
javadoc_android_api_level = 30,
javadoc_android_api_level = 32,
javadoc_root_packages = [
# Java 11 javadocs requires non-empty root package so use ".internal" as the root package.
"dagger.hilt.processor.internal",
Expand Down
6 changes: 3 additions & 3 deletions javatests/artifacts/dagger-android/simple/app/build.gradle
Expand Up @@ -17,13 +17,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

defaultConfig {
applicationId "dagger.android.simple"
minSdkVersion 15
targetSdkVersion 30
targetSdkVersion 32
versionCode 1
versionName "1.0"
}
Expand Down
Expand Up @@ -20,13 +20,13 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

defaultConfig {
applicationId "dagger.hilt.android.simple"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
Expand All @@ -38,4 +38,4 @@ android {
dependencies {
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'
}
}
Expand Up @@ -18,13 +18,13 @@ apply plugin: 'com.android.application'
apply plugin: 'dagger.hilt.android.plugin'

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

defaultConfig {
applicationId "dagger.hilt.android.simple"
minSdkVersion 15
targetSdkVersion 30
targetSdkVersion 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
6 changes: 3 additions & 3 deletions javatests/artifacts/hilt-android/simple/app/build.gradle
Expand Up @@ -43,13 +43,13 @@ def getAdditionalTestDirs(String variant) {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
compileSdkVersion 32
buildToolsVersion "32.0.0"

defaultConfig {
applicationId "dagger.hilt.android.simple"
minSdkVersion 15
targetSdkVersion 30
targetSdkVersion 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "dagger.hilt.android.simple.SimpleEmulatorTestRunner"
Expand Down

0 comments on commit f8bea0c

Please sign in to comment.