Skip to content

Add hilt-android to the Gradle projects #4738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ kotlin.stdlib.default.dependency=false
# Required by AGP for androidx dependency usages
android.useAndroidX=true

# Required to be disabled by KAPT since the option is deprecated
kapt.include.compile.classpath=false

# Publish version
# TODO(danysantiago): Find a configurable location for the publishing version.
PUBLISH_VERSION=LOCAL-SNAPSHOT
14 changes: 9 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ androidMinSdk = "21"
autoFactory = "1.0"
autoService = "1.0"
autoValue = "1.9"
lifecycle = "2.5.1"
lint = "30.3.1"
guava = "33.0.0-jre"
gradleIncap = "0.2"
Expand All @@ -24,6 +25,9 @@ androidx-annotations = { module = "androidx.annotation:annotation", version = "1
androidx-activity = { module = "androidx.activity:activity", version = "1.5.1" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version = "1.3.1" }
androidx-fragment = { module = "androidx.fragment:fragment", version = "1.5.1" }
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "lifecycle" }
androidx-lifecycle-viewmodel-savedstate = { module = "androidx.lifecycle:lifecycle-viewmodel-savedstate", version.ref = "lifecycle" }
androidx-savedstate = { module = "androidx.savedstate:savedstate", version = "1.2.0" }
androidx-lint = { module = "androidx.lint:lint-gradle", version = "1.0.0-alpha04" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version = "1.2.1" }
auto-common = { module = "com.google.auto:auto-common", version = "1.2.1" }
Expand All @@ -37,15 +41,12 @@ binaryCompatibilityValidatorPlugin = { module = "org.jetbrains.kotlinx:binary-co
checkerFramework = { module = "org.checkerframework:checker-compat-qual", version = "2.5.3" }
errorprone-annotations = { module = "com.google.errorprone:error_prone_annotations", version = "2.23.0" }
findBugs = { module = "com.google.code.findbugs:jsr305", version = "3.0.1" }
javaFormatter = { module = "com.google.googlejavaformat:google-java-format", version = "1.5" }
javaCompileTesting = { module = "com.google.testing.compile:compile-testing", version = "0.18" }
lint-api = { module = "com.android.tools.lint:lint-api", version.ref = "lint" }
lint-checks = { module = "com.android.tools.lint:lint-checks", version.ref = "lint" }
lint-tests = { module = "com.android.tools.lint:lint-tests", version.ref = "lint" }
guava-jre = { module = "com.google.guava:guava", version.ref = "guava" }
guava-failureAccess = { module = "com.google.guava:failureaccess", version = "1.0.1" }
gradleIncap-annotations = { module = "net.ltgt.gradle.incap:incap", version.ref = "gradleIncap" }
gradleIncap-compiler = { module = "net.ltgt.gradle.incap:incap-processor", version.ref = "gradleIncap" }
javaFormatter = { module = "com.google.googlejavaformat:google-java-format", version = "1.5" }
javaCompileTesting = { module = "com.google.testing.compile:compile-testing", version = "0.18" }
jakarta-inject = { module = "jakarta.inject:jakarta.inject-api", version = "2.0.1" }
javaPoet = { module = "com.squareup:javapoet", version = "1.13.0" }
javax-inject = { module = "javax.inject:javax.inject", version = "1" }
Expand All @@ -62,6 +63,9 @@ ksp = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp
ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
ksp-common = { module = "com.google.devtools.ksp:symbol-processing-common-deps", version.ref = "ksp" }
ksp-embeddable = { module = "com.google.devtools.ksp:symbol-processing-aa-embeddable", version.ref = "ksp" }
lint-api = { module = "com.android.tools.lint:lint-api", version.ref = "lint" }
lint-checks = { module = "com.android.tools.lint:lint-checks", version.ref = "lint" }
lint-tests = { module = "com.android.tools.lint:lint-tests", version.ref = "lint" }
publishPlugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "publish" }
robolectric = { module = "org.robolectric:robolectric", version = "4.14.1" }
truth = { module = "com.google.truth:truth", version.ref = "truth" }
Expand Down
5 changes: 2 additions & 3 deletions hilt-android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ gen_maven_artifact(
manifest = "AndroidManifest.xml",
packaging = "aar",
proguard_and_r8_specs = [
"//hilt-core/main/java/dagger/hilt:proguard-rules.pro",
"//hilt-android/main/java/dagger/hilt/android:proguard-rules.pro",
"//hilt-android/main/java/dagger/hilt/internal:proguard-rules.pro",
"//hilt-core/main/resources:proguard",
"//hilt-android/main/resources:proguard",
],
)
567 changes: 567 additions & 0 deletions hilt-android/api/hilt-android.api

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions hilt-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import dagger.gradle.build.SoftwareType
import dagger.gradle.build.findXProcessingJar

