diff --git a/.github/workflows/native-compile-platforms.yml b/.github/workflows/native-compile-platforms.yml index d58ee78085..5a8584b332 100644 --- a/.github/workflows/native-compile-platforms.yml +++ b/.github/workflows/native-compile-platforms.yml @@ -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 + + CocosGame + + 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 + + CocosGame + + EOF + mkdir -p build-android/assets ASSET_DIR=$GITHUB_WORKSPACE/templates/android/build/build-android/ diff --git a/scripts/native-pack-tool/source/base/cocosProjectTypes.ts b/scripts/native-pack-tool/source/base/cocosProjectTypes.ts index b1a51867d1..22f1490ef3 100644 --- a/scripts/native-pack-tool/source/base/cocosProjectTypes.ts +++ b/scripts/native-pack-tool/source/base/cocosProjectTypes.ts @@ -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 { diff --git a/scripts/native-pack-tool/source/base/default.ts b/scripts/native-pack-tool/source/base/default.ts index c4af0c7ac0..893e5659ba 100644 --- a/scripts/native-pack-tool/source/base/default.ts +++ b/scripts/native-pack-tool/source/base/default.ts @@ -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, '\\.'); diff --git a/scripts/native-pack-tool/source/platforms/android.ts b/scripts/native-pack-tool/source/platforms/android.ts index dcffbd9f7f..143559a2e1 100644 --- a/scripts/native-pack-tool/source/platforms/android.ts +++ b/scripts/native-pack-tool/source/platforms/android.ts @@ -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 = [ + ``, + ` ${this.params.projectName}`, + ``, + ]; + 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 { diff --git a/templates/android/template/app/build.gradle b/templates/android/template/app/build.gradle index ef96d26cde..32fe76d362 100644 --- a/templates/android/template/app/build.gradle +++ b/templates/android/template/app/build.gradle @@ -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" diff --git a/templates/android/template/build.gradle b/templates/android/template/build.gradle index 2ae589d169..659740eaac 100644 --- a/templates/android/template/build.gradle +++ b/templates/android/template/build.gradle @@ -20,9 +20,6 @@ allprojects { google() mavenCentral() // jcenter() // keeped as anchor, will be removed soon - flatDir { - dirs 'libs' - } } } diff --git a/templates/android/template/instantapp/build.gradle b/templates/android/template/instantapp/build.gradle index c8215aed8d..d6dc7be0ea 100644 --- a/templates/android/template/instantapp/build.gradle +++ b/templates/android/template/instantapp/build.gradle @@ -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" diff --git a/templates/cocos-project-template.json b/templates/cocos-project-template.json index aa147386c8..31d13e228e 100755 --- a/templates/cocos-project-template.json +++ b/templates/cocos-project-template.json @@ -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": [