Skip to content
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

Move identifiernamestring to proguard config + enable default #1720

Closed
wants to merge 13 commits into from
4 changes: 0 additions & 4 deletions java/dagger/android/AndroidInjectionKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
* {@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
Expand Down
4 changes: 3 additions & 1 deletion java/dagger/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ android_library(
srcs = SRCS,
javacopts = SOURCE_7_TARGET_7 + DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
manifest = "AndroidManifest.xml",
proguard_specs = ["proguard.cfg"],
tags = ["maven_coordinates=com.google.dagger:dagger-android:" + POM_VERSION],
plugins = [
"//java/dagger/android/proguardprocessor/internal:plugin",
],
deps = [
"//:dagger_with_compiler",
"@google_bazel_common//third_party/java/auto:value",
Expand Down
30 changes: 1 addition & 29 deletions java/dagger/android/processor/AndroidProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,18 @@
package dagger.android.processor;

import static javax.tools.Diagnostic.Kind.ERROR;
import static javax.tools.StandardLocation.CLASS_OUTPUT;
import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING;

import com.google.auto.common.BasicAnnotationProcessor;
import com.google.auto.service.AutoService;
import com.google.common.base.Ascii;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.googlejavaformat.java.filer.FormattingFiler;
import java.io.IOException;
import java.io.Writer;
import java.util.Set;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
Expand Down Expand Up @@ -78,7 +73,7 @@ protected Iterable<? extends ProcessingStep> initSteps() {

private boolean useStringKeys() {
if (!processingEnv.getOptions().containsKey(FLAG_EXPERIMENTAL_USE_STRING_KEYS)) {
return false;
return true;
}
String flagValue = processingEnv.getOptions().get(FLAG_EXPERIMENTAL_USE_STRING_KEYS);
if (flagValue == null || Ascii.equalsIgnoreCase(flagValue, "true")) {
Expand All @@ -97,29 +92,6 @@ private boolean useStringKeys() {
}
}

@Override
protected void postRound(RoundEnvironment roundEnv) {
if (roundEnv.processingOver() && useStringKeys()) {
try (Writer writer = createProguardFile()){
writer.write(
Joiner.on("\n")
.join(
"-identifiernamestring class dagger.android.internal.AndroidInjectionKeys {",
" java.lang.String of(java.lang.String);",
"}"));
} catch (IOException e) {
e.printStackTrace();
}
}
}

private Writer createProguardFile() throws IOException {
return processingEnv
.getFiler()
.createResource(CLASS_OUTPUT, "", "META-INF/proguard/dagger.android.AndroidInjectionKeys")
.openWriter();
}

@Override
public Set<String> getSupportedOptions() {
return ImmutableSet.of(FLAG_EXPERIMENTAL_USE_STRING_KEYS);
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/android/processor/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2017 The Dagger Authors.
# Copyright (C) 2020 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.
Expand Down
1 change: 0 additions & 1 deletion java/dagger/android/proguard.cfg

This file was deleted.

45 changes: 45 additions & 0 deletions java/dagger/android/proguardprocessor/internal/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (C) 2017 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.

# Description:
# Public Dagger API for Android

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

load(
"//:build_defs.bzl",
"DOCLINT_HTML_AND_SYNTAX",
"DOCLINT_REFERENCES",
)

filegroup(
name = "srcs",
srcs = glob(["*.java"]),
)

java_library(
name = "proguardprocessor",
srcs = [":srcs"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"@google_bazel_common//third_party/java/auto:service",
],
)

java_plugin(
name = "plugin",
generates_api = 1,
processor_class = "dagger.android.proguardprocessor.internal.ProguardProcessor",
deps = [":proguardprocessor"],
)
100 changes: 100 additions & 0 deletions java/dagger/android/proguardprocessor/internal/ProguardProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2020 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.proguardprocessor.internal;

import com.google.auto.service.AutoService;
import java.io.IOException;
import java.io.Writer;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;

import static javax.tools.StandardLocation.CLASS_OUTPUT;

/**
* An {@linkplain Processor annotation processor} to generate dagger-android's specific proguard
* needs. This is only intended to run over the dagger-android project itself, as the alternative
* is to create an intermediary java_library for proguard rules to be consumed by the project.
*
* <p>Basic structure looks like this:
* <pre><code>
* resources/META-INF/com.android.tools/proguard/dagger-android.pro
* resources/META-INF/com.android.tools/r8/dagger-android.pro
* resources/META-INF/proguard/dagger-android.pro
* </code></pre>
*/
@SupportedAnnotationTypes("*")
@AutoService(Processor.class)
public final class ProguardProcessor extends AbstractProcessor {

private boolean hasGenerated = false;

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.errorRaised()) {
return false;
} else if (hasGenerated) {
return false;
}

generate();
hasGenerated = true;

return false;
}

private void generate() {
Filer filer = processingEnv.getFiler();

StringBuilder rulesBuilder = new StringBuilder();
rulesBuilder.append("-dontwarn com.google.errorprone.annotations.**")
.append("\n");

String proguardRules = rulesBuilder.toString();

String r8Rules = rulesBuilder.append(
"-identifiernamestring class dagger.android.internal.AndroidInjectionKeys {")
.append("\n")
.append(" java.lang.String of(java.lang.String);")
.append("\n")
.append("}")
.append("\n")
.toString();

writeFile(filer, "com.android.tools/proguard", proguardRules);
writeFile(filer, "com.android.tools/r8", r8Rules);
writeFile(filer, "proguard", proguardRules);
}

private static void writeFile(Filer filer, String intermediatePath, String contents) {
try (Writer writer = filer.createResource(CLASS_OUTPUT,
"", "META-INF/" + intermediatePath + "/dagger-android.pro").openWriter()) {
writer.write(contents);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
81 changes: 0 additions & 81 deletions javatests/dagger/android/processor/AndroidProcessorTest.java

This file was deleted.