A custom lint check to detect missing hilt @AndroidEntryPoint issue.
hilt-lint is available on mavenCentral()
.
implementation "io.github.jaychang0917:hilt-lint-api:0.1.0"
lintChecks "io.github.jaychang0917:hilt-lint-checks:0.1.0"
The host Activity must be annotated with @AndroidEntryPoint
if it uses a Hilt Fragment which annotated with @AndroidEntryPoint
. The Fragment(s) declared in source code or xml(i.e. layout and navigation) will be checked.
ExampleActivity.kt:18: Error: com.example.ExampleFragment is annotated with @AndroidEntryPoint, com.example.ExampleActivity must be annotated with @AndroidEntryPoint. [RequiresAndroidEntryPoint]
.replace(R.id.root, ExampleFragment())
~~~~~~~~~~~~~~~~~
example.xml:5: Error: com.example.ExampleFragment is annotated with @AndroidEntryPoint, com.example.ExampleActivity must be annotated with @AndroidEntryPoint. [RequiresAndroidEntryPoint]
<fragment android:id="@+id/example" android:name="com.example.ExampleFragment" />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The host Activity/Fragment must be annotated with @AndroidEntryPoint
if it uses a Hilt View which annotated with @AndroidEntryPoint
. The View(s) declared in source code or xml(i.e. layout) will be checked.
ExampleFragment.kt:14: Error: com.example.ExampleView is annotated with @AndroidEntryPoint, com.example.ExampleFragment must be annotated with @AndroidEntryPoint. [RequiresAndroidEntryPoint]
ExampleView(requireContext())
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fragment_example.xml:8: Error: com.example.ExampleView is annotated with @AndroidEntryPoint, com.example.ExampleFragment must be annotated with @AndroidEntryPoint. [RequiresAndroidEntryPoint]
<com.example.ExampleView
~~~~~~~~~~~~~~~~~~~~~~~~
The Activity/Fragment must be annotated with @AndroidEntryPoint
if it uses a Hilt ViewModel which annotated with @HiltViewModel
.
ExampleFragment.kt:17: Error: com.example.ExampleViewModel is annotated with @HiltViewModel, com.example.ExampleFragment must be annotated with @AndroidEntryPoint. [RequiresAndroidEntryPoint]
private val viewModel by viewModels<ExampleViewModel>()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4. Type declared with @RequiresAndroidEntryPoint must be attached to an @AndroidEntryPoint component.
For polymorphism, we can use @RequiresAndroidEntryPoint
to let the lint know that the usage of this type requires @AndroidEntryPoint
.
interface ExampleApi {
// The concrete implementation is a Hilt Fragment, which requires the host Activity/Fragment
// to be annotated with @AndroidEntryPoint
@RequiresAndroidEntryPoint
fun exampleFragment() : Fragment
}
ExampleActivity.kt:25: Error: com.example.ExampleApi.exampleFragment is annotated with @RequiresAndroidEntryPoint, com.example.ExampleActivity must be annotated with @AndroidEntryPoint. [RequiresAndroidEntryPoint]
val exampleFragment = exampleApi.exampleFragment()
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copyright (C) 2024. Jay Chang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.