|
| 1 | +import org.jetbrains.kotlin.gradle.dsl.JvmTarget |
| 2 | + |
| 3 | +import java.net.URL |
| 4 | +import java.security.MessageDigest |
| 5 | +import java.net.HttpURLConnection |
| 6 | + |
| 7 | + |
| 8 | +plugins { |
| 9 | + id("com.android.application") |
| 10 | + id("org.jetbrains.kotlin.android") |
| 11 | + id("com.itsaky.androidide.plugins.build") |
| 12 | +} |
| 13 | + |
| 14 | +pluginBuilder { |
| 15 | + pluginName = "ndk-installer" |
| 16 | +} |
| 17 | + |
| 18 | +android { |
| 19 | + namespace = "org.appdevforall.ndkinstaller" |
| 20 | + compileSdk = 34 |
| 21 | + |
| 22 | + defaultConfig { |
| 23 | + applicationId = "org.appdevforall.ndkinstaller" |
| 24 | + minSdk = 26 |
| 25 | + targetSdk = 34 |
| 26 | + versionCode = 1 |
| 27 | + versionName = "1.0.0" |
| 28 | + } |
| 29 | + |
| 30 | + buildTypes { |
| 31 | + release { |
| 32 | + isMinifyEnabled = false |
| 33 | + proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + androidResources { |
| 38 | + noCompress += listOf("xz") |
| 39 | + } |
| 40 | + |
| 41 | + compileOptions { |
| 42 | + sourceCompatibility = JavaVersion.VERSION_17 |
| 43 | + targetCompatibility = JavaVersion.VERSION_17 |
| 44 | + } |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +kotlin { |
| 49 | + compilerOptions { |
| 50 | + jvmTarget.set(JvmTarget.JVM_17) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +dependencies { |
| 55 | + compileOnly(files("../libs/plugin-api.jar")) |
| 56 | + |
| 57 | + |
| 58 | + implementation("org.jetbrains.kotlin:kotlin-stdlib:2.1.21") |
| 59 | + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0") |
| 60 | +} |
| 61 | + |
| 62 | +fun httpDownload(url: String): ByteArray { |
| 63 | + val connection = URL(url).openConnection() as HttpURLConnection |
| 64 | + connection.instanceFollowRedirects = true |
| 65 | + connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)") |
| 66 | + connection.connectTimeout = 15000 |
| 67 | + connection.readTimeout = 30000 |
| 68 | + |
| 69 | + val code = connection.responseCode |
| 70 | + if (code != 200) { |
| 71 | + throw GradleException("Failed to download $url (HTTP $code)") |
| 72 | + } |
| 73 | + |
| 74 | + return connection.inputStream.use { it.readBytes() } |
| 75 | +} |
| 76 | + |
| 77 | +fun md5Of(file: File): String { |
| 78 | + val md = MessageDigest.getInstance("MD5") |
| 79 | + file.inputStream().use { input -> |
| 80 | + val buffer = ByteArray(8192) |
| 81 | + var read: Int |
| 82 | + while (input.read(buffer).also { read = it } != -1) { |
| 83 | + md.update(buffer, 0, read) |
| 84 | + } |
| 85 | + } |
| 86 | + return md.digest().joinToString("") { "%02x".format(it) } |
| 87 | +} |
| 88 | + |
| 89 | +val downloadAssets by tasks.registering { |
| 90 | + val assetsDir = project.file("src/main/assets") |
| 91 | + val archiveFile = assetsDir.resolve("ndk-cmake.tar.xz") |
| 92 | + val md5File = assetsDir.resolve("ndk-cmake.tar.xz.md5") |
| 93 | + |
| 94 | + outputs.files(archiveFile) |
| 95 | + |
| 96 | + doLast { |
| 97 | + assetsDir.mkdirs() |
| 98 | + |
| 99 | + val archiveUrl = "https://www.appdevforall.org/dev-assets/release/v8/ndk-cmake.tar.xz" |
| 100 | + val md5Url = "https://www.appdevforall.org/dev-assets/release/v8/ndk-cmake.tar.xz.md5" |
| 101 | + |
| 102 | + logger.info("Downloading archive....") |
| 103 | + val archiveBytes = httpDownload(archiveUrl) |
| 104 | + archiveFile.writeBytes(archiveBytes) |
| 105 | + |
| 106 | + logger.info("Downloading MD5…") |
| 107 | + val md5Bytes = httpDownload(md5Url) |
| 108 | + md5File.writeBytes(md5Bytes) |
| 109 | + |
| 110 | + val expected = md5File.readText().trim() |
| 111 | + val actual = md5Of(archiveFile) |
| 112 | + |
| 113 | + logger.info("Expected MD5: $expected") |
| 114 | + logger.info("Actual MD5: $actual") |
| 115 | + |
| 116 | + md5File.delete() |
| 117 | + |
| 118 | + if (!expected.equals(actual, ignoreCase = true)) { |
| 119 | + throw GradleException("MD5 checksum mismatch for ndk-cmake.tar.xz") |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
0 commit comments