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 13, 2022
1 parent df8b8e2 commit cbc7e5a
Show file tree
Hide file tree
Showing 54 changed files with 510 additions and 233 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -56,7 +56,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
agp: ['4.1.0', '4.2.0', '7.0.0', '7.1.2']
agp: ['7.0.0', '7.1.2']
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/artifact-android-local-tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -58,7 +58,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
agp: ['4.1.0', '4.2.0', '7.0.0']
agp: ['7.0.0', '7.1.2']
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/artifact-android-local-tests
Expand Down
4 changes: 2 additions & 2 deletions 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 All @@ -127,7 +127,7 @@ javadoc_library(
"//java/dagger/producers:producers-srcs",
"//java/dagger/spi:spi-srcs",
],
android_api_level = 30,
android_api_level = 32,
# TODO(ronshapiro): figure out how to specify the version number for release builds
doctitle = "Dagger Dependency Injection API",
exclude_packages = [
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Expand Up @@ -44,9 +44,9 @@ attempt to delete the `build/` folder will also delete the bazel `BUILD` files.
[download](https://developer.android.com/studio#command-tools)
and unzip it first.
* Install the necessary components. For example, under Linux, run:
`$ANDROID_HOME/tools/bin/sdkmanager "platforms;android-30" "build-tools;30.0.2"`
`$ANDROID_HOME/tools/bin/sdkmanager "platforms;android-32" "build-tools;32.0.0"`
* If you skip this step, you will see an error similar to
`ERROR: missing input file '@androidsdk//:build-tools/30.0.2/aapt'`.
`ERROR: missing input file '@androidsdk//:build-tools/32.0.0/aapt'`.
* You may also need to run `bazel sync`.
* Run tests with `bazel test <target>`, or `bazel test //...` to run all
tests.
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
10 changes: 6 additions & 4 deletions examples/bazel/WORKSPACE
Expand Up @@ -41,8 +41,10 @@ load(

android_sdk_repository(
name = "androidsdk",
api_level = 30,
build_tools_version = "30.0.2",
api_level = 32,
# Need to upgrade Bazel before upgrading tools
# https://github.com/bazelbuild/bazel/issues/13989
build_tools_version = "30.0.3",
)

#############################
Expand All @@ -65,9 +67,9 @@ robolectric_repositories()
# Load Maven repositories
#########################

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
2 changes: 1 addition & 1 deletion java/dagger/android/support/BUILD
Expand Up @@ -89,7 +89,7 @@ pom_file(
javadoc_library(
name = "support-javadoc",
srcs = [":support-srcs"],
android_api_level = 30,
android_api_level = 32,
root_packages = ["dagger.android.support"],
deps = [":support"],
)
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
8 changes: 4 additions & 4 deletions java/dagger/hilt/android/plugin/main/build.gradle
Expand Up @@ -65,7 +65,7 @@ dependencies {
testImplementation gradleTestKit()
testImplementation 'junit:junit:4.12'
testImplementation 'com.google.truth:truth:1.0.1'
testPluginCompile 'com.android.tools.build:gradle:4.2.0'
testPluginCompile 'com.android.tools.build:gradle:7.1.2'
}

// Configure the generating task of plugin-under-test-metadata.properties to
Expand All @@ -78,14 +78,14 @@ tasks.withType(PluginUnderTestMetadata.class).named("pluginUnderTestMetadata").c

compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = '11'
allWarningsAsErrors = true
}
}

java {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

// Imports a shared library from the main project. The library and its classes
Expand Down
Expand Up @@ -4,18 +4,18 @@ 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"
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

Expand All @@ -25,4 +25,4 @@ dependencies {

implementation project(':libraryB')
implementation project(':libraryC')
}
}
Expand Up @@ -4,22 +4,22 @@ 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"
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

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,12 +46,12 @@ android {
defaultConfig {
applicationId "simple.app"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

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,16 +45,16 @@ android {

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

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,12 +46,12 @@ android {
defaultConfig {
applicationId "simple.app"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

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,16 +45,16 @@ android {

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
targetSdkVersion 32
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

dependencies {
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'
}
}

0 comments on commit cbc7e5a

Please sign in to comment.