230 lines
7.4 KiB
Bash
Executable file
230 lines
7.4 KiB
Bash
Executable file
#!/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.5] [--agp 9.0.0] [--kotlin 2.1.0]
|
|
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. ---
|
|
# AGP 9 has Kotlin built in — no kotlin-android plugin needed.
|
|
[ -n "$GRADLE_VER" ] || GRADLE_VER="9.4.1"
|
|
[ -n "$AGP" ] || AGP="9.2.1"
|
|
[ -n "$KOTLIN" ] || KOTLIN="2.2.10"
|
|
|
|
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-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.compose) apply false
|
|
}
|
|
EOF
|
|
|
|
# --- app build -------------------------------------------------------------
|
|
cat > "$DIR/app/build.gradle.kts" <<EOF
|
|
plugins {
|
|
alias(libs.plugins.android.application)
|
|
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
|
|
}
|
|
kotlin {
|
|
compilerOptions {
|
|
jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_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."
|