1 /* 2 * Copyright (C) 2020 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.hilt.android; 18 19 import dagger.hilt.GeneratesRootInput; 20 import java.lang.annotation.ElementType; 21 import java.lang.annotation.Retention; 22 import java.lang.annotation.RetentionPolicy; 23 import java.lang.annotation.Target; 24 25 /** 26 * Annotation for marking the {@link android.app.Application} class where the Dagger components 27 * should be generated. Since all components will be built in the same compilation as the annotated 28 * application, all modules and entry points that should be installed in the component need to be 29 * transitive compilation dependencies of the annotated application. 30 * 31 * <p>Usage of this annotation is similar to {@link dagger.hilt.android.AndroidEntryPoint} with the 32 * only difference being that it only works on application classes and additionally triggers Dagger 33 * component generation. 34 * 35 * <p>This annotation will generate a base class that the annotated class should extend, either 36 * directly or via the Hilt Gradle Plugin. This base class will take care of injecting members into 37 * the Android class as well as handling instantiating the proper Hilt components at the right point 38 * in the lifecycle. The name of the base class will be "Hilt_<annotated class name>". 39 * 40 * <p>Example usage (with the Hilt Gradle Plugin): 41 * 42 * <pre><code> 43 * {@literal @}HiltAndroidApp 44 * public final class FooApplication extends Application { 45 * {@literal @}Inject Foo foo; 46 * 47 * {@literal @}Override 48 * public void onCreate() { 49 * super.onCreate(); // The foo field is injected in super.onCreate() 50 * } 51 * } 52 * </code></pre> 53 * 54 * <p>Example usage (without the Hilt Gradle Plugin): 55 * 56 * <pre><code> 57 * {@literal @}HiltAndroidApp(Application.class) 58 * public final class FooApplication extends Hilt_FooApplication { 59 * {@literal @}Inject Foo foo; 60 * 61 * {@literal @}Override 62 * public void onCreate() { 63 * super.onCreate(); // The foo field is injected in super.onCreate() 64 * } 65 * } 66 * </code></pre> 67 * 68 * @see AndroidEntryPoint 69 */ 70 // Set the retention to RUNTIME because we check it via reflection in the HiltAndroidRule. 71 @Retention(RetentionPolicy.RUNTIME) 72 @Target({ElementType.TYPE}) 73 @GeneratesRootInput 74 public @interface HiltAndroidApp { 75 /** 76 * The base class for the generated Hilt application. When applying the Hilt Gradle Plugin this 77 * value is not necessary and will be inferred from the current superclass. 78 */ 79 // TODO(erichang): It would be nice to make this Class<? extends Application> but then the default 80 // would have to be Application which would make the default actually valid even without the 81 // plugin. Maybe that is a good thing...but might be better to have users be explicit about the 82 // base class they want. value()83 Class<?> value() default Void.class; 84 } 85