|
| 1 | +#!/bin/bash |
| 2 | +# disable-android-for-flatpak.sh |
| 3 | +# |
| 4 | +# Strips all Android-related configuration from the project so it can |
| 5 | +# build inside the Flatpak sandbox where no Android SDK is available. |
| 6 | +# This script modifies files IN-PLACE — only run during Flatpak builds. |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +echo "=== Disabling Android targets for Flatpak build ===" |
| 11 | + |
| 12 | +# ───────────────────────────────────────────────────────────────────── |
| 13 | +# 1. Root build.gradle.kts — comment out Android plugin declarations |
| 14 | +# ───────────────────────────────────────────────────────────────────── |
| 15 | +echo "[1/6] Patching root build.gradle.kts" |
| 16 | +sed -i \ |
| 17 | + -e 's|alias(libs.plugins.android.application)|// alias(libs.plugins.android.application)|' \ |
| 18 | + -e 's|alias(libs.plugins.android.library)|// alias(libs.plugins.android.library)|' \ |
| 19 | + -e 's|alias(libs.plugins.android.kotlin.multiplatform.library)|// alias(libs.plugins.android.kotlin.multiplatform.library)|' \ |
| 20 | + build.gradle.kts |
| 21 | + |
| 22 | +# ───────────────────────────────────────────────────────────────────── |
| 23 | +# 2. Convention plugins — replace Android plugin applies with no-ops |
| 24 | +# ───────────────────────────────────────────────────────────────────── |
| 25 | +CONVENTION_DIR="build-logic/convention/src/main/kotlin" |
| 26 | + |
| 27 | +echo "[2/6] Patching KmpLibraryConventionPlugin (remove Android library plugin + config)" |
| 28 | +cat > "$CONVENTION_DIR/KmpLibraryConventionPlugin.kt" << 'KOTLIN' |
| 29 | +import org.gradle.api.Plugin |
| 30 | +import org.gradle.api.Project |
| 31 | +import org.gradle.kotlin.dsl.dependencies |
| 32 | +import zed.rainxch.githubstore.convention.configureJvmTarget |
| 33 | +import zed.rainxch.githubstore.convention.libs |
| 34 | +import zed.rainxch.githubstore.convention.pathToResourcePrefix |
| 35 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 36 | +import org.gradle.kotlin.dsl.configure |
| 37 | +
|
| 38 | +class KmpLibraryConventionPlugin : Plugin<Project> { |
| 39 | + override fun apply(target: Project) { |
| 40 | + with(target) { |
| 41 | + with(pluginManager) { |
| 42 | + apply("org.jetbrains.kotlin.multiplatform") |
| 43 | + apply("org.jetbrains.kotlin.plugin.serialization") |
| 44 | + } |
| 45 | +
|
| 46 | + configureJvmTarget() |
| 47 | +
|
| 48 | + extensions.configure<KotlinMultiplatformExtension> { |
| 49 | + compilerOptions { |
| 50 | + freeCompilerArgs.add("-Xexpect-actual-classes") |
| 51 | + freeCompilerArgs.add("-Xmulti-dollar-interpolation") |
| 52 | + freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn") |
| 53 | + freeCompilerArgs.add("-opt-in=kotlin.time.ExperimentalTime") |
| 54 | + } |
| 55 | + } |
| 56 | +
|
| 57 | + dependencies { |
| 58 | + "commonMainImplementation"(libs.findLibrary("kotlinx-serialization-json").get()) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +KOTLIN |
| 64 | + |
| 65 | +echo "[3/6] Patching CmpApplicationConventionPlugin (remove Android application)" |
| 66 | +cat > "$CONVENTION_DIR/CmpApplicationConventionPlugin.kt" << 'KOTLIN' |
| 67 | +import org.gradle.api.Plugin |
| 68 | +import org.gradle.api.Project |
| 69 | +import org.gradle.kotlin.dsl.dependencies |
| 70 | +import zed.rainxch.githubstore.convention.configureJvmTarget |
| 71 | +import zed.rainxch.githubstore.convention.libs |
| 72 | +
|
| 73 | +class CmpApplicationConventionPlugin : Plugin<Project> { |
| 74 | + override fun apply(target: Project) { |
| 75 | + with(target) { |
| 76 | + with(pluginManager) { |
| 77 | + apply("org.jetbrains.kotlin.multiplatform") |
| 78 | + apply("org.jetbrains.compose") |
| 79 | + apply("org.jetbrains.kotlin.plugin.compose") |
| 80 | + } |
| 81 | +
|
| 82 | + configureJvmTarget() |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +KOTLIN |
| 87 | + |
| 88 | +echo "[4/6] Patching CmpLibraryConventionPlugin & CmpFeatureConventionPlugin" |
| 89 | + |
| 90 | +# CmpLibraryConventionPlugin — remove Android library dependency |
| 91 | +sed -i \ |
| 92 | + -e 's|apply("com.android.library")|// apply("com.android.library")|' \ |
| 93 | + "$CONVENTION_DIR/CmpLibraryConventionPlugin.kt" 2>/dev/null || true |
| 94 | + |
| 95 | +sed -i \ |
| 96 | + -e 's|apply("com.android.library")|// apply("com.android.library")|' \ |
| 97 | + "$CONVENTION_DIR/CmpFeatureConventionPlugin.kt" 2>/dev/null || true |
| 98 | + |
| 99 | +# Remove configureKotlinAndroid calls and Android extension blocks |
| 100 | +for f in "$CONVENTION_DIR"/*.kt "$CONVENTION_DIR"/zed/rainxch/githubstore/convention/*.kt; do |
| 101 | + [ -f "$f" ] || continue |
| 102 | + sed -i \ |
| 103 | + -e 's|configureAndroidTarget()|// configureAndroidTarget()|g' \ |
| 104 | + -e 's|configureKotlinAndroid(this)|// configureKotlinAndroid(this)|g' \ |
| 105 | + "$f" |
| 106 | +done |
| 107 | + |
| 108 | +# ───────────────────────────────────────────────────────────────────── |
| 109 | +# 3. KotlinMultiplatform.kt — skip Android configuration |
| 110 | +# ───────────────────────────────────────────────────────────────────── |
| 111 | +echo "[5/6] Patching KotlinMultiplatform.kt" |
| 112 | +cat > "$CONVENTION_DIR/zed/rainxch/githubstore/convention/KotlinMultiplatform.kt" << 'KOTLIN' |
| 113 | +package zed.rainxch.githubstore.convention |
| 114 | +
|
| 115 | +import org.gradle.api.Project |
| 116 | +import org.gradle.kotlin.dsl.configure |
| 117 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 118 | +
|
| 119 | +internal fun Project.configureKotlinMultiplatform() { |
| 120 | + // Android target disabled for Flatpak build |
| 121 | + configureJvmTarget() |
| 122 | +
|
| 123 | + extensions.configure<KotlinMultiplatformExtension> { |
| 124 | + compilerOptions { |
| 125 | + freeCompilerArgs.add("-Xexpect-actual-classes") |
| 126 | + freeCompilerArgs.add("-Xmulti-dollar-interpolation") |
| 127 | + freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn") |
| 128 | + freeCompilerArgs.add("-opt-in=kotlin.time.ExperimentalTime") |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +KOTLIN |
| 133 | + |
| 134 | +# ───────────────────────────────────────────────────────────────────── |
| 135 | +# 4. Module build.gradle.kts files — remove android {} blocks |
| 136 | +# ───────────────────────────────────────────────────────────────────── |
| 137 | +echo "[6/6] Removing android {} blocks from module build.gradle.kts files" |
| 138 | + |
| 139 | +# composeApp — remove android {} block and its contents |
| 140 | +python3 -c " |
| 141 | +import re, sys |
| 142 | +
|
| 143 | +with open('composeApp/build.gradle.kts', 'r') as f: |
| 144 | + content = f.read() |
| 145 | +
|
| 146 | +# Remove top-level android { ... } blocks (handles nested braces) |
| 147 | +def remove_block(text, keyword): |
| 148 | + result = [] |
| 149 | + i = 0 |
| 150 | + while i < len(text): |
| 151 | + # Look for 'android {' at line start (possibly with whitespace) |
| 152 | + line_start = text.rfind('\n', 0, i) + 1 |
| 153 | + prefix = text[line_start:i].strip() |
| 154 | + if text[i:].startswith(keyword + ' {') or text[i:].startswith(keyword + '{'): |
| 155 | + if prefix == '' or prefix.endswith('\n'): |
| 156 | + # Find matching closing brace |
| 157 | + brace_start = text.index('{', i) |
| 158 | + depth = 1 |
| 159 | + j = brace_start + 1 |
| 160 | + while j < len(text) and depth > 0: |
| 161 | + if text[j] == '{': depth += 1 |
| 162 | + elif text[j] == '}': depth -= 1 |
| 163 | + j += 1 |
| 164 | + # Skip past the block and any trailing newline |
| 165 | + if j < len(text) and text[j] == '\n': |
| 166 | + j += 1 |
| 167 | + i = j |
| 168 | + continue |
| 169 | + result.append(text[i]) |
| 170 | + i += 1 |
| 171 | + return ''.join(result) |
| 172 | +
|
| 173 | +content = remove_block(content, 'android') |
| 174 | +with open('composeApp/build.gradle.kts', 'w') as f: |
| 175 | + f.write(content) |
| 176 | +" |
| 177 | + |
| 178 | +# core/data — remove android {} block |
| 179 | +for gradle_file in \ |
| 180 | + core/data/build.gradle.kts \ |
| 181 | + core/domain/build.gradle.kts \ |
| 182 | + core/presentation/build.gradle.kts; do |
| 183 | + if [ -f "$gradle_file" ]; then |
| 184 | + python3 -c " |
| 185 | +import sys |
| 186 | +with open('$gradle_file', 'r') as f: |
| 187 | + lines = f.readlines() |
| 188 | +result = [] |
| 189 | +skip_depth = 0 |
| 190 | +i = 0 |
| 191 | +while i < len(lines): |
| 192 | + stripped = lines[i].strip() |
| 193 | + if stripped.startswith('android {') or stripped == 'android{': |
| 194 | + skip_depth = 1 |
| 195 | + i += 1 |
| 196 | + while i < len(lines) and skip_depth > 0: |
| 197 | + for ch in lines[i]: |
| 198 | + if ch == '{': skip_depth += 1 |
| 199 | + elif ch == '}': skip_depth -= 1 |
| 200 | + i += 1 |
| 201 | + continue |
| 202 | + result.append(lines[i]) |
| 203 | + i += 1 |
| 204 | +with open('$gradle_file', 'w') as f: |
| 205 | + f.writelines(result) |
| 206 | +" |
| 207 | + fi |
| 208 | +done |
| 209 | + |
| 210 | +# Remove AndroidApplicationComposeConventionPlugin registration attempt |
| 211 | +# (it won't compile without AGP) |
| 212 | +cat > "$CONVENTION_DIR/AndroidApplicationComposeConventionPlugin.kt" << 'KOTLIN' |
| 213 | +import org.gradle.api.Plugin |
| 214 | +import org.gradle.api.Project |
| 215 | +
|
| 216 | +class AndroidApplicationComposeConventionPlugin : Plugin<Project> { |
| 217 | + override fun apply(target: Project) { |
| 218 | + // No-op: Android disabled for Flatpak build |
| 219 | + } |
| 220 | +} |
| 221 | +KOTLIN |
| 222 | + |
| 223 | +cat > "$CONVENTION_DIR/AndroidApplicationConventionPlugin.kt" << 'KOTLIN' |
| 224 | +import org.gradle.api.Plugin |
| 225 | +import org.gradle.api.Project |
| 226 | +
|
| 227 | +class AndroidApplicationConventionPlugin : Plugin<Project> { |
| 228 | + override fun apply(target: Project) { |
| 229 | + // No-op: Android disabled for Flatpak build |
| 230 | + } |
| 231 | +} |
| 232 | +KOTLIN |
| 233 | + |
| 234 | +echo "=== Android targets disabled successfully ===" |
0 commit comments