Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate proguard rules on-demand (#236)
* Add ProguardConfig * Wire in proguard config to externally generated files * Remove generated file bits * Add tests * Nix unused import
- Loading branch information
Showing
5 changed files
with
440 additions
and
33 deletions.
There are no files selected for viewing
32 changes: 0 additions & 32 deletions
32
auto-value-gson-runtime/src/main/resources/META-INF/proguard/autovaluegson.pro
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 |
---|---|---|
@@ -1,35 +1,3 @@ | ||
# Annotations are for embedding static analysis information. | ||
-dontwarn org.jetbrains.annotations.** | ||
-dontwarn com.google.errorprone.annotations.** | ||
|
||
# Retain generated TypeAdapters if annotated type is retained. | ||
-if @com.ryanharter.auto.value.gson.GenerateTypeAdapter class * | ||
-keep class <1>_GsonTypeAdapter { | ||
<init>(...); | ||
<fields>; | ||
} | ||
-if @com.ryanharter.auto.value.gson.GenerateTypeAdapter class **$* | ||
-keep class <1>_<2>_GsonTypeAdapter { | ||
<init>(...); | ||
<fields>; | ||
} | ||
-if @com.ryanharter.auto.value.gson.GenerateTypeAdapter class **$*$* | ||
-keep class <1>_<2>_<3>_GsonTypeAdapter { | ||
<init>(...); | ||
<fields>; | ||
} | ||
-if @com.ryanharter.auto.value.gson.GenerateTypeAdapter class **$*$*$* | ||
-keep class <1>_<2>_<3>_<4>_GsonTypeAdapter { | ||
<init>(...); | ||
<fields>; | ||
} | ||
-if @com.ryanharter.auto.value.gson.GenerateTypeAdapter class **$*$*$*$* | ||
-keep class <1>_<2>_<3>_<4>_<5>_GsonTypeAdapter { | ||
<init>(...); | ||
<fields>; | ||
} | ||
-if @com.ryanharter.auto.value.gson.GenerateTypeAdapter class **$*$*$*$*$* | ||
-keep class <1>_<2>_<3>_<4>_<5>_<6>_GsonTypeAdapter { | ||
<init>(...); | ||
<fields>; | ||
} |
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
85 changes: 85 additions & 0 deletions
85
auto-value-gson/src/main/java/com/ryanharter/auto/value/gson/ProguardConfig.java
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,85 @@ | ||
package com.ryanharter.auto.value.gson; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.squareup.javapoet.ClassName; | ||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.util.List; | ||
import javax.annotation.processing.Filer; | ||
import javax.lang.model.element.Element; | ||
|
||
import static javax.tools.StandardLocation.CLASS_OUTPUT; | ||
|
||
/** | ||
* Represents a proguard configuration for a given spec. This covers two main areas: | ||
* <ul> | ||
* <li>Keeping the target class name to GenerateTypeAdapter.FACTORY's reflective lookup of the adapter.</li> | ||
* <li>Keeping the generated adapter class name + public constructor for reflective lookup.</li> | ||
* </ul> | ||
* <p> | ||
* Each rule is intended to be as specific and targeted as possible to reduce footprint, and each is | ||
* conditioned on usage of the original target type. | ||
* <p> | ||
* To keep this processor as an {@code ISOLATING} incremental processor, we generate one file per | ||
* target class with a deterministic name (see {@link #outputFile}) with an appropriate originating | ||
* element. | ||
*/ | ||
@AutoValue | ||
abstract class ProguardConfig { | ||
abstract ClassName targetClass(); | ||
abstract ClassName adapterName(); | ||
abstract List<String> adapterConstructorParams(); | ||
abstract String outputFile(); | ||
|
||
static ProguardConfig create( | ||
ClassName targetClass, | ||
ClassName adapterName, | ||
List<String> adapterConstructorParams) { | ||
String outputFile = "META-INF/proguard/avg-" + targetClass.canonicalName() + ".pro"; | ||
return new AutoValue_ProguardConfig(targetClass, | ||
adapterName, | ||
adapterConstructorParams, | ||
outputFile); | ||
} | ||
|
||
/** Writes this to {@code filer}. */ | ||
final void writeTo(Filer filer, Element... originatingElements) throws IOException { | ||
try (Writer writer = filer.createResource(CLASS_OUTPUT, "", outputFile(), originatingElements) | ||
.openWriter()) { | ||
writeTo(writer); | ||
} | ||
} | ||
|
||
private void writeTo(Appendable out) throws IOException { | ||
// | ||
// -if class {the target class} | ||
// -keepnames class {the target class} | ||
// -if class {the target class} | ||
// -keep class {the generated adapter} { | ||
// <init>(...); | ||
// } | ||
// | ||
String targetName = targetClass().canonicalName(); | ||
String adapterCanonicalName = adapterName().canonicalName(); | ||
// Keep the class name for GenerateTypeAdapter.FACTORY's reflective lookup based on it | ||
out.append("-if class ") | ||
.append(targetName) | ||
.append("\n"); | ||
out.append("-keepnames class ") | ||
.append(targetName) | ||
.append("\n"); | ||
out.append("-if class ") | ||
.append(targetName) | ||
.append("\n"); | ||
out.append("-keep class ") | ||
.append(adapterCanonicalName) | ||
.append(" {\n"); | ||
|
||
// Keep the constructor for GenerateTypeAdapter.FACTORY's reflective lookup | ||
String constructorArgs = String.join(",", adapterConstructorParams()); | ||
out.append(" <init>(") | ||
.append(constructorArgs) | ||
.append(");\n"); | ||
out.append("}\n"); | ||
} | ||
} |
Oops, something went wrong.