生成 Android 项目,确保首次完整构建即可编译通过,包括可选的 JNI/NDK/CMake 原生配置。适用于新建 Android 应用、配置...
Ensure AI-generated Android projects compile successfully on the first attempt by providing:
Before using this skill on a production or sensitive machine, verify:
python, java, and at least one of gradle/ gradlew.JAVA_HOME (recommended), ANDROID_HOME or ANDROID_SDK_ROOT, optional ANDROID_NDK_HOME or NDK_HOME for native workflows.adb install, adb shell am start) are allowed for connected devices/emulators.This skill runs local commands and may perform build/device operations; treat it as operational code, not documentation-only guidance. It does not request API keys, tokens, or cloud credentials by design.
This skill does not require cloud credentials, API keys, or tokens.
It does rely on local environment variables and installed binaries/tooling:
JAVA_HOME, ANDROID_HOME/ ANDROID_SDK_ROOT, and for native projects ANDROID_NDK_HOME/ NDK_HOME.java, gradle/ gradlew, Android SDK/NDK on disk, and optional adb for install/launch verification.The scripts do not intentionally exfiltrate data, but they do run subprocesses that may:
adb)This repository is a skill package plus its own development test harness. Only the following paths are part of the skill:
SKILL.md, README.md, LICENSEreferences/ — version matrix and configuration templatesscripts/detect_env.py, scripts/project_validator.py, scripts/build_flow.pytests/, cache/, and reports/ are development-only and are excluded from the
release package (see .skillignore). All self-test tooling — test runner, report
generator, report renderer, and the sitecustomize cache helper — lives under
tests/.
The skill workflow never imports from or executes anything under tests/. If a
distributed copy of this package contains tests/, that is a packaging error, not
skill behaviour.
The skill requests no tool allow-list beyond ordinary local execution. Its explicit boundaries are:
DimensionAllowedNot allowedReadsSKILL.md, references/, the target project directory, and the environment variables listed belowUnrelated files outside the target projectWritesOnly the generated project directory ( settings.gradle.kts, build.gradle.kts, app/**, wrapper files) and local.propertiesAny path outside the generated projectExecutesjava -version, gradle/ gradlew, adb (install/launch only)Arbitrary shells, package installs, credential or key accessNetworkGradle wrapper and dependency resolutionAny upload or exfiltration of local dataCredentialsNone required, none requestedAPI keys, tokens, cloud credentialsEnv vars readJAVA_HOME, ANDROID_HOME/ ANDROID_SDK_ROOT, ANDROID_NDK_HOME/ NDK_HOMEAnything else
All subprocess invocations use argument lists (never a shell string), so no caller-controlled input is interpolated into a command line.
If the host policy forbids local command execution or device interaction, stop after
Phase 4 (file generation) and report the project as scaffolding_only.
Trigger this skill when:
build.gradle.kts, settings.gradle.kts)JNI, NDK, CMake, externalNativeBuild)If the project is meant to build on the current machine, do not generate files yet. Audit the environment first:
bash
python scripts/detect_env.py
Treat this phase as mandatory for local project generation because most "AI generated Android project won't compile" failures are environment mismatches, not template mistakes.
This outputs:
JAVA_HOME configuration and versionPATH java executable and versionJAVA_HOME vs PATH)If ready_for_build is false:
If the environment status is degraded:
JAVA_HOME is missing, so Gradle is falling back to PATHBase the profile on audited environment data, not guesswork:
ProfileAGPGradleJDKUse Casestable8.7.08.917+Recommended for most projectslatest9.1.09.3.117+New features, Android 16+legacy7.4.27.511+Older environments
Default to "stable" profile only when the environment is ready for it.
Use this decision order:
platforms/android-35, and build-tools are present → use stablelatestlegacyNative 触发策略( native_enabled):
native_enabled = falseJNI、 NDK、 CMake、 externalNativeBuild、 C++、 native library、 原生模块)→ 自动切到 native_enabled = truenative、 底层、 性能优化)→ 先标记候选并二次确认,再决定是否开启native_enabled=true/false),显式配置优先于关键词推断Additional JDK rules:
JAVA_HOME over an accidental PATH javaJAVA_HOME and PATH java disagree, treat that as a reproducibility risk and explain itAlways create the complete file structure:
text
ProjectName/
├── settings.gradle.kts
├── build.gradle.kts
├── gradle.properties
├── gradle/
│ └── wrapper/
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── app/
│ ├── build.gradle.kts
│ ├── src/
│ │ └── main/
│ │ ├── AndroidManifest.xml
│ │ ├── java/com/example/project/
│ │ │ └── MainActivity.kt
│ │ └── res/
│ │ ├── layout/
│ │ │ └── activity_main.xml
│ │ ├── values/
│ │ │ ├── strings.xml
│ │ │ └── themes.xml
│ │ └── mipmap-*/
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ └── proguard-rules.pro
└── local.properties (create when sdk path is known and immediate CLI build is expected)
Important:
local.properties with sdk.dir=... to reduce local environment ambiguityGRADLE_LOCAL_JAVA_HOME or equivalent project-scoped JDK bindingLoad templates from references/config-templates.md:
MyApp, com.example.myapp, com.example.myapp.MainActivitynamespace, applicationId, source package path, and theme names alignedlocal.propertiesBefore reporting the project as buildable, ensure the wrapper is genuinely usable:
gradlewgradlew.batgradle/wrapper/gradle-wrapper.propertiesgradle/wrapper/gradle-wrapper.jargradle-wrapper.properties file alone is not enoughUse one of these approaches:
gradle wrapper --gradle-version <target-version> if Gradle is installedassembleDebugYou can use scripts/project_validator.py logic as the acceptance bar:
After generating all files, MUST run:
bash
./gradlew assembleDebug
If compilation fails:
references/version-matrix.mdassembleDebug passes with the generated projectIf the wrapper is unavailable or the environment is missing mandatory SDK components:
After assembleDebug, verify that the debug APK really exists:
text
app/build/outputs/apk/debug/app-debug.apk
Use scripts/build_flow.py concepts as the final status model:
scaffolding_only
build_failed
assembleDebug failed or no APK was producedcompiled
assembleDebug succeeded and the debug APK existsIf a device or emulator is available, continue one more step:
runnableAdditional final states:
install_failed
launch_failed
runnable
Only the compiled state should be described as:
Only the runnable state should be described as:
See references/version-matrix.md for:
Key rules:
See references/config-templates.md for:
settings.gradle.kts templatebuild.gradle.ktsgradle.properties defaultsgradle-wrapper.properties formatMainActivity.kt and AndroidManifest.xmlscripts/detect_env.py provides:
java -version output)Run this script to adapt the generated project to the user's actual environment.
./gradlew assembleDebugbuild.gradle.ktslocal.properties when immediate local CLI build is required and sdk path is knownJAVA_HOME, Android Studio Gradle JDK, and project-level Gradle JDK settingsgradle/libs.versions.toml for larger projectsMistakeFixMissing namespace in app/build.gradle.ktsAdd namespace = "com.example.myapp" in android {} blockWrong JDK version for AGP 8.xUse AGP 7.x if JDK < 17, or instruct user to install JDK 17Inconsistent AGP/Gradle versionsCheck version-matrix.md, use recommended pairsMissing or fake Gradle Wrapper filesAlways include gradlew, gradlew.bat, gradle-wrapper.properties, and gradle-wrapper.jarWrong compileSdk for AGP versionAGP 8.7+ requires compileSdk = 35, AGP 9.x requires compileSdk = 36Missing SDK platform or build-toolsCheck environment audit before generation; install missing SDK components firstBuild verified only in theoryDo not claim success without a real assembleDebug runJAVA_HOME missing but PATH java existsTreat as degraded, not fully stable; recommend explicit JDK configurationJAVA_HOME and PATH java point to different JDKsExplain that Gradle CLI will prefer JAVA_HOME, which can differ from shell expectationsassembleDebug succeeded but no APK foundTreat as build verification failure until app-debug.apk is confirmedAPK exists but install failsTreat as install_failed; do not claim the app is runnableInstall succeeds but launch failsTreat as launch_failed; do not claim runtime verification passed
User: "Create a new Android project called TodoApp"
AI Response:
detect_env.py (if possible)MyApp → TodoApp, com.example.myapp → com.example.todoapp./gradlew assembleDebugapp/build/outputs/apk/debug/app-debug.apk existscompiled only if build + APK verification passrunnable only if build + install + launch all passThis skill transforms Android project generation from a guessing game into an audited, verifiable build process.