plugins {
alias(libs.plugins.daggerBuild)
id(libs.plugins.android.library.get().pluginId)
id(libs.plugins.kotlinAndroid.get().pluginId)
id(libs.plugins.binaryCompatibilityValidator.get().pluginId)
}

dependencies {
api(project(":dagger"))
implementation(project(":dagger-lint-aar"))
api(project(":hilt-core"))

api(libs.androidx.activity)
api(libs.androidx.annotations)
api(libs.androidx.fragment)
api(libs.androidx.lifecycle.viewmodel)
api(libs.androidx.lifecycle.viewmodel.savedstate)
api(libs.androidx.savedstate)
implementation(libs.findBugs)
implementation(libs.kotlin.stdlib)

annotationProcessor(project(":dagger-compiler", "unshaded"))
annotationProcessor(project(":hilt-compiler", "unshaded"))
annotationProcessor(libs.auto.common)
annotationProcessor(files(project.findXProcessingJar()))
}

android {
buildTypes {
defaultConfig {
proguardFiles("$projectDir/main/resources/META-INF/com.android.tools/r8/dagger-android.pro")
}
}
}

daggerBuild {
type = SoftwareType.ANDROID_LIBRARY
isPublished = true
}

android { namespace = "dagger.android" }

kotlin { explicitApi() }
2 changes: 1 addition & 1 deletion hilt-android/main/java/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ android_library(
"//hilt-compiler/main/java/dagger/hilt/processor/internal/aggregateddeps:plugin",
"//hilt-compiler/main/java/dagger/hilt/processor/internal/earlyentrypoint:processor",
],
proguard_specs = ["proguard-rules.pro"],
proguard_specs = ["//hilt-android/main/resources:proguard"],
exports = [
"//hilt-android/main/java/dagger/hilt/android/internal/earlyentrypoint",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,65 @@ import dagger.hilt.EntryPoints
import dagger.hilt.android.internal.Contexts

/** Utility functions for dealing with entry points for standard Android components. */
object EntryPointAccessors {
public object EntryPointAccessors {
/**
* Returns the entry point interface from an application. The context can be any context derived
* from the application context. May only be used with entry point interfaces installed in the
* SingletonComponent.
*/
@JvmStatic
fun <T> fromApplication(context: Context, entryPoint: Class<T>): T =
public fun <T> fromApplication(context: Context, entryPoint: Class<T>): T =
EntryPoints.get(Contexts.getApplication(context.applicationContext), entryPoint)

/**
* Returns the entry point interface from an application. The context can be any context derived
* from the application context. May only be used with entry point interfaces installed in the
* SingletonComponent.
*/
inline fun <reified T> fromApplication(context: Context): T =
public inline fun <reified T> fromApplication(context: Context): T =
fromApplication(context, T::class.java)

/**
* Returns the entry point interface from an activity. May only be used with entry point
* interfaces installed in the ActivityComponent.
*/
@JvmStatic
fun <T> fromActivity(activity: Activity, entryPoint: Class<T>): T =
public fun <T> fromActivity(activity: Activity, entryPoint: Class<T>): T =
EntryPoints.get(activity, entryPoint)

/**
* Returns the entry point interface from an activity. May only be used with entry point
* interfaces installed in the ActivityComponent.
*/
inline fun <reified T> fromActivity(activity: Activity): T =
public inline fun <reified T> fromActivity(activity: Activity): T =
fromActivity(activity, T::class.java)

/**
* Returns the entry point interface from a fragment. May only be used with entry point interfaces
* installed in the FragmentComponent.
*/
@JvmStatic
fun <T> fromFragment(fragment: Fragment, entryPoint: Class<T>): T =
public fun <T> fromFragment(fragment: Fragment, entryPoint: Class<T>): T =
EntryPoints.get(fragment, entryPoint)

/**
* Returns the entry point interface from a fragment. May only be used with entry point interfaces
* installed in the FragmentComponent.
*/
inline fun <reified T> fromFragment(fragment: Fragment): T =
public inline fun <reified T> fromFragment(fragment: Fragment): T =
fromFragment(fragment, T::class.java)

/**
* Returns the entry point interface from a view. May only be used with entry point interfaces
* installed in the ViewComponent or ViewNoFragmentComponent.
*/
@JvmStatic
fun <T> fromView(view: View, entryPoint: Class<T>): T = EntryPoints.get(view, entryPoint)
public fun <T> fromView(view: View, entryPoint: Class<T>): T = EntryPoints.get(view, entryPoint)

/**
* Returns the entry point interface from a view. May only be used with entry point interfaces
* installed in the ViewComponent or ViewNoFragmentComponent.
*/
inline fun <reified T> fromView(view: View): T =
public inline fun <reified T> fromView(view: View): T =
fromView(view, T::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import dagger.hilt.android.internal.lifecycle.HiltViewModelFactory
* @param callback A creation callback that takes an assisted factory and returns a {@code
* ViewModel}.
*/
fun <VMF> CreationExtras.withCreationCallback(callback: (VMF) -> ViewModel): CreationExtras =
public fun <VMF> CreationExtras.withCreationCallback(callback: (VMF) -> ViewModel): CreationExtras =
MutableCreationExtras(this).addCreationCallback(callback)

/**
Expand All @@ -42,7 +42,7 @@ fun <VMF> CreationExtras.withCreationCallback(callback: (VMF) -> ViewModel): Cre
* ViewModel}.
*/
@Suppress("UNCHECKED_CAST")
fun <VMF> MutableCreationExtras.addCreationCallback(callback: (VMF) -> ViewModel): CreationExtras =
public fun <VMF> MutableCreationExtras.addCreationCallback(callback: (VMF) -> ViewModel): CreationExtras =
this.apply {
this[HiltViewModelFactory.CREATION_CALLBACK_KEY] = { factory -> callback(factory as VMF) }
}
4 changes: 2 additions & 2 deletions hilt-android/main/java/dagger/hilt/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ java_library(
java_library(
name = "component_entry_point",
srcs = ["ComponentEntryPoint.java"],
proguard_specs = ["proguard-rules.pro"],
proguard_specs = ["//hilt-android/main/resources:proguard"],
deps = ["//hilt-core/main/java/dagger/hilt:generates_root_input"],
)

java_library(
name = "generated_entry_point",
srcs = ["GeneratedEntryPoint.java"],
proguard_specs = ["proguard-rules.pro"],
proguard_specs = ["//hilt-android/main/resources:proguard"],
deps = ["//hilt-core/main/java/dagger/hilt:generates_root_input"],
)

Expand Down
25 changes: 25 additions & 0 deletions hilt-android/main/resources/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (C) 2025 The Dagger Authors.
#
# 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.

package(default_visibility = ["//:src"])

filegroup(
name = "proguard",
srcs = ["META-INF/com.android.tools/proguard/hilt-android.pro"],
)

filegroup(
name = "r8",
srcs = ["META-INF/com.android.tools/r8/hilt-android.pro"],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Keep for the reflective cast done in EntryPoints.
# See b/183070411#comment4 for more info.
-keep,allowobfuscation,allowshrinking @dagger.hilt.internal.ComponentEntryPoint class *
-keep,allowobfuscation,allowshrinking @dagger.hilt.internal.GeneratedEntryPoint class *
-keep,allowobfuscation,allowshrinking @dagger.hilt.internal.GeneratedEntryPoint class *
-keep,allowobfuscation,allowshrinking @dagger.hilt.android.EarlyEntryPoint class *
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Keep for the reflective cast done in EntryPoints.
# See b/183070411#comment4 for more info.
-keep,allowobfuscation,allowshrinking @dagger.hilt.internal.ComponentEntryPoint class *
-keep,allowobfuscation,allowshrinking @dagger.hilt.internal.GeneratedEntryPoint class *
-keep,allowobfuscation,allowshrinking @dagger.hilt.android.EarlyEntryPoint class *
46 changes: 46 additions & 0 deletions hilt-compiler/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import dagger.gradle.build.SoftwareType
import dagger.gradle.build.findBootstrapCompilerJar
import dagger.gradle.build.findXProcessingJar

plugins {
alias(libs.plugins.daggerBuild)
id(libs.plugins.kotlinJvm.get().pluginId)
id(libs.plugins.kapt.get().pluginId)
id(libs.plugins.shadow.get().pluginId)
}

dependencies {
implementation(project(":dagger"))
implementation(project(":dagger-compiler", "unshaded"))
implementation(project(":dagger-spi", "unshaded"))

implementation(libs.auto.value.annotations)
kapt(libs.auto.value.compiler)
implementation(libs.auto.service.annotations)
kapt(libs.auto.service.compiler)
implementation(libs.findBugs)
implementation(libs.gradleIncap.annotations)
kapt(libs.gradleIncap.compiler)
implementation(libs.guava.failureAccess)
implementation(libs.guava.jre)
implementation(libs.javaPoet)
implementation(libs.ksp.api)
implementation(libs.kotlin.stdlib)
implementation(libs.kotlinPoet.javaPoet)

kapt(files(project.findBootstrapCompilerJar()))

// These dependencies are shaded into dagger-spi
compileOnly(libs.auto.common)
compileOnly(files(project.findXProcessingJar()))
}

daggerBuild {
type = SoftwareType.PROCESSOR
isPublished = true

shading {
relocate("com.google.auto.common", "dagger.spi.internal.shaded.auto.common")
relocate("androidx.room", "dagger.spi.internal.shaded.androidx.room")
}
}
Loading