forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to use string keys for dagger.android and allow the key…
…s to be obfuscated This is enabled by default internally, where proguard understands -identifiernamestring, but disabled externally where it doesn't (and where R8 is not yet the standard). It can be enabled with the -Adagger.android.experimentalUseStringKeys flag for users that don't use proguard. This way we can guarantee that any project without modification will continue to work when proguarded. This solves an issue where classloading of every Activity/Fragment/Service injector that's a child of the root component is forced into application startup. It also solves an issue where framework components that were introduced in newer versions of Android cannot be easily added without producing separate binaries, adding @ContributesAndroidInjector conditionally depending on the minimum API version. Fixes #1064 I've decided to hold off on documenting this until we have a viable solution externally (when R8 is the standard, and it supports META-INF/proguard files). RELNOTES: - If you use `dagger.android` and you aren't already installing `AndroidInjectionModule` (or `AndroidSupportInjectionModule`), you should do so, otherwise you are likely to get a missing binding error from Dagger when you upgrade. - Add an option to use string keys for dagger.android and allow the keys to be obfuscated. - Note that if you use a bytecode shrinker/optimizer, this is only viable if you use a version of R8 that can read generated proguard rules (or if you extract the contents of the generated rule and include it in your build) - You can enable this mode with the flag `-Adagger.android.experimentalUseStringKeys`. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=204987571
- Loading branch information
1 parent
e42c864
commit 3a08964
Showing
30 changed files
with
1,598 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2018 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 dagger.android; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
|
||
import dagger.MapKey; | ||
import dagger.internal.Beta; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* {@link MapKey} annotation to key {@link AndroidInjector.Factory} bindings. The {@linkplain | ||
* #value() value} of the annotation is the canonical name of the class that will be passed to | ||
* {@link AndroidInjector#inject(Object)}. | ||
* | ||
* <p>All key strings will be obfuscated by ProGuard/R8/AppReduce if the named class is obfuscated. | ||
* | ||
* <p> | ||
* You should only use this annotation if you are using a version of ProGuard/R8/AppReduce that | ||
* supports the {@code -identifiernamestring} flag. | ||
*/ | ||
@Beta | ||
@MapKey | ||
@Target(METHOD) | ||
@Documented | ||
public @interface AndroidInjectionKey { | ||
/** The fully qualified class name of the type to be injected. */ | ||
String value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2018 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 dagger.android.internal; | ||
|
||
/** | ||
* An internal implementation detail of Dagger's generated code. This is not guaranteed to remain | ||
* consistent from version to version. | ||
*/ | ||
public final class AndroidInjectionKeys { | ||
/** | ||
* Accepts the fully qualified name of a class that is injected with {@code dagger.android}. | ||
* | ||
* <p>From a runtime perspective, this method does nothing except return its single argument. It | ||
* is used as a signal to bytecode shrinking tools that its argument should be rewritten if it | ||
* corresponds to a class that has been obfuscated/relocated. Once it is done so, it is expected | ||
* that the argument will be inlined and this method will go away. | ||
*/ | ||
public static String of(String mapKey) { | ||
return mapKey; | ||
} | ||
|
||
private AndroidInjectionKeys() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.