u
This commit is contained in:
1 parent
6300778150
commit
e893ef2e48
27 files changed
+870
-33
No files matched your search
@@ -0,0 +1,95 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const { withAppBuildGradle } = require("expo/config-plugins");
|
||||
|
||||
const CREDENTIAL_PATH = path.resolve(
|
||||
__dirname,
|
||||
"../credentials/credentials.json"
|
||||
);
|
||||
|
||||
const KEYSTORE_PATH = path.resolve(
|
||||
__dirname,
|
||||
"../credentials/oneao-android.keystore"
|
||||
);
|
||||
|
||||
module.exports = function withAndroidSignature(config) {
|
||||
return withAppBuildGradle(config, (config) => {
|
||||
if (config.modResults.language !== "groovy") {
|
||||
throw new Error("only groovy build.gradle is supported");
|
||||
}
|
||||
|
||||
config.modResults.contents = setAndroidSignature(
|
||||
config.modResults.contents
|
||||
);
|
||||
|
||||
return config;
|
||||
});
|
||||
};
|
||||
|
||||
// ======================== 主入口 ========================
|
||||
function setAndroidSignature(appBuildGradle) {
|
||||
if (!fs.existsSync(CREDENTIAL_PATH)) {
|
||||
console.warn("⚠️ credentials.json 不存在,跳过签名配置");
|
||||
return appBuildGradle;
|
||||
}
|
||||
|
||||
const info = JSON.parse(
|
||||
fs.readFileSync(CREDENTIAL_PATH, "utf8")
|
||||
);
|
||||
|
||||
let output = appBuildGradle;
|
||||
|
||||
// ⭐ 1. 重写 signingConfigs(关键)
|
||||
output = replaceSigningConfigs(output, info);
|
||||
|
||||
// ⭐ 2. 确保所有 debug/release 使用统一 keystore
|
||||
output = unifySigningUsage(output);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// ======================== ⭐核心:重写 signingConfigs ========================
|
||||
function replaceSigningConfigs(appBuildGradle, info) {
|
||||
const block = `
|
||||
signingConfigs {
|
||||
release {
|
||||
storeFile file(${JSON.stringify(KEYSTORE_PATH)})
|
||||
storePassword ${JSON.stringify(info.android.keystore.keystorePassword)}
|
||||
keyAlias ${JSON.stringify(info.android.keystore.keyAlias)}
|
||||
keyPassword ${JSON.stringify(info.android.keystore.keyPassword)}
|
||||
}
|
||||
|
||||
debug {
|
||||
storeFile file(${JSON.stringify(KEYSTORE_PATH)})
|
||||
storePassword ${JSON.stringify(info.android.keystore.keystorePassword)}
|
||||
keyAlias ${JSON.stringify(info.android.keystore.keyAlias)}
|
||||
keyPassword ${JSON.stringify(info.android.keystore.keyPassword)}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const regex = /signingConfigs\s*\{[\s\S]*?\n\s*\}/m;
|
||||
|
||||
if (regex.test(appBuildGradle)) {
|
||||
return appBuildGradle.replace(regex, block.trim());
|
||||
}
|
||||
|
||||
// 没有就插入
|
||||
return appBuildGradle.replace(
|
||||
/(android\s*\{)/,
|
||||
`$1\n${block.trim()}`
|
||||
);
|
||||
}
|
||||
|
||||
// ======================== ⭐统一使用 signingConfig ========================
|
||||
function unifySigningUsage(appBuildGradle) {
|
||||
let output = appBuildGradle;
|
||||
|
||||
// debug -> release(你如果想 debug 也用 release,可以改这里)
|
||||
output = output.replace(
|
||||
/signingConfig\s+signingConfigs\.debug/g,
|
||||
"signingConfig signingConfigs.release"
|
||||
);
|
||||
|
||||
return output;
|
||||
}
|
||||
Reference in new issue
Block a user