Skip to content

Commit

Permalink
Allow obfuscating HiltViewModel class names.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 605426702
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Feb 9, 2024
1 parent c872238 commit 1ece31e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 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 @@ -55,11 +55,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,11 +72,11 @@ 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;
Expand Down Expand Up @@ -113,12 +113,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
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 @@ -22,6 +22,7 @@ import androidx.room.compiler.processing.XProcessingEnv
import androidx.room.compiler.processing.addOriginatingElement
import com.squareup.javapoet.AnnotationSpec
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.FieldSpec
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.TypeName
Expand All @@ -42,18 +43,19 @@ 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
* @HiltViewModelMap.KeySet
* public static String provide() {
* return "pkg.$";
* return className;
* }
* }
* }
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,11 +127,17 @@ internal class ViewModelModuleGenerator(
private fun getKeyModuleTypeSpec() =
createModuleTypeSpec(
className = "KeyModule",
component = AndroidClassNames.ACTIVITY_RETAINED_COMPONENT
component = AndroidClassNames.ACTIVITY_RETAINED_COMPONENT,
)
.addModifiers(Modifier.FINAL)
.addAnnotation(ClassNames.IDENTIFIER_NAME_STRING)
.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build())
.addMethod(getViewModelKeyProvidesMethod())
.addField(
FieldSpec.builder(String::class.java, "className", Modifier.PRIVATE, Modifier.STATIC)
.initializer(S, viewModelMetadata.className.reflectionName())
.build()
)
.build()

private fun getViewModelKeyProvidesMethod() =
Expand All @@ -139,15 +147,15 @@ internal class ViewModelModuleGenerator(
.addAnnotation(AndroidClassNames.HILT_VIEW_MODEL_KEYS_QUALIFIER)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(String::class.java)
.addStatement("return $S", viewModelMetadata.className.reflectionName())
.addStatement("return className")
.build()

/**
* Should generate:
* ```
* @Binds
* @IntoMap
* @StringKey("pkg.FooViewModel")
* @LazyClassKey(pkg.FooViewModel.class)
* @HiltViewModelAssistedMap
* public abstract Object bind(FooViewModelAssistedFactory factory);
* ```
Expand All @@ -160,8 +168,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 1ece31e

Please sign in to comment.