u
This commit is contained in:
1 parent
b6415c0d65
commit
89aaef1d6b
29 files changed
+1118
-277
No files matched your search
@@ -1,95 +1,47 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const { withAppBuildGradle } = require("expo/config-plugins");
|
||||
// plugins/withAndroidSignature.js
|
||||
|
||||
const CREDENTIAL_PATH = path.resolve(
|
||||
__dirname,
|
||||
"../credentials/credentials.json"
|
||||
);
|
||||
|
||||
const KEYSTORE_PATH = path.resolve(
|
||||
__dirname,
|
||||
"../credentials/oneao-android.keystore"
|
||||
);
|
||||
const fs = require('node:fs')
|
||||
const path = require('node:path')
|
||||
const { withAppBuildGradle } = require('expo/config-plugins')
|
||||
|
||||
module.exports = function withAndroidSignature(config) {
|
||||
return withAppBuildGradle(config, (config) => {
|
||||
if (config.modResults.language !== "groovy") {
|
||||
throw new Error("only groovy build.gradle is supported");
|
||||
if (config.modResults.language === 'groovy') {
|
||||
config.modResults.contents = setAndroidSignature(config.modResults.contents)
|
||||
}
|
||||
else {
|
||||
throw new Error('如果不是 groovy,则无法在 app/build.gradle 中设置 signingConfigs')
|
||||
}
|
||||
return config
|
||||
})
|
||||
}
|
||||
|
||||
config.modResults.contents = setAndroidSignature(
|
||||
config.modResults.contents
|
||||
);
|
||||
|
||||
return config;
|
||||
});
|
||||
};
|
||||
|
||||
// ======================== 主入口 ========================
|
||||
function setAndroidSignature(appBuildGradle) {
|
||||
if (!fs.existsSync(CREDENTIAL_PATH)) {
|
||||
console.warn("⚠️ credentials.json 不存在,跳过签名配置");
|
||||
return appBuildGradle;
|
||||
if (!fs.existsSync(path.resolve(__dirname, '../credentials/credentials.json'))) {
|
||||
console.warn('警告:没有设置正式版本的 Android Keystore 文件,因为 credentials.json 不存在。')
|
||||
return appBuildGradle
|
||||
}
|
||||
const info = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../credentials/credentials.json'), { encoding: 'utf8' }))
|
||||
|
||||
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 {
|
||||
// 使用正则表达式插入签名信息
|
||||
let output = appBuildGradle.replace(
|
||||
/(signingConfigs\s*\{)/,
|
||||
`$1
|
||||
release {
|
||||
storeFile file(${JSON.stringify(KEYSTORE_PATH)})
|
||||
storeFile file(${JSON.stringify(path.resolve(__dirname, '../credentials/oneao-android.keystore'))})
|
||||
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,可以改这里)
|
||||
// 使用正则表达式替换 signingConfig
|
||||
output = output.replace(
|
||||
/signingConfig\s+signingConfigs\.debug/g,
|
||||
"signingConfig signingConfigs.release"
|
||||
);
|
||||
/(release\s*\{)[^}]*?signingConfig\s+signingConfigs\.debug/,
|
||||
`$1
|
||||
signingConfig signingConfigs.release
|
||||
`,
|
||||
)
|
||||
|
||||
return output;
|
||||
}
|
||||
return output
|
||||
}
|
||||
Reference in new issue
Block a user