Skip to content

Commit 07abbf3

Browse files
berstanioBerstanio
authored andcommitted
Add proguard config collector
1 parent f9ef578 commit 07abbf3

File tree

6 files changed

+203
-1
lines changed

6 files changed

+203
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ moe {
180180
// whether code obfuscation is enabled. Ignored when `baseCfgFile` is specified. Default to `false`
181181
obfuscationEnabled = false
182182
183+
// whether proguard config collector is enabled. Default to `true`
184+
proguardCollectorEnabled = true
185+
183186
// exclude files from `-injars` config that will be processed by proguard
184187
excludeFiles = [
185188
'META-INF/*.SF',

src/main/java/org/moe/gradle/MoePlugin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.moe.gradle.anns.Nullable;
2929
import org.moe.gradle.remote.Server;
3030
import org.moe.gradle.tasks.AbstractBaseTask;
31+
import org.moe.gradle.tasks.ProguardCollect;
3132
import org.moe.gradle.tasks.Dex2Oat;
3233
import org.moe.gradle.tasks.ClassValidate;
3334
import org.moe.gradle.tasks.GenerateUIObjCInterfaces;
@@ -136,6 +137,8 @@ public void apply(Project project) {
136137
// Install rules
137138
addRule(R8.class, "Shrinks/obfuscates app and produces dex files.",
138139
asList(SOURCE_SET, MODE), MoePlugin.this);
140+
addRule(ProguardCollect.class, "Generate code specific proguard keep configs.",
141+
asList(SOURCE_SET, MODE), MoePlugin.this);
139142
addRule(ClassValidate.class, "Validate classes.",
140143
asList(SOURCE_SET, MODE), MoePlugin.this);
141144
addRule(Dex2Oat.class, "Creates art and oat files.",

src/main/java/org/moe/gradle/options/ProGuardOptions.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import java.util.Arrays;
1010
import java.util.Collection;
11-
import java.util.HashSet;
1211
import java.util.LinkedHashSet;
1312
import java.util.Set;
1413

@@ -25,6 +24,7 @@ public class ProGuardOptions {
2524
private int level = LEVEL_APP;
2625
private boolean minifyEnabled = true;
2726
private boolean obfuscationEnabled = false;
27+
private boolean proguardCollectorEnabled = true;
2828
@NotNull
2929
@org.jetbrains.annotations.NotNull
3030
private Set<String> excludeFiles = new LinkedHashSet<>();
@@ -90,6 +90,15 @@ public void setObfuscationEnabled(boolean obfuscationEnabled) {
9090
this.obfuscationEnabled = obfuscationEnabled;
9191
}
9292

93+
public boolean isProguardCollectorEnabled () {
94+
return proguardCollectorEnabled;
95+
}
96+
97+
@IgnoreUnused
98+
public void setProguardCollectorEnabled (boolean proguardCollectorEnabled) {
99+
this.proguardCollectorEnabled = proguardCollectorEnabled;
100+
}
101+
93102
@NotNull
94103
@org.jetbrains.annotations.NotNull
95104
public Collection<String> getExcludeFiles() {
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
Copyright (C) 2016 Migeran
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package org.moe.gradle.tasks;
18+
19+
import org.gradle.api.GradleException;
20+
import org.gradle.api.Project;
21+
import org.gradle.api.file.ConfigurableFileCollection;
22+
import org.gradle.api.logging.Logger;
23+
import org.gradle.api.logging.Logging;
24+
import org.gradle.api.tasks.InputFiles;
25+
import org.gradle.api.tasks.Internal;
26+
import org.gradle.api.tasks.OutputFile;
27+
import org.gradle.api.tasks.SourceSet;
28+
import org.moe.gradle.MoeExtension;
29+
import org.moe.gradle.MoePlugin;
30+
import org.moe.gradle.MoeSDK;
31+
import org.moe.gradle.anns.IgnoreUnused;
32+
import org.moe.gradle.anns.NotNull;
33+
import org.moe.gradle.anns.Nullable;
34+
import org.moe.gradle.utils.FileUtils;
35+
import org.moe.gradle.utils.Mode;
36+
import org.moe.gradle.utils.Require;
37+
import org.moe.tools.classvalidator.proguard.ProguardCollector;
38+
39+
import java.io.File;
40+
import java.io.IOException;
41+
import java.nio.file.Path;
42+
import java.nio.file.Paths;
43+
import java.util.Collection;
44+
import java.util.HashSet;
45+
import java.util.LinkedHashSet;
46+
import java.util.Set;
47+
48+
public class ProguardCollect extends AbstractBaseTask {
49+
50+
private static final Logger LOG = Logging.getLogger(ProguardCollect.class);
51+
52+
private static final String CONVENTION_OUT_CFG_FILE = "outCfgFile";
53+
private static final String CONVENTION_INPUT_FILES = "inputFiles";
54+
private static final String CONVENTION_CLASSPATH_FILES = "classpathFiles";
55+
56+
57+
@Nullable
58+
private Object outCfgFile;
59+
60+
@OutputFile
61+
@NotNull
62+
public File getOutCfgFile() {
63+
return getProject().file(getOrConvention(this.outCfgFile, CONVENTION_OUT_CFG_FILE));
64+
}
65+
66+
@IgnoreUnused
67+
public void setOutCfgFile(@Nullable Object outCfgFile) {
68+
this.outCfgFile = outCfgFile;
69+
}
70+
71+
@Nullable
72+
private Set<Object> inputFiles;
73+
74+
@InputFiles
75+
@NotNull
76+
public ConfigurableFileCollection getInputFiles() {
77+
return getProject().files(getOrConvention(inputFiles, CONVENTION_INPUT_FILES));
78+
}
79+
80+
@IgnoreUnused
81+
public void setInputFiles(@Nullable Collection<Object> inputFiles) {
82+
this.inputFiles = inputFiles == null ? null : new HashSet<>(inputFiles);
83+
}
84+
85+
@Nullable
86+
private Set<Object> classpathFiles;
87+
88+
@InputFiles
89+
@NotNull
90+
public ConfigurableFileCollection getClasspathFiles() {
91+
return getProject().files(getOrConvention(classpathFiles, CONVENTION_CLASSPATH_FILES));
92+
}
93+
94+
@IgnoreUnused
95+
public void setClasspathFiles(@Nullable Set<Object> classpathFiles) {
96+
this.classpathFiles = classpathFiles == null ? null : new HashSet<>(classpathFiles);
97+
}
98+
99+
@Override
100+
protected void run() {
101+
try {
102+
FileUtils.deleteFileOrFolder(getOutCfgFile());
103+
} catch (IOException e) {
104+
throw new GradleException("an IOException occurred", e);
105+
}
106+
ProguardCollector.process(getInputFiles().getFiles(), getOutCfgFile().toPath(), getClasspathFiles().getFiles());
107+
}
108+
109+
private ClassValidate classValidateTaskDep;
110+
111+
@Nullable
112+
@IgnoreUnused
113+
@Internal
114+
public ClassValidate getClassValidateTaskDep() {
115+
return classValidateTaskDep;
116+
}
117+
118+
protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @NotNull Mode mode) {
119+
Require.nonNull(sourceSet);
120+
121+
setSupportsRemoteBuild(false);
122+
123+
final Project project = getProject();
124+
final MoeExtension ext = getMoeExtension();
125+
final MoeSDK sdk = getMoeSDK();
126+
127+
// Construct default output path
128+
final Path out = Paths.get(MoePlugin.MOE, sourceSet.getName(), "collect", mode.name);
129+
130+
setDescription("Generate code specific proguard keep configs (sourceset: " + sourceSet.getName() + ", mode: " + mode.name + ").");
131+
132+
// Add dependencies
133+
final ClassValidate classValidateTask = getMoePlugin().getTaskBy(ClassValidate.class, sourceSet, mode);
134+
classValidateTaskDep = classValidateTask;
135+
dependsOn(classValidateTask);
136+
137+
addConvention(CONVENTION_OUT_CFG_FILE, () -> resolvePathInBuildDir(out, "proguard_collect.cfg"));
138+
addConvention(CONVENTION_INPUT_FILES, () -> new LinkedHashSet<Object>(classValidateTask.getOutputJars().getFiles()));
139+
addConvention(CONVENTION_CLASSPATH_FILES, () -> {
140+
HashSet<Object> hashSet = new LinkedHashSet<Object>(classValidateTask.getClasspathFiles().getFiles());
141+
hashSet.add(sdk.getJava8SupportJar());
142+
hashSet.add(sdk.getCoreJar());
143+
hashSet.add(ext.getPlatformJar());
144+
145+
return hashSet;
146+
});
147+
addConvention(CONVENTION_LOG_FILE, () -> resolvePathInBuildDir(out, "ProguardCollect.log"));
148+
}
149+
}

src/main/java/org/moe/gradle/tasks/R8.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class R8 extends AbstractBaseTask {
7272
private static final String CONVENTION_MAPPING_FILE = "mappingFile";
7373
private static final String CONVENTION_MINIFY_ENABLED = "minifyEnabled";
7474
private static final String CONVENTION_OBFUSCATION_ENABLED = "obfuscationEnabled";
75+
private static final String CONVENTION_COLLECTOR_ENABLED = "collectorEnabled";
7576
private static final String CONVENTION_DEBUG_ENABLED = "debugEnabled";
7677

7778
@Nullable
@@ -228,6 +229,19 @@ public boolean isObfuscationEnabled() {
228229
return getOrConvention(obfuscationEnabled, CONVENTION_OBFUSCATION_ENABLED);
229230
}
230231

232+
@Nullable
233+
private Boolean collectorEnabled;
234+
235+
@IgnoreUnused
236+
public void setCollectorEnabled(Boolean collectorEnabled) {
237+
this.collectorEnabled = collectorEnabled;
238+
}
239+
240+
@Input
241+
public boolean isCollectorEnabled() {
242+
return getOrConvention(collectorEnabled, CONVENTION_COLLECTOR_ENABLED);
243+
}
244+
231245
@Nullable
232246
private Boolean debugEnabled;
233247

@@ -336,6 +350,12 @@ private void composeConfigurationFile() {
336350
startSection(conf, "Appending from " + baseCfgFile);
337351
conf.append(FileUtils.read(baseCfgFile));
338352

353+
if (isCollectorEnabled()) {
354+
final File collectedConfig = getProguardCollectTaskDep().getOutCfgFile();
355+
startSection(conf, "Appending from " + collectedConfig);
356+
conf.append(FileUtils.read(collectedConfig));
357+
}
358+
339359
startSection(conf, "Shrinking & obfuscation flags");
340360
if (!isCustomisedBaseConfig()) {
341361
if (isMinifyEnabled()) {
@@ -419,6 +439,15 @@ public ClassValidate getClassValidateTaskDep() {
419439
return classValidateTaskDep;
420440
}
421441

442+
private ProguardCollect proguardCollectTaskDep;
443+
444+
@Nullable
445+
@IgnoreUnused
446+
@Internal
447+
public ProguardCollect getProguardCollectTaskDep() {
448+
return proguardCollectTaskDep;
449+
}
450+
422451
protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @NotNull Mode mode) {
423452
Require.nonNull(sourceSet);
424453

@@ -438,6 +467,10 @@ protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @Not
438467
classValidateTaskDep = classValidateTask;
439468
dependsOn(classValidateTask);
440469

470+
final ProguardCollect proguardCollectTask = getMoePlugin().getTaskBy(ProguardCollect.class, sourceSet, mode);
471+
proguardCollectTaskDep = proguardCollectTask;
472+
dependsOn(proguardCollectTask);
473+
441474
addConvention(CONVENTION_R8_JAR, sdk::getR8Jar);
442475
addConvention(CONVENTION_BASE_CFG_FILE, () -> {
443476
if (ext.proguard.getBaseCfgFile() != null) {
@@ -496,6 +529,7 @@ protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @Not
496529
addConvention(CONVENTION_LOG_FILE, () -> resolvePathInBuildDir(out, "R8.log"));
497530
addConvention(CONVENTION_MINIFY_ENABLED, ext.proguard::isMinifyEnabled);
498531
addConvention(CONVENTION_OBFUSCATION_ENABLED, ext.proguard::isObfuscationEnabled);
532+
addConvention(CONVENTION_COLLECTOR_ENABLED, ext.proguard::isProguardCollectorEnabled);
499533
addConvention(CONVENTION_DEBUG_ENABLED, () -> mode.equals(Mode.DEBUG));
500534
}
501535
}

src/main/java/org/moe/gradle/tasks/XcodeBuild.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ protected void run() {
428428
excludes.add(r8Task.getComposedCfgFile());
429429
excludes.add(r8Task.getLogFile());
430430

431+
final ProguardCollect proguardCollectTask = r8Task.getProguardCollectTaskDep();
432+
excludes.add(proguardCollectTask.getOutCfgFile());
433+
excludes.add(proguardCollectTask.getLogFile());
434+
431435
final ClassValidate classValidateTask = r8Task.getClassValidateTaskDep();
432436
excludes.add(classValidateTask.getOutputDir());
433437
excludes.add(classValidateTask.getLogFile());

0 commit comments

Comments
 (0)