fun main()

in repository-tools/src/main/kotlin/installAndroidSdk.kt [11:81]


fun main() {
    val sdkZipFile = Path(".local/android-sdk.zip").createParentDirectories()
    val sdkDir = Path(".local/android-sdk").createParentDirectories()
    val sdkManager = sdkDir.resolve("cmdline-tools/bin").resolve(
        when (Os.current()) {
            Os.Windows -> "sdkmanager.bat"
            Os.MacOs -> "sdkmanager"
            Os.Linux -> "sdkmanager"
        }
    )

    if (sdkManager.exists()) return

    val url = when (Os.current()) {
        Os.Windows ->
            "https://dl.google.com/android/repository/commandlinetools-win-13114758_latest.zip?hl=de"
        Os.MacOs ->
            "https://dl.google.com/android/repository/commandlinetools-mac-13114758_latest.zip?hl=de"
        Os.Linux ->
            "https://dl.google.com/android/repository/commandlinetools-linux-13114758_latest.zip?hl=de"
    }

    URI(url).toURL().openStream().use { input ->
        input.copyTo(sdkZipFile.outputStream())
    }

    ZipFile(sdkZipFile.toFile()).use {
        it.entries().asSequence().forEach { entry ->
            val target = sdkDir.resolve(entry.name)
            if (entry.isDirectory) {
                target.createDirectories()
                return@forEach
            }


            it.getInputStream(entry).use { input ->
                target
                    .createParentDirectories()
                    .outputStream().use { output -> input.copyTo(output) }
            }
        }
    }

    sdkManager.toFile().setExecutable(true)

    val command = if (System.getProperty("os.name").startsWith("Win")) {
        listOf("cmd", "/c", sdkManager.absolutePathString())
    } else listOf(sdkManager.absolutePathString())

    val process = ProcessBuilder(
        *command.toTypedArray(),
        "--sdk_root=${sdkDir.absolutePathString()}",
        "--install",
        "platforms;android-34",
        "platforms;android-35",
        "build-tools;34.0.0",
        "build-tools;35.0.0",
        "platform-tools"
    )
        .redirectError(ProcessBuilder.Redirect.INHERIT)
        .redirectOutput(ProcessBuilder.Redirect.INHERIT)
        .start()

    runCatching {
        process.outputStream.writer().use { writer ->
            while (true) {
                writer.appendLine("y")
            }
        }
    }
}