Attempt to reduce jar size a little
- Run optipng on all our images. This has very little effect on most of them (as they're all so small anyway), but has resulted in a 50% reduction in some cases. - Run Proguard on our shadowed dependencies (Cobalt). - Minify our JSON files, stripping all whitespace. This is mostly useful for FML's annotation cache, as that's a massive file, but still a semi-useful optimisation to make. This has helped reduce the jar by about 110kb, which isn't much but still feels somewhat worth it.
96
build.gradle
@ -9,7 +9,9 @@ buildscript {
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.google.code.gson:gson:2.8.1'
|
||||
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
|
||||
classpath 'net.sf.proguard:proguard-gradle:6.1.0beta1'
|
||||
classpath 'org.ajoberstar.grgit:grgit-gradle:3.0.0'
|
||||
}
|
||||
}
|
||||
@ -93,7 +95,60 @@ jar {
|
||||
from configurations.shade.collect { it.isDirectory() ? it : zipTree(it) }
|
||||
}
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.nio.file.*
|
||||
import java.util.zip.*
|
||||
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.JsonElement
|
||||
import org.ajoberstar.grgit.Grgit
|
||||
import proguard.gradle.ProGuardTask
|
||||
|
||||
task proguard(type: ProGuardTask, dependsOn: jar) {
|
||||
description "Removes unused shadowed classes from the jar"
|
||||
|
||||
injars jar.archivePath
|
||||
outjars "${jar.archivePath.absolutePath.replace(".jar", "")}-min.jar"
|
||||
|
||||
// We want to avoid as much obfuscation as possible. We're only doing this to shrink code size.
|
||||
dontobfuscate; dontoptimize; keepattributes; keepparameternames
|
||||
|
||||
// Proguard will remove directories by default, but that breaks JarMount.
|
||||
keepdirectories 'assets/computercraft/lua**'
|
||||
|
||||
// Preserve ComputerCraft classes - we only want to strip shadowed files.
|
||||
keep 'class dan200.computercraft.** { *; }'
|
||||
|
||||
// Preserve the constructors in Cobalt library class, as we init them via reflection
|
||||
keepclassmembers 'class org.squiddev.cobalt.lib.** { <init>(...); }'
|
||||
}
|
||||
|
||||
gradle.projectsEvaluated {
|
||||
tasks.withType(ProGuardTask) {
|
||||
group "compact"
|
||||
|
||||
// Add the main runtime jar and all non-shadowed dependencies
|
||||
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
|
||||
sourceSets.main.compileClasspath
|
||||
.filter { !it.name.contains("Cobalt") }
|
||||
.each { libraryjars it }
|
||||
}
|
||||
}
|
||||
|
||||
task proguardMove(dependsOn: proguard) {
|
||||
description "Replace the original jar with the minified version"
|
||||
group "compact"
|
||||
|
||||
doLast {
|
||||
Files.move(
|
||||
file("${jar.archivePath.absolutePath.replace(".jar", "")}-min.jar").toPath(),
|
||||
file(jar.archivePath).toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
reobfJar.dependsOn proguardMove
|
||||
|
||||
processResources {
|
||||
inputs.property "version", project.version
|
||||
@ -129,6 +184,47 @@ processResources {
|
||||
}
|
||||
}
|
||||
|
||||
task compressJson(dependsOn: extractAnnotationsJar) {
|
||||
group "compact"
|
||||
description "Minifies all JSON files, stripping whitespace"
|
||||
|
||||
def jarPath = file(jar.archivePath)
|
||||
|
||||
def tempPath = File.createTempFile("input", ".jar", temporaryDir)
|
||||
tempPath.deleteOnExit()
|
||||
|
||||
def gson = new GsonBuilder().create()
|
||||
|
||||
doLast {
|
||||
// Copy over all files in the current jar to the new one, running json files from GSON. As pretty printing
|
||||
// is turned off, they should be minified.
|
||||
new ZipFile(jarPath).withCloseable { inJar ->
|
||||
new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tempPath))).withCloseable { outJar ->
|
||||
inJar.entries().each { entry ->
|
||||
if(entry.directory) {
|
||||
outJar.putNextEntry(entry)
|
||||
} else if(!entry.name.endsWith(".json")) {
|
||||
outJar.putNextEntry(entry)
|
||||
inJar.getInputStream(entry).withCloseable { outJar << it }
|
||||
} else {
|
||||
ZipEntry newEntry = new ZipEntry(entry.name)
|
||||
newEntry.setTime(entry.time)
|
||||
outJar.putNextEntry(newEntry)
|
||||
|
||||
def element = inJar.getInputStream(entry).withCloseable { gson.fromJson(it.newReader("UTF8"), JsonElement.class) }
|
||||
outJar.write(gson.toJson(element).getBytes(StandardCharsets.UTF_8))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// And replace the original jar again
|
||||
Files.move(tempPath.toPath(), jarPath.toPath(), StandardCopyOption.REPLACE_EXISTING)
|
||||
}
|
||||
}
|
||||
|
||||
assemble.dependsOn compressJson
|
||||
|
||||
curseforge {
|
||||
apiKey = project.hasProperty('curseForgeApiKey') ? project.curseForgeApiKey : ''
|
||||
|
Before Width: | Height: | Size: 230 B After Width: | Height: | Size: 190 B |
Before Width: | Height: | Size: 222 B After Width: | Height: | Size: 181 B |
Before Width: | Height: | Size: 643 B After Width: | Height: | Size: 641 B |
Before Width: | Height: | Size: 421 B After Width: | Height: | Size: 247 B |
Before Width: | Height: | Size: 501 B After Width: | Height: | Size: 268 B |
Before Width: | Height: | Size: 439 B After Width: | Height: | Size: 253 B |
Before Width: | Height: | Size: 327 B After Width: | Height: | Size: 217 B |
Before Width: | Height: | Size: 290 B After Width: | Height: | Size: 195 B |
Before Width: | Height: | Size: 312 B After Width: | Height: | Size: 205 B |
Before Width: | Height: | Size: 327 B After Width: | Height: | Size: 218 B |
Before Width: | Height: | Size: 332 B After Width: | Height: | Size: 296 B |
Before Width: | Height: | Size: 333 B After Width: | Height: | Size: 294 B |
Before Width: | Height: | Size: 327 B After Width: | Height: | Size: 217 B |
Before Width: | Height: | Size: 290 B After Width: | Height: | Size: 195 B |
Before Width: | Height: | Size: 273 B After Width: | Height: | Size: 179 B |
Before Width: | Height: | Size: 290 B After Width: | Height: | Size: 195 B |
Before Width: | Height: | Size: 278 B After Width: | Height: | Size: 186 B |
Before Width: | Height: | Size: 381 B After Width: | Height: | Size: 236 B |
Before Width: | Height: | Size: 273 B After Width: | Height: | Size: 193 B |
Before Width: | Height: | Size: 259 B After Width: | Height: | Size: 224 B |
Before Width: | Height: | Size: 250 B After Width: | Height: | Size: 175 B |
Before Width: | Height: | Size: 279 B After Width: | Height: | Size: 186 B |
Before Width: | Height: | Size: 327 B After Width: | Height: | Size: 217 B |
Before Width: | Height: | Size: 314 B After Width: | Height: | Size: 212 B |
Before Width: | Height: | Size: 290 B After Width: | Height: | Size: 202 B |
Before Width: | Height: | Size: 312 B After Width: | Height: | Size: 210 B |
Before Width: | Height: | Size: 306 B After Width: | Height: | Size: 199 B |
Before Width: | Height: | Size: 286 B After Width: | Height: | Size: 188 B |
Before Width: | Height: | Size: 310 B After Width: | Height: | Size: 212 B |
Before Width: | Height: | Size: 287 B After Width: | Height: | Size: 203 B |
Before Width: | Height: | Size: 327 B After Width: | Height: | Size: 217 B |
Before Width: | Height: | Size: 258 B After Width: | Height: | Size: 234 B |
Before Width: | Height: | Size: 288 B After Width: | Height: | Size: 202 B |
Before Width: | Height: | Size: 263 B After Width: | Height: | Size: 235 B |
Before Width: | Height: | Size: 222 B After Width: | Height: | Size: 179 B |
Before Width: | Height: | Size: 266 B After Width: | Height: | Size: 221 B |
Before Width: | Height: | Size: 290 B After Width: | Height: | Size: 189 B |
Before Width: | Height: | Size: 261 B After Width: | Height: | Size: 224 B |
Before Width: | Height: | Size: 287 B After Width: | Height: | Size: 186 B |
Before Width: | Height: | Size: 310 B After Width: | Height: | Size: 212 B |
Before Width: | Height: | Size: 286 B After Width: | Height: | Size: 188 B |
Before Width: | Height: | Size: 306 B After Width: | Height: | Size: 199 B |
Before Width: | Height: | Size: 327 B After Width: | Height: | Size: 216 B |
Before Width: | Height: | Size: 337 B After Width: | Height: | Size: 302 B |
Before Width: | Height: | Size: 325 B After Width: | Height: | Size: 214 B |
Before Width: | Height: | Size: 330 B After Width: | Height: | Size: 217 B |
Before Width: | Height: | Size: 327 B After Width: | Height: | Size: 217 B |
Before Width: | Height: | Size: 290 B After Width: | Height: | Size: 195 B |
Before Width: | Height: | Size: 493 B After Width: | Height: | Size: 352 B |
Before Width: | Height: | Size: 327 B After Width: | Height: | Size: 217 B |
Before Width: | Height: | Size: 290 B After Width: | Height: | Size: 195 B |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 619 B After Width: | Height: | Size: 294 B |
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 316 B After Width: | Height: | Size: 206 B |
Before Width: | Height: | Size: 254 B After Width: | Height: | Size: 241 B |
Before Width: | Height: | Size: 316 B After Width: | Height: | Size: 282 B |
Before Width: | Height: | Size: 253 B After Width: | Height: | Size: 239 B |
Before Width: | Height: | Size: 324 B After Width: | Height: | Size: 210 B |
Before Width: | Height: | Size: 259 B After Width: | Height: | Size: 235 B |
Before Width: | Height: | Size: 974 B After Width: | Height: | Size: 426 B |
Before Width: | Height: | Size: 989 B After Width: | Height: | Size: 436 B |
Before Width: | Height: | Size: 992 B After Width: | Height: | Size: 430 B |
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 490 B |
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 241 B After Width: | Height: | Size: 223 B |
Before Width: | Height: | Size: 211 B After Width: | Height: | Size: 200 B |
Before Width: | Height: | Size: 299 B After Width: | Height: | Size: 260 B |
Before Width: | Height: | Size: 212 B After Width: | Height: | Size: 200 B |
Before Width: | Height: | Size: 638 B After Width: | Height: | Size: 516 B |