Last active
March 22, 2025 03:39
-
-
Save theprashant-one/2caa96dfcb5e9958a7d9058c0d10c94e to your computer and use it in GitHub Desktop.
A Config Plugin for Expo that disables bundle compression in build.gradle by ensuring "bundle" is included in noCompress. Reference (https://x.com/margelo_io/status/1890789725872525381?t=sZABYYbVSb4Re3A8aww7Ew&s=19)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { withAppBuildGradle } = require("@expo/config-plugins") | |
/** | |
* A Config Plugin to disable bundle compression in Android build.gradle. | |
* @param {import('@expo/config-plugins').ConfigPlugin} config | |
* @returns {import('@expo/config-plugins').ConfigPlugin} | |
*/ | |
const withDisableBundleCompression = (config) => { | |
return withAppBuildGradle(config, (config) => { | |
let buildGradle = config.modResults.contents | |
const hasAndroidResources = buildGradle.includes("androidResources {") | |
const hasNoCompress = buildGradle.includes("noCompress") | |
if (hasAndroidResources && hasNoCompress) { | |
if ( | |
buildGradle.includes('noCompress += ["bundle"]') || | |
buildGradle.includes("noCompress += 'bundle'") || | |
buildGradle.includes('noCompress += "bundle"') | |
) { | |
return config | |
} | |
const lines = buildGradle.split("\n") | |
const modifiedLines = lines.map((line) => { | |
if (line.trim().startsWith("noCompress")) { | |
if (line.includes("+=")) { | |
return line.replace(/\]/, ', "bundle"]') | |
} else if (line.includes("=")) { | |
return line.replace("=", '+= ["bundle",') + "]" | |
} | |
} | |
return line | |
}) | |
config.modResults.contents = modifiedLines.join("\n") | |
} else { | |
const androidBlock = buildGradle.indexOf("android {") | |
if (androidBlock !== -1) { | |
const insertPosition = buildGradle.indexOf("\n", androidBlock) + 1 | |
const newContent = | |
buildGradle.slice(0, insertPosition) + | |
" androidResources {\n" + | |
' noCompress += ["bundle"]\n' + | |
" }\n" + | |
buildGradle.slice(insertPosition) | |
config.modResults.contents = newContent | |
} | |
} | |
return config | |
}) | |
} | |
module.exports = withDisableBundleCompression |
Thanks for the improvements! The error handling for missing android/androidResources blocks makes it much more safe.
Hey this is great - I made a few adjustments to also support existing
androidResources
blocks: https://gist.github.com/mrousavy/d8d15eb1b8cffe60765f7fa7d67d6767
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey this is great - I made a few adjustments to also support existing
androidResources
blocks: https://gist.github.com/mrousavy/d8d15eb1b8cffe60765f7fa7d67d6767