Skip to content

Commit 1886b6d

Browse files
committed
(multi-os-engine/multi-os-engine#156) Add global moe.proguard.baseCfgFile and appendCfgFile settings. Rename excludedFiles config to excludeFiles.
Update readme.
1 parent a7eb123 commit 1886b6d

File tree

3 files changed

+89
-21
lines changed

3 files changed

+89
-21
lines changed

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,38 @@ buildscript {
160160

161161
### ProGuard
162162

163-
ProGuard has three supported levels of trimming: `app` (default), `platform`, `all`
164-
165-
This level can be set in the buildscript:
163+
ProGuard can be configured globally here, or per-config in [ProGuard Task](#proguard-task).
166164

167165
```groovy
168166
moe {
169-
proguardLevel 'platform'
167+
proguard {
168+
// apply level
169+
level = 'platform'
170+
171+
// path to the base configuration file, or `null` to use SDK default config
172+
baseCfgFile = null
173+
174+
// null or path to to appended configuration file
175+
appendCfgFile = null
176+
177+
// whether code minification is enabled. Ignored when `baseCfgFile` is specified. Default to `true`
178+
minifyEnabled = true
179+
180+
// whether code obfuscation is enabled. Ignored when `baseCfgFile` is specified. Default to `false`
181+
obfuscationEnabled = false
182+
183+
// exclude files from `-injars` config that will be processed by proguard
184+
excludeFiles = [
185+
'META-INF/*.SF',
186+
'META-INF/*.DSA',
187+
'META-INF/*.RSA'
188+
]
189+
excludeFile 'org/bouncycastle/jce/provider/*Spi_8.class'
190+
}
170191
}
171192
```
172193

194+
ProGuard has three supported levels of trimming: `app` (default), `platform`, `all`:
173195
- `app` will only process the classes of your application and it's non-MOE dependencies. This is the fastest mode and
174196
is recommended for development.
175197
- `platform` will do the same as `app` but will also include the `moe-ios.jar` MOE dependency. This mode is a bit
@@ -322,6 +344,7 @@ information about what got stripped can be found in the `proguard.log` file whic
322344
- `obfuscationEnabled`: whether code obfuscation is enabled. Ignored when `baseCfgFile` is specified.
323345
- `mappingFile`: path to the name mapping output file. Generated only if `obfuscationEnabled` is `true`.
324346
- `inJars`: collection of paths to files being passed to ProGuard via `-injars`.
347+
- `excludeFiles`: exclude files from `inJars` that will be processed by proguard.
325348
- `libraryJars`: collection of paths to files being passed to ProGuard via `-libraryjars`.
326349
- `outJar`: path to ProGuard's output jar.
327350
- `composedCfgFile`: path to the composed configuration file.

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.moe.gradle.anns.IgnoreUnused;
55
import org.moe.gradle.anns.NotNull;
66
import org.moe.gradle.anns.Nullable;
7+
import org.moe.gradle.utils.Require;
78

9+
import java.util.Arrays;
810
import java.util.Collection;
911
import java.util.LinkedHashSet;
1012
import java.util.Set;
@@ -22,7 +24,8 @@ public class ProGuardOptions {
2224
private int level = LEVEL_APP;
2325
private boolean minifyEnabled = true;
2426
private boolean obfuscationEnabled = false;
25-
private Set<String> excludedFiles;
27+
@Nullable
28+
private Set<String> excludeFiles;
2629

2730
@NotNull
2831
@IgnoreUnused
@@ -86,12 +89,46 @@ public void setObfuscationEnabled(boolean obfuscationEnabled) {
8689
}
8790

8891
@Nullable
89-
public Collection<String> getExcludedFiles() {
90-
return excludedFiles;
92+
public Collection<String> getExcludeFiles() {
93+
return excludeFiles;
94+
}
95+
96+
@IgnoreUnused
97+
public void setExcludeFiles(@Nullable Collection<String> excludedFiles) {
98+
this.excludeFiles = excludedFiles == null ? null : new LinkedHashSet<>(excludedFiles);
99+
}
100+
101+
public ProGuardOptions excludeFile(String... names) {
102+
if (excludeFiles == null) {
103+
excludeFiles = new LinkedHashSet<>();
104+
}
105+
excludeFiles.addAll(Arrays.asList(Require.nonNull(names)));
106+
return this;
107+
}
108+
109+
@Nullable
110+
private Object baseCfgFile;
111+
112+
@Nullable
113+
public Object getBaseCfgFile() {
114+
return baseCfgFile;
115+
}
116+
117+
@IgnoreUnused
118+
public void setBaseCfgFile(@Nullable Object baseCfgFile) {
119+
this.baseCfgFile = baseCfgFile;
120+
}
121+
122+
@Nullable
123+
private Object appendCfgFile;
124+
125+
@Nullable
126+
public Object getAppendCfgFile() {
127+
return appendCfgFile;
91128
}
92129

93130
@IgnoreUnused
94-
public void setExcludedFiles(@Nullable Collection<String> excludedFiles) {
95-
this.excludedFiles = excludedFiles == null ? null : new LinkedHashSet<>(excludedFiles);
131+
public void setAppendCfgFile(@Nullable Object appendCfgFile) {
132+
this.appendCfgFile = appendCfgFile;
96133
}
97134
}

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class ProGuard extends AbstractBaseTask {
7373
private static final String CONVENTION_BASE_CFG_FILE = "baseCfgFile";
7474
private static final String CONVENTION_APPEND_CFG_FILE = "appendCfgFile";
7575
private static final String CONVENTION_IN_JARS = "inJars";
76-
private static final String CONVENTION_EXCLUDED_FILES = "excludedFiles";
76+
private static final String CONVENTION_EXCLUDE_FILES = "excludeFiles";
7777
private static final String CONVENTION_LIBRARY_JARS = "libraryJars";
7878
private static final String CONVENTION_OUT_JAR = "outJar";
7979
private static final String CONVENTION_COMPOSED_CFG_FILE = "composedCfgFile";
@@ -142,17 +142,17 @@ public void setInJars(@Nullable Collection<Object> inJars) {
142142
}
143143

144144
@Nullable
145-
private Set<String> excludedFiles;
145+
private Set<String> excludeFiles;
146146

147147
@Input
148148
@NotNull
149-
public Collection<String> getExcludedFiles() {
150-
return getOrConvention(excludedFiles, CONVENTION_EXCLUDED_FILES);
149+
public Collection<String> getExcludeFiles() {
150+
return getOrConvention(excludeFiles, CONVENTION_EXCLUDE_FILES);
151151
}
152152

153153
@IgnoreUnused
154-
public void setExcludedFiles(@Nullable Collection<String> excludedFiles) {
155-
this.excludedFiles = excludedFiles == null ? null : new LinkedHashSet<>(excludedFiles);
154+
public void setExcludeFiles(@Nullable Collection<String> excludeFiles) {
155+
this.excludeFiles = excludeFiles == null ? null : new LinkedHashSet<>(excludeFiles);
156156
}
157157

158158
@Nullable
@@ -269,8 +269,8 @@ private String composeInputFileFilter() {
269269
sb.append("(!**.framework/**,!**.bundle/**,!module-info.class");
270270

271271
// Add user specified
272-
for (String excludedFile : getExcludedFiles()) {
273-
sb.append(",!").append(excludedFile);
272+
for (String excludeFile : getExcludeFiles()) {
273+
sb.append(",!").append(excludeFile);
274274
}
275275

276276
sb.append(")");
@@ -458,6 +458,10 @@ protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @Not
458458

459459
addConvention(CONVENTION_PROGUARD_JAR, sdk::getProGuardJar);
460460
addConvention(CONVENTION_BASE_CFG_FILE, () -> {
461+
if (ext.proguard.getBaseCfgFile() != null) {
462+
return ext.proguard.getBaseCfgFile();
463+
}
464+
461465
final File cfg = project.file("proguard.cfg");
462466
if (cfg.exists() && cfg.isFile()) {
463467
return cfg;
@@ -473,6 +477,10 @@ protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @Not
473477
}
474478
});
475479
addConvention(CONVENTION_APPEND_CFG_FILE, () -> {
480+
if (ext.proguard.getAppendCfgFile() != null) {
481+
return ext.proguard.getAppendCfgFile();
482+
}
483+
476484
final File cfg = project.file("proguard.append.cfg");
477485
if (cfg.exists() && cfg.isFile()) {
478486
return cfg;
@@ -521,8 +529,8 @@ protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @Not
521529

522530
return jars;
523531
});
524-
addConvention(CONVENTION_EXCLUDED_FILES, () -> {
525-
Collection<String> exc = ext.proguard.getExcludedFiles();
532+
addConvention(CONVENTION_EXCLUDE_FILES, () -> {
533+
Collection<String> exc = ext.proguard.getExcludeFiles();
526534
if (exc == null) {
527535
exc = Collections.emptySet();
528536
}
@@ -557,7 +565,7 @@ protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @Not
557565
addConvention(CONVENTION_COMPOSED_CFG_FILE, () -> resolvePathInBuildDir(out, "configuration.pro"));
558566
addConvention(CONVENTION_MAPPING_FILE, () -> isCustomisedBaseConfig() || !isObfuscationEnabled() ? null : resolvePathInBuildDir(out, "mapping.txt"));
559567
addConvention(CONVENTION_LOG_FILE, () -> resolvePathInBuildDir(out, "ProGuard.log"));
560-
addConvention(CONVENTION_MINIFY_ENABLED, () -> getMoeExtension().proguard.isMinifyEnabled());
561-
addConvention(CONVENTION_OBFUSCATION_ENABLED, () -> getMoeExtension().proguard.isObfuscationEnabled());
568+
addConvention(CONVENTION_MINIFY_ENABLED, ext.proguard::isMinifyEnabled);
569+
addConvention(CONVENTION_OBFUSCATION_ENABLED, ext.proguard::isObfuscationEnabled);
562570
}
563571
}

0 commit comments

Comments
 (0)