Add scaffold-new-android-project skill
Verifies the Android dev environment (JDK/SDK/cmdline-tools/Gradle), suggests runnable commands for anything missing without installing, and generates a minimal Kotlin+Compose skeleton that passes ./gradlew build. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
ac549e8a07
commit
4ac3954ebb
3 changed files with 381 additions and 0 deletions
96
skills/scaffold-new-android-project/scripts/doctor.sh
Executable file
96
skills/scaffold-new-android-project/scripts/doctor.sh
Executable file
|
|
@ -0,0 +1,96 @@
|
|||
#!/usr/bin/env bash
|
||||
# doctor.sh — inspect the Android/Gradle/JDK dev environment WITHOUT installing anything.
|
||||
# Prints a report and, where a tool is missing, prints copy-pasteable commands the USER can run.
|
||||
# Exit code: 0 if a usable environment was found (SDK + JDK 17+), 1 otherwise.
|
||||
set -uo pipefail
|
||||
|
||||
ok() { printf ' \033[32m✓\033[0m %s\n' "$1"; }
|
||||
warn() { printf ' \033[33m!\033[0m %s\n' "$1"; }
|
||||
bad() { printf ' \033[31m✗\033[0m %s\n' "$1"; }
|
||||
hdr() { printf '\n\033[1m%s\033[0m\n' "$1"; }
|
||||
|
||||
FAIL=0
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
hdr "JDK"
|
||||
if command -v java >/dev/null 2>&1; then
|
||||
JV=$(java -version 2>&1 | head -1)
|
||||
JMAJOR=$(java -version 2>&1 | head -1 | sed -E 's/.*version "([0-9]+).*/\1/')
|
||||
if [ "${JMAJOR:-0}" -ge 17 ] 2>/dev/null; then
|
||||
ok "java: $JV"
|
||||
[ -n "${JAVA_HOME:-}" ] && ok "JAVA_HOME=$JAVA_HOME" || warn "JAVA_HOME is unset (fine if 'java' works, but Gradle prefers it set)"
|
||||
else
|
||||
bad "java is $JV — Android Gradle Plugin needs JDK 17+"
|
||||
FAIL=1
|
||||
fi
|
||||
else
|
||||
bad "java not found on PATH"
|
||||
echo " Look for a JDK 17 here, then export JAVA_HOME + add to PATH:"
|
||||
echo " ls /usr/lib/jvm/ 2>/dev/null; ls /Library/Java/JavaVirtualMachines/ 2>/dev/null"
|
||||
echo " # export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64"
|
||||
echo " # export PATH=\"\$JAVA_HOME/bin:\$PATH\""
|
||||
FAIL=1
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
hdr "Android SDK"
|
||||
SDK=""
|
||||
for cand in "${ANDROID_HOME:-}" "${ANDROID_SDK_ROOT:-}" \
|
||||
"$HOME/Android/Sdk" "$HOME/Library/Android/sdk" \
|
||||
"/usr/lib/android-sdk" "/opt/android-sdk"; do
|
||||
[ -n "$cand" ] && [ -d "$cand" ] && { SDK="$cand"; break; }
|
||||
done
|
||||
|
||||
if [ -n "$SDK" ]; then
|
||||
ok "SDK root: $SDK"
|
||||
[ -n "${ANDROID_HOME:-}" ] && ok "ANDROID_HOME set" || warn "ANDROID_HOME unset — 'export ANDROID_HOME=$SDK'"
|
||||
else
|
||||
bad "No Android SDK found in ANDROID_HOME / ANDROID_SDK_ROOT or common locations."
|
||||
echo " Search for one (do NOT download unless the user asks):"
|
||||
echo " find \$HOME /opt /usr/lib -maxdepth 4 -type d -name platform-tools 2>/dev/null"
|
||||
echo " If found at <dir>/platform-tools, set:"
|
||||
echo " export ANDROID_HOME=<dir>; export PATH=\"\$ANDROID_HOME/platform-tools:\$ANDROID_HOME/cmdline-tools/latest/bin:\$PATH\""
|
||||
FAIL=1
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
hdr "SDK command-line tools"
|
||||
for tool in adb sdkmanager avdmanager; do
|
||||
P=$(command -v $tool 2>/dev/null)
|
||||
if [ -n "$P" ]; then ok "$tool: $P"
|
||||
elif [ -n "$SDK" ]; then
|
||||
HIT=$(find "$SDK" -name "$tool" -type f 2>/dev/null | head -1)
|
||||
[ -n "$HIT" ] && warn "$tool present but not on PATH: $HIT" || warn "$tool not found (sdkmanager can install it)"
|
||||
else warn "$tool not found"; fi
|
||||
done
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
hdr "Installed platforms & build-tools"
|
||||
if [ -n "$SDK" ]; then
|
||||
PLATS=$(ls "$SDK/platforms" 2>/dev/null | tr '\n' ' ')
|
||||
BT=$(ls "$SDK/build-tools" 2>/dev/null | tr '\n' ' ')
|
||||
[ -n "$PLATS" ] && ok "platforms: $PLATS" || warn "no platforms installed — e.g. 'sdkmanager \"platforms;android-34\"'"
|
||||
[ -n "$BT" ] && ok "build-tools: $BT" || warn "no build-tools installed — e.g. 'sdkmanager \"build-tools;34.0.0\"'"
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
hdr "Gradle (for generating the wrapper)"
|
||||
GRADLE_BIN=""
|
||||
if command -v gradle >/dev/null 2>&1; then
|
||||
GRADLE_BIN=$(command -v gradle); ok "gradle on PATH: $GRADLE_BIN ($(gradle --version 2>/dev/null | awk '/^Gradle/{print $2}'))"
|
||||
else
|
||||
warn "gradle not on PATH — searching disk for a cached distribution..."
|
||||
for g in /opt/gradle-*/bin/gradle "$HOME"/.gradle/wrapper/dists/gradle-*/*/gradle-*/bin/gradle; do
|
||||
[ -x "$g" ] && { echo " found: $g"; GRADLE_BIN="$g"; }
|
||||
done
|
||||
if [ -n "$GRADLE_BIN" ]; then
|
||||
ok "Use one of the above to bootstrap the wrapper, e.g.: \"$GRADLE_BIN\" wrapper --gradle-version <ver>"
|
||||
else
|
||||
bad "No gradle binary found. Once a skeleton with a committed wrapper exists, ./gradlew self-bootstraps."
|
||||
echo " If you have NO wrapper and NO gradle, ask the user to install one or copy a gradle-wrapper.jar."
|
||||
fi
|
||||
fi
|
||||
|
||||
hdr "Result"
|
||||
if [ "$FAIL" -eq 0 ]; then ok "Environment looks usable for an Android build."; else bad "Blockers above must be resolved (or flagged to the user) before scaffolding."; fi
|
||||
exit $FAIL
|
||||
229
skills/scaffold-new-android-project/scripts/scaffold.sh
Executable file
229
skills/scaffold-new-android-project/scripts/scaffold.sh
Executable file
|
|
@ -0,0 +1,229 @@
|
|||
#!/usr/bin/env bash
|
||||
# scaffold.sh — generate a MINIMAL Android + Compose skeleton that "./gradlew build" passes.
|
||||
# Kotlin DSL + version catalog. No Hilt/Room/networking — add those per project after this builds.
|
||||
#
|
||||
# Usage:
|
||||
# scaffold.sh --dir <path> --package <com.foo.bar> [--name "App"] \
|
||||
# [--compile-sdk 34] [--min-sdk 24] [--target-sdk 34] \
|
||||
# [--gradle-version 9.4.1] [--agp 8.5.2] [--kotlin 2.0.20]
|
||||
set -euo pipefail
|
||||
|
||||
DIR="" ; PKG="" ; NAME="My App"
|
||||
COMPILE_SDK=34 ; MIN_SDK=24 ; TARGET_SDK=""
|
||||
GRADLE_VER="" ; AGP="" ; KOTLIN=""
|
||||
|
||||
while [ $# -gt 0 ]; do case "$1" in
|
||||
--dir) DIR="$2"; shift 2;;
|
||||
--package) PKG="$2"; shift 2;;
|
||||
--name) NAME="$2"; shift 2;;
|
||||
--compile-sdk) COMPILE_SDK="$2"; shift 2;;
|
||||
--min-sdk) MIN_SDK="$2"; shift 2;;
|
||||
--target-sdk) TARGET_SDK="$2"; shift 2;;
|
||||
--gradle-version) GRADLE_VER="$2"; shift 2;;
|
||||
--agp) AGP="$2"; shift 2;;
|
||||
--kotlin) KOTLIN="$2"; shift 2;;
|
||||
*) echo "unknown arg: $1" >&2; exit 2;;
|
||||
esac; done
|
||||
|
||||
[ -n "$DIR" ] || { echo "--dir required" >&2; exit 2; }
|
||||
[ -n "$PKG" ] || { echo "--package required" >&2; exit 2; }
|
||||
[ -n "$TARGET_SDK" ] || TARGET_SDK="$COMPILE_SDK"
|
||||
|
||||
# --- version defaults: KEEP THIS TABLE CURRENT. AGP<->Gradle<->Kotlin must be compatible. ---
|
||||
# The caller (the model running the skill) should override these after checking release notes.
|
||||
[ -n "$GRADLE_VER" ] || GRADLE_VER="8.9"
|
||||
[ -n "$AGP" ] || AGP="8.5.2"
|
||||
[ -n "$KOTLIN" ] || KOTLIN="2.0.20"
|
||||
|
||||
echo ">> Scaffolding $PKG at $DIR (compileSdk=$COMPILE_SDK, AGP=$AGP, Gradle=$GRADLE_VER, Kotlin=$KOTLIN)"
|
||||
|
||||
PKG_PATH=$(echo "$PKG" | tr '.' '/')
|
||||
APP_SRC="$DIR/app/src/main/java/$PKG_PATH"
|
||||
mkdir -p "$APP_SRC" "$DIR/app/src/main/res/values" "$DIR/gradle/wrapper"
|
||||
|
||||
# --- root settings ---------------------------------------------------------
|
||||
cat > "$DIR/settings.gradle.kts" <<EOF
|
||||
pluginManagement {
|
||||
repositories { google(); mavenCentral(); gradlePluginPortal() }
|
||||
}
|
||||
dependencyResolutionManagement {
|
||||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
||||
repositories { google(); mavenCentral() }
|
||||
}
|
||||
rootProject.name = "${NAME}"
|
||||
include(":app")
|
||||
EOF
|
||||
|
||||
# --- version catalog -------------------------------------------------------
|
||||
cat > "$DIR/gradle/libs.versions.toml" <<EOF
|
||||
[versions]
|
||||
agp = "${AGP}"
|
||||
kotlin = "${KOTLIN}"
|
||||
coreKtx = "1.13.1"
|
||||
activityCompose = "1.9.1"
|
||||
composeBom = "2024.06.00"
|
||||
junit = "4.13.2"
|
||||
|
||||
[libraries]
|
||||
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
||||
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
|
||||
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
|
||||
androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" }
|
||||
androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
|
||||
androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" }
|
||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||
|
||||
[plugins]
|
||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
|
||||
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
|
||||
EOF
|
||||
|
||||
# --- root build ------------------------------------------------------------
|
||||
cat > "$DIR/build.gradle.kts" <<EOF
|
||||
plugins {
|
||||
alias(libs.plugins.android.application) apply false
|
||||
alias(libs.plugins.kotlin.android) apply false
|
||||
alias(libs.plugins.kotlin.compose) apply false
|
||||
}
|
||||
EOF
|
||||
|
||||
# --- app build -------------------------------------------------------------
|
||||
cat > "$DIR/app/build.gradle.kts" <<EOF
|
||||
plugins {
|
||||
alias(libs.plugins.android.application)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
alias(libs.plugins.kotlin.compose)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "${PKG}"
|
||||
compileSdk = ${COMPILE_SDK}
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "${PKG}"
|
||||
minSdk = ${MIN_SDK}
|
||||
targetSdk = ${TARGET_SDK}
|
||||
versionCode = 1
|
||||
versionName = "1.0"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
kotlinOptions { jvmTarget = "17" }
|
||||
buildFeatures { compose = true }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.androidx.core.ktx)
|
||||
implementation(libs.androidx.activity.compose)
|
||||
implementation(platform(libs.androidx.compose.bom))
|
||||
implementation(libs.androidx.compose.ui)
|
||||
implementation(libs.androidx.compose.ui.tooling.preview)
|
||||
implementation(libs.androidx.compose.material3)
|
||||
testImplementation(libs.junit)
|
||||
}
|
||||
EOF
|
||||
|
||||
# --- gradle.properties -----------------------------------------------------
|
||||
cat > "$DIR/gradle.properties" <<EOF
|
||||
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
|
||||
org.gradle.caching=true
|
||||
android.useAndroidX=true
|
||||
kotlin.code.style=official
|
||||
android.nonTransitiveRClass=true
|
||||
EOF
|
||||
|
||||
# --- manifest + source + resources ----------------------------------------
|
||||
cat > "$DIR/app/src/main/AndroidManifest.xml" <<EOF
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:label="${NAME}"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.App">
|
||||
<activity android:name=".MainActivity" android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
EOF
|
||||
|
||||
cat > "$DIR/app/src/main/res/values/themes.xml" <<EOF
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="Theme.App" parent="android:Theme.Material.Light.NoActionBar" />
|
||||
</resources>
|
||||
EOF
|
||||
|
||||
cat > "$APP_SRC/MainActivity.kt" <<EOF
|
||||
package ${PKG}
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContent { MaterialTheme { Surface { Greeting() } } }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Greeting() {
|
||||
Text(text = "Hello from ${NAME}")
|
||||
}
|
||||
EOF
|
||||
|
||||
# --- git hygiene + local.properties ---------------------------------------
|
||||
cat > "$DIR/.gitignore" <<EOF
|
||||
local.properties
|
||||
**/build/
|
||||
build/
|
||||
.gradle/
|
||||
.kotlin/
|
||||
*.iml
|
||||
.idea/
|
||||
EOF
|
||||
|
||||
# Point Gradle at the SDK (never commit this — it's gitignored).
|
||||
SDK="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"
|
||||
[ -n "$SDK" ] && printf 'sdk.dir=%s\n' "$SDK" > "$DIR/local.properties"
|
||||
|
||||
# --- bootstrap the wrapper -------------------------------------------------
|
||||
GRADLE_BIN="$(command -v gradle || true)"
|
||||
if [ -z "$GRADLE_BIN" ]; then
|
||||
for g in /opt/gradle-*/bin/gradle "$HOME"/.gradle/wrapper/dists/gradle-*/*/gradle-*/bin/gradle; do
|
||||
[ -x "$g" ] && GRADLE_BIN="$g" && break
|
||||
done
|
||||
fi
|
||||
if [ -z "$GRADLE_BIN" ]; then
|
||||
echo "!! No gradle binary to generate the wrapper. Run doctor.sh and flag to the user." >&2
|
||||
exit 3
|
||||
fi
|
||||
|
||||
echo ">> Generating wrapper with $GRADLE_BIN"
|
||||
( cd "$DIR" && "$GRADLE_BIN" wrapper --gradle-version "$GRADLE_VER" >/dev/null )
|
||||
|
||||
echo ">> Building"
|
||||
( cd "$DIR" && ./gradlew build )
|
||||
|
||||
echo ">> DONE. Skeleton at $DIR builds successfully."
|
||||
Loading…
Add table
Add a link
Reference in a new issue