cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 1 | # Dagger 2 |
Jesse Wilson | 70eee48 | 2012-06-25 12:29:15 -0700 | [diff] [blame] | 2 | |
cpovirk | 3a68b06 | 2018-08-20 09:54:35 -0700 | [diff] [blame] | 3 | [![Maven Central][mavenbadge-svg]][mavencentral] |
shaunkawano | cb4b3ef | 2016-07-28 12:06:37 -0700 | [diff] [blame] | 4 | |
Jesse Wilson | aadda23 | 2012-08-09 19:25:52 -0400 | [diff] [blame] | 5 | A fast dependency injector for Android and Java. |
| 6 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 7 | ## About Google's Fork |
Christian Edward Gruber | 803309b | 2013-08-20 12:07:35 -0700 | [diff] [blame] | 8 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 9 | Dagger 2 is a compile-time evolution approach to dependency injection. |
| 10 | Taking the approach started in Dagger 1.x to its ultimate conclusion, |
| 11 | Dagger 2.x eliminates all reflection, and improves code clarity by |
| 12 | removing the traditional ObjectGraph/Injector in favor of user-specified |
| 13 | `@Component` interfaces. |
Christian Edward Gruber | 803309b | 2013-08-20 12:07:35 -0700 | [diff] [blame] | 14 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 15 | This github project represents the Dagger 2 development stream. The earlier |
| 16 | [project page][square] (Square, Inc's repository) represents the earlier 1.0 |
Robert Pocklington | 2a59b14 | 2018-08-01 07:48:32 -0700 | [diff] [blame] | 17 | development stream. Both versions have benefited from strong involvement from |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 18 | Square, Google, and other contributors. |
Christian Edward Gruber | 803309b | 2013-08-20 12:07:35 -0700 | [diff] [blame] | 19 | |
cgruber | ed47efc | 2016-07-28 13:45:54 -0700 | [diff] [blame] | 20 | Dagger is currently in active development, primarily internally at Google, |
| 21 | with regular pushes to the open-source community. Snapshot releases are |
| 22 | auto-deployed to sonatype's central maven repository on every clean build with |
| 23 | the version `HEAD-SNAPSHOT`. |
| 24 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 25 | > [Dagger 2's main documentation website can be found here.][website] |
Christian Edward Gruber | 40a5b39 | 2014-11-14 10:09:53 -0800 | [diff] [blame] | 26 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 27 | ## Documentation |
Christian Edward Gruber | 24916c6 | 2014-10-17 13:37:19 -0700 | [diff] [blame] | 28 | |
Christian Edward Gruber | ddbe7f1 | 2015-04-07 18:49:47 -0700 | [diff] [blame] | 29 | You can [find the dagger documentation here][website] which has extended usage |
| 30 | instructions and other useful information. Substantial usage information can be |
| 31 | found in the [API documentation][20api]. |
Christian Edward Gruber | 24916c6 | 2014-10-17 13:37:19 -0700 | [diff] [blame] | 32 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 33 | You can also learn more from [the original proposal][proposal], |
cgruber | a7e6285 | 2015-05-26 16:01:02 -0700 | [diff] [blame] | 34 | [this talk by Greg Kick][gaktalk], and on the dagger-discuss@googlegroups.com |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 35 | mailing list. |
Jesse Wilson | aadda23 | 2012-08-09 19:25:52 -0400 | [diff] [blame] | 36 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 37 | ## Installation |
Jesse Wilson | aadda23 | 2012-08-09 19:25:52 -0400 | [diff] [blame] | 38 | |
ronshapiro | 8ab63f1 | 2018-01-16 08:40:08 -0800 | [diff] [blame] | 39 | ### Bazel |
| 40 | |
ronshapiro | 5715fa7 | 2018-01-16 14:51:16 -0800 | [diff] [blame] | 41 | If you build with `bazel`, follow the [`bazel` documentation for referencing |
ronshapiro | 8ab63f1 | 2018-01-16 08:40:08 -0800 | [diff] [blame] | 42 | external projects][bazel-external-deps] to include Dagger in your build. |
| 43 | |
| 44 | Given the following `WORKSPACE` definition, you can reference dagger via |
ronshapiro | 5715fa7 | 2018-01-16 14:51:16 -0800 | [diff] [blame] | 45 | `@com_google_dagger//:dagger_with_compiler` in your deps. |
ronshapiro | 8ab63f1 | 2018-01-16 08:40:08 -0800 | [diff] [blame] | 46 | |
| 47 | ```python |
| 48 | http_archive( |
cgruber | 45f931a | 2018-08-07 11:28:35 -0700 | [diff] [blame] | 49 | name = "com_google_dagger", |
ronshapiro | 8ab63f1 | 2018-01-16 08:40:08 -0800 | [diff] [blame] | 50 | urls = ["https://github.com/google/dagger/archive/dagger-<version>.zip"], |
| 51 | ) |
| 52 | ``` |
| 53 | |
| 54 | ### Other build systems |
| 55 | |
shaunkawano | cb4b3ef | 2016-07-28 12:06:37 -0700 | [diff] [blame] | 56 | You will need to include the `dagger-2.x.jar` in your application's runtime. |
Christian Edward Gruber | ddbe7f1 | 2015-04-07 18:49:47 -0700 | [diff] [blame] | 57 | In order to activate code generation and generate implementations to manage |
shaunkawano | cb4b3ef | 2016-07-28 12:06:37 -0700 | [diff] [blame] | 58 | your graph you will need to include `dagger-compiler-2.x.jar` in your build |
Christian Edward Gruber | ddbe7f1 | 2015-04-07 18:49:47 -0700 | [diff] [blame] | 59 | at compile time. |
Jesse Wilson | aadda23 | 2012-08-09 19:25:52 -0400 | [diff] [blame] | 60 | |
ronshapiro | 8ab63f1 | 2018-01-16 08:40:08 -0800 | [diff] [blame] | 61 | #### Maven |
kkovach | bee27f0 | 2016-06-28 07:11:56 -0700 | [diff] [blame] | 62 | |
cgruber | a7e6285 | 2015-05-26 16:01:02 -0700 | [diff] [blame] | 63 | In a Maven project, include the `dagger` artifact in the dependencies section |
ronshapiro | 7ef7121 | 2017-05-02 14:59:24 -0700 | [diff] [blame] | 64 | of your `pom.xml` and the `dagger-compiler` artifact as an |
| 65 | `annotationProcessorPaths` value of the `maven-compiler-plugin`: |
Thomas Broyer | 24a47a7 | 2017-05-02 11:32:49 -0700 | [diff] [blame] | 66 | |
| 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 | |
ronshapiro | 7ef7121 | 2017-05-02 14:59:24 -0700 | [diff] [blame] | 95 | If you are using a version of the `maven-compiler-plugin` lower than `3.5`, add |
ronshapiro | 43498a6 | 2017-05-03 11:28:04 -0700 | [diff] [blame] | 96 | the `dagger-compiler` artifact with the `provided` scope: |
cgruber | 92f9d9f | 2017-08-10 09:57:04 -0700 | [diff] [blame] | 97 | |
ronshapiro | 7ef7121 | 2017-05-02 14:59:24 -0700 | [diff] [blame] | 98 | ```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 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 114 | If you use the beta `dagger-producers` extension (which supplies |
| 115 | parallelizable execution graphs), then add this to your maven configuration: |
Christian Edward Gruber | ddbe7f1 | 2015-04-07 18:49:47 -0700 | [diff] [blame] | 116 | |
| 117 | ```xml |
| 118 | <dependencies> |
| 119 | <dependency> |
| 120 | <groupId>com.google.dagger</groupId> |
| 121 | <artifactId>dagger-producers</artifactId> |
shaunkawano | cb4b3ef | 2016-07-28 12:06:37 -0700 | [diff] [blame] | 122 | <version>2.x</version> |
Christian Edward Gruber | ddbe7f1 | 2015-04-07 18:49:47 -0700 | [diff] [blame] | 123 | </dependency> |
| 124 | </dependencies> |
| 125 | ``` |
| 126 | |
ronshapiro | 8ab63f1 | 2018-01-16 08:40:08 -0800 | [diff] [blame] | 127 | #### Java Gradle |
kkovach | bee27f0 | 2016-06-28 07:11:56 -0700 | [diff] [blame] | 128 | ```groovy |
| 129 | // Add plugin https://plugins.gradle.org/plugin/net.ltgt.apt |
| 130 | plugins { |
Thomas Broyer | 24a47a7 | 2017-05-02 11:32:49 -0700 | [diff] [blame] | 131 | id "net.ltgt.apt" version "0.10" |
kkovach | bee27f0 | 2016-06-28 07:11:56 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Add Dagger dependencies |
| 135 | dependencies { |
cgruber | ed47efc | 2016-07-28 13:45:54 -0700 | [diff] [blame] | 136 | compile 'com.google.dagger:dagger:2.x' |
| 137 | apt 'com.google.dagger:dagger-compiler:2.x' |
kkovach | bee27f0 | 2016-06-28 07:11:56 -0700 | [diff] [blame] | 138 | } |
| 139 | ``` |
| 140 | |
ronshapiro | 8ab63f1 | 2018-01-16 08:40:08 -0800 | [diff] [blame] | 141 | #### Android Gradle |
kkovach | bee27f0 | 2016-06-28 07:11:56 -0700 | [diff] [blame] | 142 | ```groovy |
kkovach | bee27f0 | 2016-06-28 07:11:56 -0700 | [diff] [blame] | 143 | // Add Dagger dependencies |
| 144 | dependencies { |
cgruber | ed47efc | 2016-07-28 13:45:54 -0700 | [diff] [blame] | 145 | compile 'com.google.dagger:dagger:2.x' |
Ron Shapiro | bcef45b | 2016-11-03 15:32:10 -0400 | [diff] [blame] | 146 | annotationProcessor 'com.google.dagger:dagger-compiler:2.x' |
kkovach | bee27f0 | 2016-06-28 07:11:56 -0700 | [diff] [blame] | 147 | } |
| 148 | ``` |
Christian Edward Gruber | ddbe7f1 | 2015-04-07 18:49:47 -0700 | [diff] [blame] | 149 | |
ronshapiro | edc12ed | 2017-04-06 13:49:04 -0700 | [diff] [blame] | 150 | If you're using classes in `dagger.android` you'll also want to include: |
| 151 | |
| 152 | ```groovy |
| 153 | compile 'com.google.dagger:dagger-android:2.x' |
| 154 | compile 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries |
| 155 | annotationProcessor 'com.google.dagger:dagger-android-processor:2.x' |
| 156 | ``` |
| 157 | |
Ron Shapiro | bcef45b | 2016-11-03 15:32:10 -0400 | [diff] [blame] | 158 | If you're using a version of the Android gradle plugin below `2.2`, see |
| 159 | https://bitbucket.org/hvisser/android-apt. |
| 160 | |
ronshapiro | 9ad2f3d | 2016-12-19 09:07:31 -0800 | [diff] [blame] | 161 | If you're using the [Android Databinding library][databinding], you may want to |
| 162 | increase the number of errors that `javac` will print. When Dagger prints an |
| 163 | error, databinding compilation will halt and sometimes print more than 100 |
Said Tahsin Dane | 8c81795 | 2017-07-04 08:24:25 -0700 | [diff] [blame] | 164 | errors, which is the default amount for `javac`. For more information, see |
| 165 | [Issue 306](https://github.com/google/dagger/issues/306). |
ronshapiro | 9ad2f3d | 2016-12-19 09:07:31 -0800 | [diff] [blame] | 166 | |
| 167 | ```groovy |
| 168 | gradle.projectsEvaluated { |
| 169 | tasks.withType(JavaCompile) { |
| 170 | options.compilerArgs << "-Xmaxerrs" << "500" // or whatever number you want |
| 171 | } |
| 172 | } |
| 173 | ``` |
| 174 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 175 | ### Download |
Christian Edward Gruber | 97cc761 | 2014-10-29 15:52:47 -0700 | [diff] [blame] | 176 | |
Christian Edward Gruber | eb75262 | 2015-06-01 00:19:00 -0700 | [diff] [blame] | 177 | * 2.x (google/dagger) |
Christian Edward Gruber | 40a5b39 | 2014-11-14 10:09:53 -0800 | [diff] [blame] | 178 | * [Dagger 2.0 Documentation][website] |
Christian Edward Gruber | ddbe7f1 | 2015-04-07 18:49:47 -0700 | [diff] [blame] | 179 | * [Dagger 2.0 Javadocs][20api] |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 180 | * [Dagger development Javadocs][latestapi] (from the `master` branch |
| 181 | on GitHub) |
Christian Edward Gruber | 24916c6 | 2014-10-17 13:37:19 -0700 | [diff] [blame] | 182 | * [Google's Dagger project site on GitHub][project] |
Christian Edward Gruber | eb75262 | 2015-06-01 00:19:00 -0700 | [diff] [blame] | 183 | * 1.x (square/dagger) |
Christian Edward Gruber | 24916c6 | 2014-10-17 13:37:19 -0700 | [diff] [blame] | 184 | * [Square's original Dagger project site on GitHub][square] |
Christian Edward Gruber | 97cc761 | 2014-10-29 15:52:47 -0700 | [diff] [blame] | 185 | |
| 186 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 187 | If you do not use maven, gradle, ivy, or other build systems that consume |
| 188 | maven-style binary artifacts, they can be downloaded directly via the |
cpovirk | 3a68b06 | 2018-08-20 09:54:35 -0700 | [diff] [blame] | 189 | [Maven Central Repository][mavencentral]. |
Christian Edward Gruber | f0de394 | 2012-10-17 13:05:41 -0400 | [diff] [blame] | 190 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 191 | Developer snapshots are available from Sonatype's |
| 192 | [snapshot repository][dagger-snap], and are built on a clean build of |
| 193 | the GitHub project's master branch. |
Jesse Wilson | bcc571d | 2012-08-06 17:59:43 -0400 | [diff] [blame] | 194 | |
ronshapiro | b7ce1dc | 2017-10-23 08:11:52 -0700 | [diff] [blame] | 195 | ## Building Dagger |
| 196 | |
Pierre-Yves Ricau | 19f768d | 2019-04-12 12:43:06 -0700 | [diff] [blame] | 197 | See [the CONTRIBUTING.md docs][Building Dagger]. |
ronshapiro | b7ce1dc | 2017-10-23 08:11:52 -0700 | [diff] [blame] | 198 | |
cgruber | 4ab5846 | 2016-03-11 12:41:52 -0800 | [diff] [blame] | 199 | ## License |
Jesse Wilson | 70eee48 | 2012-06-25 12:29:15 -0700 | [diff] [blame] | 200 | |
isogai.shiraji | 611ff4f | 2016-08-24 08:16:19 -0700 | [diff] [blame] | 201 | Copyright 2012 The Dagger Authors |
Jesse Wilson | 70eee48 | 2012-06-25 12:29:15 -0700 | [diff] [blame] | 202 | |
| 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 Wilson | bcc571d | 2012-08-06 17:59:43 -0400 | [diff] [blame] | 214 | |
ronshapiro | 072c477 | 2019-05-28 07:55:21 -0700 | [diff] [blame] | 215 | [20api]: https://dagger.dev/api/2.0/ |
ronshapiro | b7ce1dc | 2017-10-23 08:11:52 -0700 | [diff] [blame] | 216 | [`bazel`]: https://bazel.build |
ronshapiro | 8ab63f1 | 2018-01-16 08:40:08 -0800 | [diff] [blame] | 217 | [bazel-external-deps]: https://docs.bazel.build/versions/master/external.html#depending-on-other-bazel-projects |
Pierre-Yves Ricau | 19f768d | 2019-04-12 12:43:06 -0700 | [diff] [blame] | 218 | [Building Dagger]: CONTRIBUTING.md#building-dagger |
ronshapiro | 9ad2f3d | 2016-12-19 09:07:31 -0800 | [diff] [blame] | 219 | [dagger-snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/dagger/ |
dpb | 5367767 | 2017-03-14 11:33:48 -0700 | [diff] [blame] | 220 | [databinding]: https://developer.android.com/topic/libraries/data-binding/ |
ronshapiro | 9ad2f3d | 2016-12-19 09:07:31 -0800 | [diff] [blame] | 221 | [gaktalk]: https://www.youtube.com/watch?v=oK_XtfXPkqw |
ronshapiro | 072c477 | 2019-05-28 07:55:21 -0700 | [diff] [blame] | 222 | [latestapi]: https://dagger.dev/api/latest/ |
ronshapiro | 9ad2f3d | 2016-12-19 09:07:31 -0800 | [diff] [blame] | 223 | [mavenbadge-svg]: https://maven-badges.herokuapp.com/maven-central/com.google.dagger/dagger/badge.svg |
cpovirk | 3a68b06 | 2018-08-20 09:54:35 -0700 | [diff] [blame] | 224 | [mavencentral]: https://search.maven.org/artifact/com.google.dagger/dagger |
ronshapiro | 9ad2f3d | 2016-12-19 09:07:31 -0800 | [diff] [blame] | 225 | [project]: http://github.com/google/dagger/ |
| 226 | [proposal]: https://github.com/square/dagger/issues/366 |
| 227 | [square]: http://github.com/square/dagger/ |
ronshapiro | 072c477 | 2019-05-28 07:55:21 -0700 | [diff] [blame] | 228 | [website]: https://dagger.dev |