Skip to content

Commit a45363e

Browse files
author
berstanio
committed
Add proguard config collector
1 parent f9ef578 commit a45363e

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

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.",
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ private void composeConfigurationFile() {
336336
startSection(conf, "Appending from " + baseCfgFile);
337337
conf.append(FileUtils.read(baseCfgFile));
338338

339+
final File collectedConfig = getProguardCollectTaskDep().getOutCfgFile();
340+
startSection(conf, "Appending from " + collectedConfig);
341+
conf.append(FileUtils.read(collectedConfig));
342+
339343
startSection(conf, "Shrinking & obfuscation flags");
340344
if (!isCustomisedBaseConfig()) {
341345
if (isMinifyEnabled()) {
@@ -419,6 +423,15 @@ public ClassValidate getClassValidateTaskDep() {
419423
return classValidateTaskDep;
420424
}
421425

426+
private ProguardCollect proguardCollectTaskDep;
427+
428+
@Nullable
429+
@IgnoreUnused
430+
@Internal
431+
public ProguardCollect getProguardCollectTaskDep() {
432+
return proguardCollectTaskDep;
433+
}
434+
422435
protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @NotNull Mode mode) {
423436
Require.nonNull(sourceSet);
424437

@@ -438,6 +451,10 @@ protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @Not
438451
classValidateTaskDep = classValidateTask;
439452
dependsOn(classValidateTask);
440453

454+
final ProguardCollect proguardCollectTask = getMoePlugin().getTaskBy(ProguardCollect.class, sourceSet, mode);
455+
proguardCollectTaskDep = proguardCollectTask;
456+
dependsOn(proguardCollectTask);
457+
441458
addConvention(CONVENTION_R8_JAR, sdk::getR8Jar);
442459
addConvention(CONVENTION_BASE_CFG_FILE, () -> {
443460
if (ext.proguard.getBaseCfgFile() != null) {

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)