Files
life-echo/app-expo/plugins/withAndroidReleaseSigning.js
2026-03-20 17:25:42 +08:00

90 lines
3.1 KiB
JavaScript

// @ts-check
/**
* When android/app/keystore.properties + store file exist (CI / local release),
* wire signingConfigs.release and use it for assembleRelease.
* Pass -PversionName / -PversionCode to override defaultConfig.
*/
const { withAppBuildGradle } = require('@expo/config-plugins');
function withAndroidReleaseSigning(config) {
return withAppBuildGradle(config, (mod) => {
let contents = mod.modResults.contents;
const inject = `def keystorePropertiesFile = file("keystore.properties")
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
`;
if (!contents.includes('keystorePropertiesFile')) {
contents = contents.replace(/^android \{/m, `${inject}android {`);
}
const oldSigning = ` signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}`;
const newSigning = ` signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
release {
if (keystorePropertiesFile.exists()) {
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
}
}
}`;
if (!contents.includes(oldSigning)) {
throw new Error(
'[withAndroidReleaseSigning] Default signingConfigs block not found; update plugin for current Expo prebuild template.',
);
}
contents = contents.replace(oldSigning, newSigning);
const oldReleaseSigningLine = ` release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
signingConfig signingConfigs.debug`;
const newReleaseSigningLine = ` release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
signingConfig (keystorePropertiesFile.exists() ? signingConfigs.release : signingConfigs.debug)`;
if (!contents.includes(oldReleaseSigningLine)) {
throw new Error(
'[withAndroidReleaseSigning] Default release signingConfig line not found; update plugin for current Expo prebuild template.',
);
}
contents = contents.replace(oldReleaseSigningLine, newReleaseSigningLine);
contents = contents.replace(
/versionCode \d+/,
'versionCode (project.hasProperty("versionCode") ? project.property("versionCode").toInteger() : 1)',
);
contents = contents.replace(
/versionName "[^"]+"/,
'versionName (project.hasProperty("versionName") ? project.property("versionName") : "1.0.0")',
);
mod.modResults.contents = contents;
return mod;
});
}
module.exports = withAndroidReleaseSigning;