List Android assets at build time for the installer

- add generateAssetFileList gradle task mirroring aapt packaging rules
- read the asset list in the installer
This commit is contained in:
dobbry-vechur
2026-10-09 03:34:04 +04:00
parent f86d3fa641
commit d39ddb515b
2 changed files with 70 additions and 6 deletions
+38
View File
@@ -273,10 +273,47 @@ task createWindowsJunctionLinks() {
}
}
// asset list for the installer, so it needn't walk the APK. rules mirror aapt
// packaging: default ignore patterns, .gz files stored without the suffix, no broken links
def assetFileListDir = "$buildDir/generated/assetFileList"
task generateAssetFileList {
dependsOn makeLocalization
dependsOn createWindowsJunctionLinks
def listFile = file("$assetFileListDir/android/asset_files.txt")
outputs.file listFile
outputs.upToDateWhen { false }
doLast {
def assetRoot = file(OperatingSystem.current() == OperatingSystem.WINDOWS ? assetsDirWindows : assetsDir)
def packaged = { String name, boolean isDir ->
def lower = name.toLowerCase()
!( lower.startsWith('.') || ( isDir && lower.startsWith('_') ) ||
lower in ['cvs', 'thumbs.db', 'picasa.ini'] || lower.endsWith('.scc') || lower.endsWith('~') )
}
def assetFiles = new TreeSet<String>()
def walk
walk = { File dir, String path ->
dir.listFiles().each { File child ->
String childPath = path + "/" + child.name
if (child.isDirectory()) {
if (packaged(child.name, true)) {
walk(child, childPath)
}
} else if (child.isFile() && packaged(child.name, false)) {
assetFiles << ( childPath.toLowerCase().endsWith('.gz') ? childPath[0..-4] : childPath )
}
}
}
['data', 'gfx', 'lang'].each { top -> walk(new File(assetRoot, top), top) }
listFile.parentFile.mkdirs()
listFile.text = assetFiles.join('\n') + '\n'
}
}
preBuild.dependsOn fetchSdl3Aars
preBuild.dependsOn makeLocalization
preBuild.dependsOn generateVersionHeader
preBuild.dependsOn createWindowsJunctionLinks
preBuild.dependsOn generateAssetFileList
createWindowsJunctionLinks.dependsOn makeLocalization
android {
@@ -445,6 +482,7 @@ android {
if (OperatingSystem.current() == OperatingSystem.WINDOWS) {
sourceSets.main.assets.srcDirs = ['src/main/assetsWinHost']
}
sourceSets.main.assets.srcDir assetFileListDir
if (buildAsLibrary) {
libraryVariants.all { variant ->
@@ -7,6 +7,8 @@ import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
@@ -238,6 +240,7 @@ public class SplashScreen extends Activity {
private final byte[] buffer = new byte[256 * 1024];
// per-file progress updates flood the UI thread
private static final long PROGRESS_INTERVAL_MILLIS = 100;
private static final String ASSET_FILE_LIST = "android/asset_files.txt";
private long lastProgressMillis = 0;
private long copiedBytes = 0;
private int installedFiles = 0;
@@ -444,13 +447,15 @@ public class SplashScreen extends Activity {
long deleteMillis;
long copyMillis;
try {
// One walk of the asset tree: listing a path is slow, and every
// file used to be listed twice, once to count and once to copy.
// builds without the generated list fall back to walking the APK
long phaseStart = SystemClock.elapsedRealtime();
List<String> assetFiles = new ArrayList<String>();
collectAssetFiles(assetManager, "data", assetFiles);
collectAssetFiles(assetManager, "gfx", assetFiles);
collectAssetFiles(assetManager, "lang", assetFiles);
List<String> assetFiles = readAssetFileList(assetManager);
if (assetFiles == null) {
assetFiles = new ArrayList<String>();
collectAssetFiles(assetManager, "data", assetFiles);
collectAssetFiles(assetManager, "gfx", assetFiles);
collectAssetFiles(assetManager, "lang", assetFiles);
}
totalFiles = assetFiles.size();
countMillis = SystemClock.elapsedRealtime() - phaseStart;
publishProgress(installedFiles, totalFiles);
@@ -535,6 +540,27 @@ public class SplashScreen extends Activity {
}
}
// asset paths generated by generateAssetFileList at build time, or null if not
// written
private List<String> readAssetFileList(AssetManager assetManager) throws IOException {
InputStream in;
try {
in = assetManager.open(ASSET_FILE_LIST);
} catch (FileNotFoundException e) {
return null;
}
List<String> files = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.isEmpty()) {
files.add(line);
}
}
}
return files;
}
private void collectAssetFiles(AssetManager assetManager, String assetPath, List<String> out) throws Exception {
for (String file : assetManager.list(assetPath)) {
String filePath = assetPath + "/" + file;