blob: 393e2e7c7cea13bca6944f144abb86161f01dad5 [file] [log] [blame] [view]
ronshapiroe2c18ea2019-06-03 12:45:51 -07001# Dagger
Jesse Wilson70eee482012-06-25 12:29:15 -07002
cpovirk3a68b062018-08-20 09:54:35 -07003[![Maven Central][mavenbadge-svg]][mavencentral]
shaunkawanocb4b3ef2016-07-28 12:06:37 -07004
ronshapiroe2c18ea2019-06-03 12:45:51 -07005A fast dependency injector for Java and Android.
Jesse Wilsonaadda232012-08-09 19:25:52 -04006
ronshapiroe2c18ea2019-06-03 12:45:51 -07007Dagger is a compile-time framework for dependency injection. It uses no
8reflection or runtime bytecode generation, does all its analysis at
9compile-time, and generates plain Java source code.
Christian Edward Gruber803309b2013-08-20 12:07:35 -070010
ronshapiroe2c18ea2019-06-03 12:45:51 -070011Dagger is actively maintained by the same team that works on [Guava]. Snapshot
12releases are auto-deployed to Sonatype's central Maven repository on every clean
13build with the version `HEAD-SNAPSHOT`. The current version builds upon previous
14work done at [Square][square].
Christian Edward Gruber40a5b392014-11-14 10:09:53 -080015
cgruber4ab58462016-03-11 12:41:52 -080016## Documentation
Christian Edward Gruber24916c62014-10-17 13:37:19 -070017
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -070018You can [find the dagger documentation here][website] which has extended usage
ronshapiroe2c18ea2019-06-03 12:45:51 -070019instructions and other useful information. More detailed information can be
20found in the [API documentation][latestapi].
Christian Edward Gruber24916c62014-10-17 13:37:19 -070021
cgruber4ab58462016-03-11 12:41:52 -080022You can also learn more from [the original proposal][proposal],
cgrubera7e62852015-05-26 16:01:02 -070023[this talk by Greg Kick][gaktalk], and on the dagger-discuss@googlegroups.com
cgruber4ab58462016-03-11 12:41:52 -080024mailing list.
Jesse Wilsonaadda232012-08-09 19:25:52 -040025
cgruber4ab58462016-03-11 12:41:52 -080026## Installation
Jesse Wilsonaadda232012-08-09 19:25:52 -040027
ronshapiro8ab63f12018-01-16 08:40:08 -080028### Bazel
29
bcorso51c9a1d2020-06-08 17:40:40 -070030First, import the Dagger repository into your `WORKSPACE` file using
bcorsobc6f9a92020-06-17 16:29:02 -070031[`http_archive`][bazel-external-deps].
bcorso51c9a1d2020-06-08 17:40:40 -070032
33Note: The `http_archive` must point to a tagged release of Dagger, not just any
34commit. The version of the Dagger artifacts will match the version of the tagged
35release.
ronshapiro8ab63f12018-01-16 08:40:08 -080036
37```python
bcorso51c9a1d2020-06-08 17:40:40 -070038# Top-level WORKSPACE file
39
bcorsod7fc5ee2020-06-18 17:32:20 -070040load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
41
42DAGGER_TAG = "2.28.1"
bcorso7b83fc72020-06-23 21:24:15 -070043DAGGER_SHA = "9e69ab2f9a47e0f74e71fe49098bea908c528aa02fa0c5995334447b310d0cdd"
ronshapiro8ab63f12018-01-16 08:40:08 -080044http_archive(
bcorso51c9a1d2020-06-08 17:40:40 -070045 name = "dagger",
bcorsod7fc5ee2020-06-18 17:32:20 -070046 strip_prefix = "dagger-dagger-%s" % DAGGER_TAG,
47 sha256 = DAGGER_SHA,
48 urls = ["https://github.com/google/dagger/archive/dagger-%s.zip" % DAGGER_TAG],
ronshapiro8ab63f12018-01-16 08:40:08 -080049)
bcorsobc6f9a92020-06-17 16:29:02 -070050```
51
52Next you will need to setup targets that export the proper dependencies
53and plugins. Follow the sections below to setup the dependencies you need.
54
55#### Dagger Setup
56
57First, load the Dagger artifacts and repositories, and add them to your list of
58[`maven_install`] artifacts.
59
60```python
61# Top-level WORKSPACE file
bcorso51c9a1d2020-06-08 17:40:40 -070062
63load("@dagger//:workspace_defs.bzl", "DAGGER_ARTIFACTS", "DAGGER_REPOSITORIES")
64
65maven_install(
66 artifacts = DAGGER_ARTIFACTS + [...],
67 repositories = DAGGER_REPOSITORIES + [...],
68)
69```
70
bcorsobc6f9a92020-06-17 16:29:02 -070071Next, load and call [`dagger_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl)
72in your top-level `BUILD` file:
bcorso51c9a1d2020-06-08 17:40:40 -070073
74```python
75# Top-level BUILD file
76
77load("@dagger//:workspace_defs.bzl", "dagger_rules")
78
79dagger_rules()
80```
81
bcorsobc6f9a92020-06-17 16:29:02 -070082This will add the following Dagger build targets:
bcorso51c9a1d2020-06-08 17:40:40 -070083(Note that these targets already export all of the dependencies and processors
84they need).
85
86```python
87deps = [
88 ":dagger", # For Dagger
89 ":dagger-spi", # For Dagger SPI
90 ":dagger-producers", # For Dagger Producers
bcorsobc6f9a92020-06-17 16:29:02 -070091]
92```
93
94#### Dagger Android Setup
95
96First, load the Dagger Android artifacts and repositories, and add them to your
97list of [`maven_install`] artifacts.
98
99```python
100# Top-level WORKSPACE file
101
102load(
103 "@dagger//:workspace_defs.bzl",
104 "DAGGER_ANDROID_ARTIFACTS",
105 "DAGGER_ANDROID_REPOSITORIES"
106)
107
108maven_install(
109 artifacts = DAGGER_ANDROID_ARTIFACTS + [...],
110 repositories = DAGGER_ANDROID_REPOSITORIES + [...],
111)
112```
113
114Next, load and call [`dagger_android_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl)
115in your top-level `BUILD` file:
116
117```python
118# Top-level BUILD file
119
120load("@dagger//:workspace_defs.bzl", "dagger_android_rules")
121
122dagger_android_rules()
123```
124
125This will add the following Dagger Android build targets:
126(Note that these targets already export all of the dependencies and processors
127they need).
128
129```python
130deps = [
bcorso51c9a1d2020-06-08 17:40:40 -0700131 ":dagger-android", # For Dagger Android
132 ":dagger-android-support", # For Dagger Android (Support)
bcorsobc6f9a92020-06-17 16:29:02 -0700133]
134```
135
136#### Hilt Android Setup
137
138First, load the Hilt Android artifacts and repositories, and add them to your
139list of [`maven_install`] artifacts.
140
141```python
142# Top-level WORKSPACE file
143
144load(
145 "@dagger//:workspace_defs.bzl",
146 "HILT_ANDROID_ARTIFACTS",
147 "HILT_ANDROID_REPOSITORIES"
148)
149
150maven_install(
151 artifacts = HILT_ANDROID_ARTIFACTS + [...],
152 repositories = HILT_ANDROID_REPOSITORIES + [...],
153)
154```
155
156Next, load and call [`hilt_android_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl)
157in your top-level `BUILD` file:
158
159```python
160# Top-level BUILD file
161
162load("@dagger//:workspace_defs.bzl", "hilt_android_rules")
163
164hilt_android_rules()
165```
166
167This will add the following Hilt Android build targets:
168(Note that these targets already export all of the dependencies and processors
169they need).
170
171```python
172deps = [
bcorso51c9a1d2020-06-08 17:40:40 -0700173 ":hilt-android", # For Hilt Android
174 ":hilt-android-testing", # For Hilt Android Testing
175]
ronshapiro8ab63f12018-01-16 08:40:08 -0800176```
177
178### Other build systems
179
shaunkawanocb4b3ef2016-07-28 12:06:37 -0700180You will need to include the `dagger-2.x.jar` in your application's runtime.
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700181In order to activate code generation and generate implementations to manage
shaunkawanocb4b3ef2016-07-28 12:06:37 -0700182your graph you will need to include `dagger-compiler-2.x.jar` in your build
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700183at compile time.
Jesse Wilsonaadda232012-08-09 19:25:52 -0400184
ronshapiro8ab63f12018-01-16 08:40:08 -0800185#### Maven
kkovachbee27f02016-06-28 07:11:56 -0700186
cgrubera7e62852015-05-26 16:01:02 -0700187In a Maven project, include the `dagger` artifact in the dependencies section
ronshapiro7ef71212017-05-02 14:59:24 -0700188of your `pom.xml` and the `dagger-compiler` artifact as an
189`annotationProcessorPaths` value of the `maven-compiler-plugin`:
Thomas Broyer24a47a72017-05-02 11:32:49 -0700190
191```xml
192<dependencies>
193 <dependency>
194 <groupId>com.google.dagger</groupId>
195 <artifactId>dagger</artifactId>
196 <version>2.x</version>
197 </dependency>
198</dependencies>
199<build>
200 <plugins>
201 <plugin>
202 <groupId>org.apache.maven.plugins</groupId>
203 <artifactId>maven-compiler-plugin</artifactId>
204 <version>3.6.1</version>
205 <configuration>
206 <annotationProcessorPaths>
207 <path>
208 <groupId>com.google.dagger</groupId>
209 <artifactId>dagger-compiler</artifactId>
210 <version>2.x</version>
211 </path>
212 </annotationProcessorPaths>
213 </configuration>
214 </plugin>
215 </plugins>
216</build>
217```
218
ronshapiro7ef71212017-05-02 14:59:24 -0700219If you are using a version of the `maven-compiler-plugin` lower than `3.5`, add
ronshapiro43498a62017-05-03 11:28:04 -0700220the `dagger-compiler` artifact with the `provided` scope:
cgruber92f9d9f2017-08-10 09:57:04 -0700221
ronshapiro7ef71212017-05-02 14:59:24 -0700222```xml
223<dependencies>
224 <dependency>
225 <groupId>com.google.dagger</groupId>
226 <artifactId>dagger</artifactId>
227 <version>2.x</version>
228 </dependency>
229 <dependency>
230 <groupId>com.google.dagger</groupId>
231 <artifactId>dagger-compiler</artifactId>
232 <version>2.x</version>
233 <scope>provided</scope>
234 </dependency>
235</dependencies>
236```
237
cgruber4ab58462016-03-11 12:41:52 -0800238If you use the beta `dagger-producers` extension (which supplies
239parallelizable execution graphs), then add this to your maven configuration:
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700240
241```xml
242<dependencies>
243 <dependency>
244 <groupId>com.google.dagger</groupId>
245 <artifactId>dagger-producers</artifactId>
shaunkawanocb4b3ef2016-07-28 12:06:37 -0700246 <version>2.x</version>
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700247 </dependency>
248</dependencies>
249```
250
ronshapiroc7b83592019-08-23 08:49:53 -0700251#### Gradle
kkovachbee27f02016-06-28 07:11:56 -0700252```groovy
kkovachbee27f02016-06-28 07:11:56 -0700253// Add Dagger dependencies
254dependencies {
Eric Chang9465a0e2021-01-11 13:58:15 -0800255 implementation 'com.google.dagger:dagger:2.x'
Ron Shapirobcef45b2016-11-03 15:32:10 -0400256 annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
kkovachbee27f02016-06-28 07:11:56 -0700257}
258```
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700259
ronshapiroedc12ed2017-04-06 13:49:04 -0700260If you're using classes in `dagger.android` you'll also want to include:
261
262```groovy
Eric Chang9465a0e2021-01-11 13:58:15 -0800263implementation 'com.google.dagger:dagger-android:2.x'
264implementation 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
ronshapiroedc12ed2017-04-06 13:49:04 -0700265annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'
266```
267
ronshapiroc7b83592019-08-23 08:49:53 -0700268Notes:
269
Eric Chang9465a0e2021-01-11 13:58:15 -0800270- We use `implementation` instead of `api` for better compilation performance.
ronshapiroc7b83592019-08-23 08:49:53 -0700271 - See the [Gradle documentation][gradle-api-implementation] for more
272 information on how to select appropriately, and the [Android Gradle
273 plugin documentation][gradle-api-implementation-android] for Android
274 projects.
275- For Kotlin projects, use [`kapt`] in place of `annotationProcessor`.
Ron Shapirobcef45b2016-11-03 15:32:10 -0400276
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800277If you're using the [Android Databinding library][databinding], you may want to
278increase the number of errors that `javac` will print. When Dagger prints an
279error, databinding compilation will halt and sometimes print more than 100
Said Tahsin Dane8c817952017-07-04 08:24:25 -0700280errors, which is the default amount for `javac`. For more information, see
281[Issue 306](https://github.com/google/dagger/issues/306).
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800282
283```groovy
284gradle.projectsEvaluated {
285 tasks.withType(JavaCompile) {
286 options.compilerArgs << "-Xmaxerrs" << "500" // or whatever number you want
287 }
288}
289```
290
ronshapiroe2c18ea2019-06-03 12:45:51 -0700291### Resources
Christian Edward Gruber97cc7612014-10-29 15:52:47 -0700292
ronshapiroe2c18ea2019-06-03 12:45:51 -0700293* [Documentation][website]
294* [Javadocs][latestapi]
295* [GitHub Issues]
Christian Edward Gruber97cc7612014-10-29 15:52:47 -0700296
297
cgruber4ab58462016-03-11 12:41:52 -0800298If you do not use maven, gradle, ivy, or other build systems that consume
299maven-style binary artifacts, they can be downloaded directly via the
cpovirk3a68b062018-08-20 09:54:35 -0700300[Maven Central Repository][mavencentral].
Christian Edward Gruberf0de3942012-10-17 13:05:41 -0400301
cgruber4ab58462016-03-11 12:41:52 -0800302Developer snapshots are available from Sonatype's
303[snapshot repository][dagger-snap], and are built on a clean build of
304the GitHub project's master branch.
Jesse Wilsonbcc571d2012-08-06 17:59:43 -0400305
ronshapirob7ce1dc2017-10-23 08:11:52 -0700306## Building Dagger
307
Pierre-Yves Ricau19f768d2019-04-12 12:43:06 -0700308See [the CONTRIBUTING.md docs][Building Dagger].
ronshapirob7ce1dc2017-10-23 08:11:52 -0700309
cgruber4ab58462016-03-11 12:41:52 -0800310## License
Jesse Wilson70eee482012-06-25 12:29:15 -0700311
isogai.shiraji611ff4f2016-08-24 08:16:19 -0700312 Copyright 2012 The Dagger Authors
Jesse Wilson70eee482012-06-25 12:29:15 -0700313
314 Licensed under the Apache License, Version 2.0 (the "License");
315 you may not use this file except in compliance with the License.
316 You may obtain a copy of the License at
317
318 http://www.apache.org/licenses/LICENSE-2.0
319
320 Unless required by applicable law or agreed to in writing, software
321 distributed under the License is distributed on an "AS IS" BASIS,
322 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
323 See the License for the specific language governing permissions and
324 limitations under the License.
Jesse Wilsonbcc571d2012-08-06 17:59:43 -0400325
ronshapirob7ce1dc2017-10-23 08:11:52 -0700326[`bazel`]: https://bazel.build
ronshapiro8ab63f12018-01-16 08:40:08 -0800327[bazel-external-deps]: https://docs.bazel.build/versions/master/external.html#depending-on-other-bazel-projects
bcorsob181de52020-06-11 14:22:31 -0700328[`maven_install`]: https://github.com/bazelbuild/rules_jvm_external#exporting-and-consuming-artifacts-from-external-repositories
Pierre-Yves Ricau19f768d2019-04-12 12:43:06 -0700329[Building Dagger]: CONTRIBUTING.md#building-dagger
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800330[dagger-snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/dagger/
dpb53677672017-03-14 11:33:48 -0700331[databinding]: https://developer.android.com/topic/libraries/data-binding/
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800332[gaktalk]: https://www.youtube.com/watch?v=oK_XtfXPkqw
ronshapiroe2c18ea2019-06-03 12:45:51 -0700333[GitHub Issues]: https://github.com/google/dagger/issues
ronshapiroc7b83592019-08-23 08:49:53 -0700334[gradle-api-implementation]: https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation
335[gradle-api-implementation-android]: https://developer.android.com/studio/build/dependencies#dependency_configurations
ronshapiroe2c18ea2019-06-03 12:45:51 -0700336[Guava]: https://github.com/google/guava
ronshapiroc7b83592019-08-23 08:49:53 -0700337[`kapt`]: https://kotlinlang.org/docs/reference/kapt.html
ronshapiro072c4772019-05-28 07:55:21 -0700338[latestapi]: https://dagger.dev/api/latest/
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800339[mavenbadge-svg]: https://maven-badges.herokuapp.com/maven-central/com.google.dagger/dagger/badge.svg
cpovirk3a68b062018-08-20 09:54:35 -0700340[mavencentral]: https://search.maven.org/artifact/com.google.dagger/dagger
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800341[project]: http://github.com/google/dagger/
342[proposal]: https://github.com/square/dagger/issues/366
343[square]: http://github.com/square/dagger/
ronshapiro072c4772019-05-28 07:55:21 -0700344[website]: https://dagger.dev