blob: a9ebc26eb5401ed66da34c938259cea39b0e900d [file] [log] [blame] [view]
cgruber4ab58462016-03-11 12:41:52 -08001# Dagger 2
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
Jesse Wilsonaadda232012-08-09 19:25:52 -04005A fast dependency injector for Android and Java.
6
cgruber4ab58462016-03-11 12:41:52 -08007## About Google's Fork
Christian Edward Gruber803309b2013-08-20 12:07:35 -07008
cgruber4ab58462016-03-11 12:41:52 -08009Dagger 2 is a compile-time evolution approach to dependency injection.
10Taking the approach started in Dagger 1.x to its ultimate conclusion,
11Dagger 2.x eliminates all reflection, and improves code clarity by
12removing the traditional ObjectGraph/Injector in favor of user-specified
13`@Component` interfaces.
Christian Edward Gruber803309b2013-08-20 12:07:35 -070014
cgruber4ab58462016-03-11 12:41:52 -080015This github project represents the Dagger 2 development stream. The earlier
16[project page][square] (Square, Inc's repository) represents the earlier 1.0
Robert Pocklington2a59b142018-08-01 07:48:32 -070017development stream. Both versions have benefited from strong involvement from
cgruber4ab58462016-03-11 12:41:52 -080018Square, Google, and other contributors.
Christian Edward Gruber803309b2013-08-20 12:07:35 -070019
cgrubered47efc2016-07-28 13:45:54 -070020Dagger is currently in active development, primarily internally at Google,
21with regular pushes to the open-source community. Snapshot releases are
22auto-deployed to sonatype's central maven repository on every clean build with
23the version `HEAD-SNAPSHOT`.
24
cgruber4ab58462016-03-11 12:41:52 -080025> [Dagger 2's main documentation website can be found here.][website]
Christian Edward Gruber40a5b392014-11-14 10:09:53 -080026
cgruber4ab58462016-03-11 12:41:52 -080027## Documentation
Christian Edward Gruber24916c62014-10-17 13:37:19 -070028
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -070029You can [find the dagger documentation here][website] which has extended usage
30instructions and other useful information. Substantial usage information can be
31found in the [API documentation][20api].
Christian Edward Gruber24916c62014-10-17 13:37:19 -070032
cgruber4ab58462016-03-11 12:41:52 -080033You can also learn more from [the original proposal][proposal],
cgrubera7e62852015-05-26 16:01:02 -070034[this talk by Greg Kick][gaktalk], and on the dagger-discuss@googlegroups.com
cgruber4ab58462016-03-11 12:41:52 -080035mailing list.
Jesse Wilsonaadda232012-08-09 19:25:52 -040036
cgruber4ab58462016-03-11 12:41:52 -080037## Installation
Jesse Wilsonaadda232012-08-09 19:25:52 -040038
ronshapiro8ab63f12018-01-16 08:40:08 -080039### Bazel
40
ronshapiro5715fa72018-01-16 14:51:16 -080041If you build with `bazel`, follow the [`bazel` documentation for referencing
ronshapiro8ab63f12018-01-16 08:40:08 -080042external projects][bazel-external-deps] to include Dagger in your build.
43
44Given the following `WORKSPACE` definition, you can reference dagger via
ronshapiro5715fa72018-01-16 14:51:16 -080045`@com_google_dagger//:dagger_with_compiler` in your deps.
ronshapiro8ab63f12018-01-16 08:40:08 -080046
47```python
48http_archive(
cgruber45f931a2018-08-07 11:28:35 -070049 name = "com_google_dagger",
ronshapiro8ab63f12018-01-16 08:40:08 -080050 urls = ["https://github.com/google/dagger/archive/dagger-<version>.zip"],
51)
52```
53
54### Other build systems
55
shaunkawanocb4b3ef2016-07-28 12:06:37 -070056You will need to include the `dagger-2.x.jar` in your application's runtime.
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -070057In order to activate code generation and generate implementations to manage
shaunkawanocb4b3ef2016-07-28 12:06:37 -070058your graph you will need to include `dagger-compiler-2.x.jar` in your build
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -070059at compile time.
Jesse Wilsonaadda232012-08-09 19:25:52 -040060
ronshapiro8ab63f12018-01-16 08:40:08 -080061#### Maven
kkovachbee27f02016-06-28 07:11:56 -070062
cgrubera7e62852015-05-26 16:01:02 -070063In a Maven project, include the `dagger` artifact in the dependencies section
ronshapiro7ef71212017-05-02 14:59:24 -070064of your `pom.xml` and the `dagger-compiler` artifact as an
65`annotationProcessorPaths` value of the `maven-compiler-plugin`:
Thomas Broyer24a47a72017-05-02 11:32:49 -070066
67```xml
68<dependencies>
69 <dependency>
70 <groupId>com.google.dagger</groupId>
71 <artifactId>dagger</artifactId>
72 <version>2.x</version>
73 </dependency>
74</dependencies>
75<build>
76 <plugins>
77 <plugin>
78 <groupId>org.apache.maven.plugins</groupId>
79 <artifactId>maven-compiler-plugin</artifactId>
80 <version>3.6.1</version>
81 <configuration>
82 <annotationProcessorPaths>
83 <path>
84 <groupId>com.google.dagger</groupId>
85 <artifactId>dagger-compiler</artifactId>
86 <version>2.x</version>
87 </path>
88 </annotationProcessorPaths>
89 </configuration>
90 </plugin>
91 </plugins>
92</build>
93```
94
ronshapiro7ef71212017-05-02 14:59:24 -070095If you are using a version of the `maven-compiler-plugin` lower than `3.5`, add
ronshapiro43498a62017-05-03 11:28:04 -070096the `dagger-compiler` artifact with the `provided` scope:
cgruber92f9d9f2017-08-10 09:57:04 -070097
ronshapiro7ef71212017-05-02 14:59:24 -070098```xml
99<dependencies>
100 <dependency>
101 <groupId>com.google.dagger</groupId>
102 <artifactId>dagger</artifactId>
103 <version>2.x</version>
104 </dependency>
105 <dependency>
106 <groupId>com.google.dagger</groupId>
107 <artifactId>dagger-compiler</artifactId>
108 <version>2.x</version>
109 <scope>provided</scope>
110 </dependency>
111</dependencies>
112```
113
cgruber4ab58462016-03-11 12:41:52 -0800114If you use the beta `dagger-producers` extension (which supplies
115parallelizable execution graphs), then add this to your maven configuration:
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700116
117```xml
118<dependencies>
119 <dependency>
120 <groupId>com.google.dagger</groupId>
121 <artifactId>dagger-producers</artifactId>
shaunkawanocb4b3ef2016-07-28 12:06:37 -0700122 <version>2.x</version>
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700123 </dependency>
124</dependencies>
125```
126
ronshapiro8ab63f12018-01-16 08:40:08 -0800127#### Java Gradle
kkovachbee27f02016-06-28 07:11:56 -0700128```groovy
129// Add plugin https://plugins.gradle.org/plugin/net.ltgt.apt
130plugins {
Thomas Broyer24a47a72017-05-02 11:32:49 -0700131 id "net.ltgt.apt" version "0.10"
kkovachbee27f02016-06-28 07:11:56 -0700132}
133
134// Add Dagger dependencies
135dependencies {
cgrubered47efc2016-07-28 13:45:54 -0700136 compile 'com.google.dagger:dagger:2.x'
137 apt 'com.google.dagger:dagger-compiler:2.x'
kkovachbee27f02016-06-28 07:11:56 -0700138}
139```
140
ronshapiro8ab63f12018-01-16 08:40:08 -0800141#### Android Gradle
kkovachbee27f02016-06-28 07:11:56 -0700142```groovy
kkovachbee27f02016-06-28 07:11:56 -0700143// Add Dagger dependencies
144dependencies {
cgrubered47efc2016-07-28 13:45:54 -0700145 compile 'com.google.dagger:dagger:2.x'
Ron Shapirobcef45b2016-11-03 15:32:10 -0400146 annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
kkovachbee27f02016-06-28 07:11:56 -0700147}
148```
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700149
ronshapiroedc12ed2017-04-06 13:49:04 -0700150If you're using classes in `dagger.android` you'll also want to include:
151
152```groovy
153compile 'com.google.dagger:dagger-android:2.x'
154compile 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
155annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'
156```
157
Ron Shapirobcef45b2016-11-03 15:32:10 -0400158If you're using a version of the Android gradle plugin below `2.2`, see
159https://bitbucket.org/hvisser/android-apt.
160
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800161If you're using the [Android Databinding library][databinding], you may want to
162increase the number of errors that `javac` will print. When Dagger prints an
163error, databinding compilation will halt and sometimes print more than 100
Said Tahsin Dane8c817952017-07-04 08:24:25 -0700164errors, which is the default amount for `javac`. For more information, see
165[Issue 306](https://github.com/google/dagger/issues/306).
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800166
167```groovy
168gradle.projectsEvaluated {
169 tasks.withType(JavaCompile) {
170 options.compilerArgs << "-Xmaxerrs" << "500" // or whatever number you want
171 }
172}
173```
174
cgruber4ab58462016-03-11 12:41:52 -0800175### Download
Christian Edward Gruber97cc7612014-10-29 15:52:47 -0700176
Christian Edward Grubereb752622015-06-01 00:19:00 -0700177 * 2.x (google/dagger)
Christian Edward Gruber40a5b392014-11-14 10:09:53 -0800178 * [Dagger 2.0 Documentation][website]
Christian Edward Gruberddbe7f12015-04-07 18:49:47 -0700179 * [Dagger 2.0 Javadocs][20api]
cgruber4ab58462016-03-11 12:41:52 -0800180 * [Dagger development Javadocs][latestapi] (from the `master` branch
181 on GitHub)
Christian Edward Gruber24916c62014-10-17 13:37:19 -0700182 * [Google's Dagger project site on GitHub][project]
Christian Edward Grubereb752622015-06-01 00:19:00 -0700183 * 1.x (square/dagger)
Christian Edward Gruber24916c62014-10-17 13:37:19 -0700184 * [Square's original Dagger project site on GitHub][square]
Christian Edward Gruber97cc7612014-10-29 15:52:47 -0700185
186
cgruber4ab58462016-03-11 12:41:52 -0800187If you do not use maven, gradle, ivy, or other build systems that consume
188maven-style binary artifacts, they can be downloaded directly via the
cpovirk3a68b062018-08-20 09:54:35 -0700189[Maven Central Repository][mavencentral].
Christian Edward Gruberf0de3942012-10-17 13:05:41 -0400190
cgruber4ab58462016-03-11 12:41:52 -0800191Developer snapshots are available from Sonatype's
192[snapshot repository][dagger-snap], and are built on a clean build of
193the GitHub project's master branch.
Jesse Wilsonbcc571d2012-08-06 17:59:43 -0400194
ronshapirob7ce1dc2017-10-23 08:11:52 -0700195## Building Dagger
196
Pierre-Yves Ricau19f768d2019-04-12 12:43:06 -0700197See [the CONTRIBUTING.md docs][Building Dagger].
ronshapirob7ce1dc2017-10-23 08:11:52 -0700198
cgruber4ab58462016-03-11 12:41:52 -0800199## License
Jesse Wilson70eee482012-06-25 12:29:15 -0700200
isogai.shiraji611ff4f2016-08-24 08:16:19 -0700201 Copyright 2012 The Dagger Authors
Jesse Wilson70eee482012-06-25 12:29:15 -0700202
203 Licensed under the Apache License, Version 2.0 (the "License");
204 you may not use this file except in compliance with the License.
205 You may obtain a copy of the License at
206
207 http://www.apache.org/licenses/LICENSE-2.0
208
209 Unless required by applicable law or agreed to in writing, software
210 distributed under the License is distributed on an "AS IS" BASIS,
211 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
212 See the License for the specific language governing permissions and
213 limitations under the License.
Jesse Wilsonbcc571d2012-08-06 17:59:43 -0400214
ronshapiro072c4772019-05-28 07:55:21 -0700215[20api]: https://dagger.dev/api/2.0/
ronshapirob7ce1dc2017-10-23 08:11:52 -0700216[`bazel`]: https://bazel.build
ronshapiro8ab63f12018-01-16 08:40:08 -0800217[bazel-external-deps]: https://docs.bazel.build/versions/master/external.html#depending-on-other-bazel-projects
Pierre-Yves Ricau19f768d2019-04-12 12:43:06 -0700218[Building Dagger]: CONTRIBUTING.md#building-dagger
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800219[dagger-snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/dagger/
dpb53677672017-03-14 11:33:48 -0700220[databinding]: https://developer.android.com/topic/libraries/data-binding/
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800221[gaktalk]: https://www.youtube.com/watch?v=oK_XtfXPkqw
ronshapiro072c4772019-05-28 07:55:21 -0700222[latestapi]: https://dagger.dev/api/latest/
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800223[mavenbadge-svg]: https://maven-badges.herokuapp.com/maven-central/com.google.dagger/dagger/badge.svg
cpovirk3a68b062018-08-20 09:54:35 -0700224[mavencentral]: https://search.maven.org/artifact/com.google.dagger/dagger
ronshapiro9ad2f3d2016-12-19 09:07:31 -0800225[project]: http://github.com/google/dagger/
226[proposal]: https://github.com/square/dagger/issues/366
227[square]: http://github.com/square/dagger/
ronshapiro072c4772019-05-28 07:55:21 -0700228[website]: https://dagger.dev