Android support CJK app name (#15326)

This commit is contained in:
江战
2023-06-06 15:26:52 +08:00
committed by GitHub
parent 9b0996a17d
commit b898340ee0
8 changed files with 78 additions and 52 deletions
@@ -96,6 +96,13 @@ jobs:
echo "set(COCOS_X_PATH $NATIVE_ROOT)" >> build-android/proj/cfg.cmake
echo "set(CC_EXECUTABLE_NAME \"\")" >> build-android/proj/cfg.cmake
mkdir -p build-android/proj/res/values
cat << EOF > build-android/proj/res/values/strings.xml
<resources>
<string name="app_name" translatable="false">CocosGame</string>
</resources>
EOF
mkdir -p build-android/assets
ASSET_DIR=$GITHUB_WORKSPACE/templates/android/build/build-android/
@@ -163,6 +170,13 @@ jobs:
echo "set(COCOS_X_PATH $NATIVE_ROOT)" >> build-android/proj/cfg.cmake
echo "set(CC_EXECUTABLE_NAME \"\")" >> build-android/proj/cfg.cmake
mkdir -p build-android/proj/res/values
cat << EOF > build-android/proj/res/values/strings.xml
<resources>
<string name="app_name" translatable="false">CocosGame</string>
</resources>
EOF
mkdir -p build-android/assets
ASSET_DIR=$GITHUB_WORKSPACE/templates/android/build/build-android/
@@ -8,50 +8,21 @@ export interface CocosProject {
export interface CocosProjectTasks {
excludeFromTemplate?: string[],
appendFromTemplate?: {
from: string;
to: string;
exclude?: string[];
},
appendFile?: { from: string, to: string }[],
appendXEngine?: {
from: string; to: string
},
projectRename: {
srcProjectName: string;
files: []
},
projectReplaceProjectName?: {
srcProjectName: string,
files: string[];
},
projectReplaceProjectNameASCII?: {
srcProjectName: string,
files: string[];
},
projectReplacePackageName?: {
srcPackageName: string;
files: string[]
},
projectReplaceMacBundleid?: {
srcBundleId: string;
files: string[]
},
projectReplaceIosBundleid?: {
srcBundleId: string,
files: string[]
},
projectReplaceCocosRoot?: {
pattern: string;
files: (string | { file: string, default?: string, link?: string, needFix?: boolean })[];
},
projectReplaceTemplatePath?: {
pattern: string;
files: (string | { file: string, default?: string, link?: string, needFix?: boolean })[];
},
projectReplaceProjectCommon?: {
pattern: string;
files: (string | { file: string, default?: string, link?: string, needFix?: boolean })[];
}
commonReplace?: { pattern: string, value: string, files: string[] }[];
}
export interface CocosProjectConfig {
@@ -364,6 +364,10 @@ export abstract class NativePackTool {
}
}
protected projectNameASCII(): string {
return /^[0-9a-zA-Z_-]+$/.test(this.params.projectName) ? this.params.projectName : 'CocosGame';
}
protected async excuteTemplateTask(tasks: CocosProjectTasks) {
if (tasks.appendFile) {
await Promise.all(tasks.appendFile.map((task) => {
@@ -390,6 +394,21 @@ export abstract class NativePackTool {
delete tasks.projectReplaceProjectName;
}
if (tasks.projectReplaceProjectNameASCII) {
const cmd = tasks.projectReplaceProjectNameASCII;
if (cmd.srcProjectName !== this.projectNameASCII()) {
cmd.files.forEach((file) => {
const fp = cchelper.join(this.paths.buildDir, file);
replaceFilesDelay[fp] = replaceFilesDelay[fp] || [];
replaceFilesDelay[fp].push({
reg: cmd.srcProjectName,
content: this.projectNameASCII(),
});
});
}
delete tasks.projectReplaceProjectNameASCII;
}
if (tasks.projectReplacePackageName) {
const cmd = tasks.projectReplacePackageName;
const name = cmd.srcPackageName.replace(/\./g, '\\.');
@@ -64,6 +64,7 @@ export class AndroidPackTool extends NativePackTool {
await this.encrypteScripts();
await this.updateAndroidGradleValues();
await this.configAndroidInstant();
await this.generateAppNameValues();
return true;
}
@@ -85,7 +86,7 @@ export class AndroidPackTool extends NativePackTool {
const outputMode = this.params.debug ? 'Debug' : 'Release';
// compile android
buildMode = `${this.params.projectName}:assemble${outputMode}`;
buildMode = `${this.projectNameASCII()}:assemble${outputMode}`;
// await cchelper.runCmd(gradle, [buildMode /* "--quiet",*/ /*"--build-cache", "--project-cache-dir", nativePrjDir */], false, projDir);
// pushd
@@ -229,7 +230,7 @@ export class AndroidPackTool extends NativePackTool {
content = content.replace(/:/g, '\\:');
}
fs.writeFileSync(cchelper.join(ps.dirname(gradlePropertyPath), 'local.properties'), content, {encoding: 'utf8'});
fs.writeFileSync(cchelper.join(ps.dirname(gradlePropertyPath), 'local.properties'), content, { encoding: 'utf8' });
} else {
console.log(`warning: ${gradlePropertyPath} not found!`);
}
@@ -263,6 +264,21 @@ export class AndroidPackTool extends NativePackTool {
fs.writeFileSync(manifestPath, manifest, 'utf8');
}
private async generateAppNameValues() {
const valuesPath = cchelper.join(this.paths.platformTemplateDirInPrj, 'res/values/strings.xml');
const matchCnt = fs.readFileSync(valuesPath, 'utf8').toString().split('\n').map(x => x.trim()).filter(x => /name=\"app_name\"/.test(x)).length;
if (matchCnt == 0) { // should generate
const content = [
`<resources>`,
` <string name="app_name" translatable="false">${this.params.projectName}</string>`,
`</resources>`,
];
const dir = ps.join(this.paths.buildDir, 'proj/res/values');
await fs.ensureDir(dir);
await fs.writeFileSync(ps.join(dir, `strings.xml`), content.join('\n'), 'utf8');
}
}
/**
* 到对应目录拷贝文件到工程发布目录
*/
@@ -272,8 +288,8 @@ export class AndroidPackTool extends NativePackTool {
const suffix = this.params.debug ? 'debug' : 'release';
const destDir: string = ps.join(this.paths.buildDir, 'publish', suffix);
fs.ensureDirSync(destDir);
let apkName = `${this.params.projectName}-${suffix}.apk`;
let apkPath = ps.join(this.paths.nativePrjDir, `build/${this.params.projectName}/outputs/apk/${suffix}/${apkName}`);
let apkName = `${this.projectNameASCII()}-${suffix}.apk`;
let apkPath = ps.join(this.outputsDir(), `apk/${suffix}/${apkName}`);
if (!fs.existsSync(apkPath)) {
throw new Error(`apk not found at ${apkPath}`);
}
@@ -289,7 +305,7 @@ export class AndroidPackTool extends NativePackTool {
if (options.appBundle) {
apkName = `${this.params.projectName}-${suffix}.aab`;
apkPath = ps.join(this.paths.nativePrjDir, `build/${this.params.projectName}/outputs/bundle/${suffix}/${apkName}`);
apkPath = ps.join(this.outputsDir(), `bundle/${suffix}/${apkName}`);
if (!fs.existsSync(apkPath)) {
throw new Error(`instant apk not found at ${apkPath}`);
}
@@ -315,10 +331,15 @@ export class AndroidPackTool extends NativePackTool {
getApkPath() {
const suffix = this.params.debug ? 'debug' : 'release';
const apkName = `${this.params.projectName}-${suffix}.apk`;
return ps.join(
this.paths.nativePrjDir,
`build/${this.params.projectName}/outputs/apk/${suffix}/${apkName}`);
const apkName = `${this.projectNameASCII()}-${suffix}.apk`;
return ps.join(this.outputsDir(), `apk/${suffix}/${apkName}`);
}
private outputsDir() {
const folderName = this.projectNameASCII();
const targetDir = ps.join(this.paths.nativePrjDir, 'build', folderName);
const fallbackDir = ps.join(this.paths.nativePrjDir, 'build', this.params.projectName);
return ps.join(fs.existsSync(targetDir) ? targetDir : fallbackDir, 'outputs');
}
async install(): Promise<boolean> {
+1 -2
View File
@@ -23,7 +23,6 @@ android {
targetSdkVersion PROP_TARGET_SDK_VERSION
versionCode 1
versionName "1.0"
resValue "string", "app_name", PROP_APP_NAME
externalNativeBuild {
cmake {
@@ -36,7 +35,7 @@ android {
sourceSets.main {
java.srcDirs "../src", "src"
res.srcDirs "../res", 'res'
res.srcDirs "../res", 'res', "${RES_PATH}/proj/res"
jniLibs.srcDirs "../libs", 'libs'
manifest.srcFile "AndroidManifest.xml"
assets.srcDir "${RES_PATH}/data"
-3
View File
@@ -20,9 +20,6 @@ allprojects {
google()
mavenCentral()
// jcenter() // keeped as anchor, will be removed soon
flatDir {
dirs 'libs'
}
}
}
@@ -4,7 +4,7 @@ apply plugin: 'com.android.application'
RES_PATH = RES_PATH.replace("\\", "/")
COCOS_ENGINE_PATH = COCOS_ENGINE_PATH.replace("\\", "/")
buildDir = "${RES_PATH}/proj/build/${project.name ==~ /^[_a-zA-Z0-9-]+$/ ? project.name : 'CocosGame'}Instant"
buildDir = "${RES_PATH}/proj/build/instantapp"
android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
@@ -23,7 +23,6 @@ android {
targetSdkVersion PROP_TARGET_SDK_VERSION
versionCode 1
versionName "1.0"
resValue "string", "app_name", PROP_APP_NAME
externalNativeBuild {
cmake {
@@ -37,7 +36,7 @@ android {
sourceSets.main {
java.srcDirs "../src", "src"
res.srcDirs "../res", 'res'
res.srcDirs "../res", 'res', "${RES_PATH}/proj/res"
jniLibs.srcDirs "../libs", 'libs'
manifest.srcFile "AndroidManifest.xml"
assets.srcDir "${RES_PATH}/data"
+7 -1
View File
@@ -5,11 +5,17 @@
"srcProjectName": "CocosGame",
"files": [
"proj/main.cpp",
"proj/settings.gradle",
"proj/main.m",
"${NATIVE_DIR}/CMakeLists.txt"
]
},
"projectReplaceProjectNameASCII": {
"srcProjectName": "CocosGame",
"files": [
"proj/settings.gradle"
]
},
"projectReplacePackageName": {
"srcPackageName": "com.cocos.helloworld",
"files": [