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