Skip to content

Commit

Permalink
Allow obfuscating @HiltViewModel annotated ViewModel name with r8.
Browse files Browse the repository at this point in the history
RELNOTES=Allow obfuscating @HiltViewModel annotated ViewModel name with r8.
PiperOrigin-RevId: 610474363
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Feb 26, 2024
1 parent c5075df commit 0786d0a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 38 deletions.
1 change: 0 additions & 1 deletion java/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ gen_maven_artifact(
proguard_and_r8_specs = [
"//java/dagger/hilt:proguard-rules.pro",
"//java/dagger/hilt/android:proguard-rules.pro",
"//java/dagger/hilt/android/lifecycle:proguard-rules.pro",
"//java/dagger/hilt/internal:proguard-rules.pro",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import dagger.hilt.android.components.FragmentComponent;
import dagger.hilt.android.internal.builders.ViewModelComponentBuilder;
import dagger.multibindings.Multibinds;
import java.util.Set;
import java.util.Map;
import javax.inject.Inject;

/**
Expand Down Expand Up @@ -69,12 +69,12 @@ public static ViewModelProvider.Factory getFragmentFactory(
/** Internal factory for the Hilt ViewModel Factory. */
public static final class InternalFactoryFactory {

private final Set<String> keySet;
private final Map<Class<?>, Boolean> keySet;
private final ViewModelComponentBuilder viewModelComponentBuilder;

@Inject
InternalFactoryFactory(
@HiltViewModelMap.KeySet Set<String> keySet,
@HiltViewModelMap.KeySet Map<Class<?>, Boolean> keySet,
ViewModelComponentBuilder viewModelComponentBuilder) {
this.keySet = keySet;
this.viewModelComponentBuilder = viewModelComponentBuilder;
Expand Down Expand Up @@ -103,7 +103,7 @@ private ViewModelProvider.Factory getHiltViewModelFactory(
interface ActivityModule {
@Multibinds
@HiltViewModelMap.KeySet
abstract Set<String> viewModelKeys();
abstract Map<Class<?>, Boolean> viewModelKeys();
}

/** The activity entry point to retrieve the factory. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import dagger.hilt.android.internal.builders.ViewModelComponentBuilder;
import dagger.multibindings.Multibinds;
import java.util.Map;
import java.util.Set;
import javax.inject.Provider;
import kotlin.jvm.functions.Function1;

Expand All @@ -55,11 +54,11 @@ public final class HiltViewModelFactory implements ViewModelProvider.Factory {
@InstallIn(ViewModelComponent.class)
public interface ViewModelFactoriesEntryPoint {
@HiltViewModelMap
Map<String, Provider<ViewModel>> getHiltViewModelMap();
Map<Class<?>, Provider<ViewModel>> getHiltViewModelMap();

// From ViewModel class names to user defined @AssistedFactory-annotated implementations.
@HiltViewModelAssistedMap
Map<String, Object> getHiltViewModelAssistedMap();
Map<Class<?>, Object> getHiltViewModelAssistedMap();
}

/** Creation extra key for the callbacks that create @AssistedInject-annotated ViewModels. */
Expand All @@ -72,19 +71,19 @@ public interface ViewModelFactoriesEntryPoint {
interface ViewModelModule {
@Multibinds
@HiltViewModelMap
Map<String, ViewModel> hiltViewModelMap();
Map<Class<?>, ViewModel> hiltViewModelMap();

@Multibinds
@HiltViewModelAssistedMap
Map<String, Object> hiltViewModelAssistedMap();
Map<Class<?>, Object> hiltViewModelAssistedMap();
}

private final Set<String> hiltViewModelKeys;
private final Map<Class<?>, Boolean> hiltViewModelKeys;
private final ViewModelProvider.Factory delegateFactory;
private final ViewModelProvider.Factory hiltViewModelFactory;

public HiltViewModelFactory(
@NonNull Set<String> hiltViewModelKeys,
@NonNull Map<Class<?>, Boolean> hiltViewModelKeys,
@NonNull ViewModelProvider.Factory delegateFactory,
@NonNull ViewModelComponentBuilder viewModelComponentBuilder) {
this.hiltViewModelKeys = hiltViewModelKeys;
Expand Down Expand Up @@ -113,12 +112,12 @@ private <T extends ViewModel> T createViewModel(
Provider<? extends ViewModel> provider =
EntryPoints.get(component, ViewModelFactoriesEntryPoint.class)
.getHiltViewModelMap()
.get(modelClass.getName());
.get(modelClass);
Function1<Object, ViewModel> creationCallback = extras.get(CREATION_CALLBACK_KEY);
Object assistedFactory =
EntryPoints.get(component, ViewModelFactoriesEntryPoint.class)
.getHiltViewModelAssistedMap()
.get(modelClass.getName());
.get(modelClass);

if (assistedFactory == null) {
if (creationCallback == null) {
Expand Down Expand Up @@ -167,7 +166,7 @@ private <T extends ViewModel> T createViewModel(
@Override
public <T extends ViewModel> T create(
@NonNull Class<T> modelClass, @NonNull CreationExtras extras) {
if (hiltViewModelKeys.contains(modelClass.getName())) {
if (hiltViewModelKeys.containsKey(modelClass)) {
return hiltViewModelFactory.create(modelClass, extras);
} else {
return delegateFactory.create(modelClass, extras);
Expand All @@ -177,7 +176,7 @@ public <T extends ViewModel> T create(
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (hiltViewModelKeys.contains(modelClass.getName())) {
if (hiltViewModelKeys.containsKey(modelClass)) {
return hiltViewModelFactory.create(modelClass);
} else {
return delegateFactory.create(modelClass);
Expand All @@ -188,7 +187,8 @@ public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
@InstallIn(ActivityComponent.class)
interface ActivityCreatorEntryPoint {
@HiltViewModelMap.KeySet
Set<String> getViewModelKeys();
Map<Class<?>, Boolean> getViewModelKeys();

ViewModelComponentBuilder getViewModelComponentBuilder();
}

Expand Down
1 change: 0 additions & 1 deletion java/dagger/hilt/android/lifecycle/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ android_library(
exported_plugins = [
"//java/dagger/hilt/android/processor/internal/viewmodel:processor",
],
proguard_specs = ["proguard-rules.pro"],
exports = [
"//:dagger_with_compiler",
"//java/dagger/hilt:install_in",
Expand Down
2 changes: 0 additions & 2 deletions java/dagger/hilt/android/lifecycle/proguard-rules.pro

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ import javax.lang.model.element.Modifier
* public static abstract class BindsModule {
* @Binds
* @IntoMap
* @StringKey("pkg.$")
* @LazyClassKey(pkg.$)
* @HiltViewModelMap
* public abstract ViewModel bind($ vm)
* }
* @Module
* @InstallIn(ActivityRetainedComponent.class)
* public static final class KeyModule {
* private static String className = "pkg.$";
* @Provides
* @IntoSet
* @IntoMap
* @HiltViewModelMap.KeySet
* public static String provide() {
* return "pkg.$";
* @LazyClassKey(pkg.$)
* public static boolean provide() {
* return true;
* }
* }
* }
Expand All @@ -62,7 +64,7 @@ import javax.lang.model.element.Modifier
@OptIn(ExperimentalProcessingApi::class)
internal class ViewModelModuleGenerator(
private val processingEnv: XProcessingEnv,
private val viewModelMetadata: ViewModelMetadata
private val viewModelMetadata: ViewModelMetadata,
) {
fun generate() {
val modulesTypeSpec =
Expand All @@ -75,7 +77,7 @@ internal class ViewModelModuleGenerator(
.addMember(
"topLevelClass",
"$T.class",
viewModelMetadata.className.topLevelClassName()
viewModelMetadata.className.topLevelClassName(),
)
.build()
)
Expand All @@ -94,7 +96,7 @@ internal class ViewModelModuleGenerator(
private fun getBindsModuleTypeSpec() =
createModuleTypeSpec(
className = "BindsModule",
component = AndroidClassNames.VIEW_MODEL_COMPONENT
component = AndroidClassNames.VIEW_MODEL_COMPONENT,
)
.addModifiers(Modifier.ABSTRACT)
.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build())
Expand All @@ -112,8 +114,8 @@ internal class ViewModelModuleGenerator(
.addAnnotation(ClassNames.BINDS)
.addAnnotation(ClassNames.INTO_MAP)
.addAnnotation(
AnnotationSpec.builder(ClassNames.STRING_KEY)
.addMember("value", S, viewModelMetadata.className.reflectionName())
AnnotationSpec.builder(ClassNames.LAZY_CLASS_KEY)
.addMember("value", "$T.class", viewModelMetadata.className)
.build()
)
.addAnnotation(AndroidClassNames.HILT_VIEW_MODEL_MAP_QUALIFIER)
Expand All @@ -125,7 +127,7 @@ internal class ViewModelModuleGenerator(
private fun getKeyModuleTypeSpec() =
createModuleTypeSpec(
className = "KeyModule",
component = AndroidClassNames.ACTIVITY_RETAINED_COMPONENT
component = AndroidClassNames.ACTIVITY_RETAINED_COMPONENT,
)
.addModifiers(Modifier.FINAL)
.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build())
Expand All @@ -135,19 +137,24 @@ internal class ViewModelModuleGenerator(
private fun getViewModelKeyProvidesMethod() =
MethodSpec.methodBuilder("provide")
.addAnnotation(ClassNames.PROVIDES)
.addAnnotation(ClassNames.INTO_SET)
.addAnnotation(ClassNames.INTO_MAP)
.addAnnotation(
AnnotationSpec.builder(ClassNames.LAZY_CLASS_KEY)
.addMember("value", "$T.class", viewModelMetadata.className)
.build()
)
.addAnnotation(AndroidClassNames.HILT_VIEW_MODEL_KEYS_QUALIFIER)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(String::class.java)
.addStatement("return $S", viewModelMetadata.className.reflectionName())
.returns(Boolean::class.java)
.addStatement("return true")
.build()

/**
* Should generate:
* ```
* @Binds
* @IntoMap
* @StringKey("pkg.FooViewModel")
* @LazyClassKey(pkg.FooViewModel.class)
* @HiltViewModelAssistedMap
* public abstract Object bind(FooViewModelAssistedFactory factory);
* ```
Expand All @@ -160,8 +167,8 @@ internal class ViewModelModuleGenerator(
.addAnnotation(ClassNames.BINDS)
.addAnnotation(ClassNames.INTO_MAP)
.addAnnotation(
AnnotationSpec.builder(ClassNames.STRING_KEY)
.addMember("value", S, viewModelMetadata.className.reflectionName())
AnnotationSpec.builder(ClassNames.LAZY_CLASS_KEY)
.addMember("value", "$T.class", viewModelMetadata.className)
.build()
)
.addAnnotation(AndroidClassNames.HILT_VIEW_MODEL_ASSISTED_FACTORY_MAP_QUALIFIER)
Expand Down
4 changes: 4 additions & 0 deletions java/dagger/hilt/processor/internal/ClassNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public final class ClassNames {
public static final ClassName DEFINE_COMPONENT_CLASSES =
get("dagger.hilt.internal.definecomponent", "DefineComponentClasses");

public static final ClassName IDENTIFIER_NAME_STRING =
get("dagger.internal", "IdentifierNameString");

public static final ClassName ASSISTED_INJECT = get("dagger.assisted", "AssistedInject");
public static final ClassName ASSISTED_FACTORY = get("dagger.assisted", "AssistedFactory");
public static final ClassName BINDS =
Expand All @@ -86,6 +89,7 @@ public final class ClassNames {
public static final ClassName INTO_SET = get("dagger.multibindings", "IntoSet");
public static final ClassName ELEMENTS_INTO_SET = get("dagger.multibindings", "ElementsIntoSet");
public static final ClassName STRING_KEY = get("dagger.multibindings", "StringKey");
public static final ClassName LAZY_CLASS_KEY = get("dagger.multibindings", "LazyClassKey");
public static final ClassName PROVIDES =
get("dagger", "Provides");
public static final ClassName COMPONENT = get("dagger", "Component");
Expand Down
5 changes: 3 additions & 2 deletions java/dagger/internal/IdentifierNameString.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
/**
* Annotates the dagger generated class that requires applying -identifiernamestring rule.
*
* <p>When applied, all the strings that corresponds to a class name within the annotated class will
* be obfuscated if its corresponding class is obfuscated. This only works with r8. This annotation
* <p>When applied, all the strings fields that corresponds to a class name within the annotated
* class will be obfuscated if its corresponding class is obfuscated. This only works with r8.
*
*/
@Documented
@Retention(CLASS)
Expand Down

0 comments on commit 0786d0a

Please sign in to comment.