55 lines
2.0 KiB
JavaScript
55 lines
2.0 KiB
JavaScript
const { withGradleProperties, withAppBuildGradle, withDangerousMod } = require('@expo/config-plugins');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
module.exports = function withOptimization(config) {
|
|
// 1. Gradle properties: Enable resource shrinking & specify NDK version
|
|
config = withGradleProperties(config, (config) => {
|
|
if (!config.modResults.some((item) => item.key === 'android.enableShrinkResourcesInReleaseBuilds')) {
|
|
config.modResults.push({
|
|
type: 'property',
|
|
key: 'android.enableShrinkResourcesInReleaseBuilds',
|
|
value: 'true',
|
|
});
|
|
}
|
|
if (!config.modResults.some((item) => item.key === 'android.ndkVersion')) {
|
|
config.modResults.push({
|
|
type: 'property',
|
|
key: 'android.ndkVersion',
|
|
value: '28.0.12932649',
|
|
});
|
|
}
|
|
return config;
|
|
});
|
|
|
|
// 2. build.gradle: Switch to proguard-android-optimize.txt to activate R8 optimizations
|
|
config = withAppBuildGradle(config, (config) => {
|
|
if (config.modResults.language === 'groovy') {
|
|
let contents = config.modResults.contents;
|
|
contents = contents.replace(
|
|
/getDefaultProguardFile\(["']proguard-android\.txt["']\)/g,
|
|
'getDefaultProguardFile("proguard-android-optimize.txt")'
|
|
);
|
|
config.modResults.contents = contents;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
// 3. Keep xml resources from being aggressively stripped by resource shrinker
|
|
config = withDangerousMod(config, [
|
|
'android',
|
|
async (config) => {
|
|
const rawDir = path.join(config.modRequest.platformProjectRoot, 'app/src/main/res/raw');
|
|
if (!fs.existsSync(rawDir)) {
|
|
fs.mkdirSync(rawDir, { recursive: true });
|
|
}
|
|
const keepPath = path.join(rawDir, 'keep.xml');
|
|
const xmlContent = `<?xml version="1.0" encoding="utf-8"?>\n<resources xmlns:tools="http://schemas.android.com/tools"\n tools:keep="@xml/*,@drawable/*" />\n`;
|
|
fs.writeFileSync(keepPath, xmlContent);
|
|
return config;
|
|
},
|
|
]);
|
|
|
|
return config;
|
|
};
|