fix: bootstrap packages are not properly included in the APK

This commit is contained in:
Akash Yadav
2024-01-02 11:45:05 +05:30
parent 3d25438765
commit 748453d726
6 changed files with 54 additions and 113 deletions
@@ -17,12 +17,10 @@
package com.itsaky.androidide.plugins
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.itsaky.androidide.plugins.tasks.TerminalBootstrapDownloadTask
import com.itsaky.androidide.plugins.util.DownloadUtils
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.compile.JavaCompile
import java.io.File
/**
* Gradle plugin which downloads the bootstrap packages for the terminal.
@@ -46,33 +44,50 @@ class TerminalBootstrapPackagesPlugin : Plugin<Project> {
* The bootstrap packages version, basically the tag name of the GitHub release.
*/
private const val BOOTSTRAP_PACKAGES_VERSION = "16.12.2023"
private const val PACKAGES_DOWNLOAD_URL = "https://github.com/AndroidIDEOfficial/terminal-packages/releases/download/bootstrap-%1\$s/bootstrap-%2\$s.zip"
}
override fun apply(target: Project) {
target.run {
val downloadTask = tasks.register("downloadBootstrapPackages",
TerminalBootstrapDownloadTask::class.java) {
packages.set(BOOTSTRAP_PACKAGES)
version.set(BOOTSTRAP_PACKAGES_VERSION)
}
val cleanTask = tasks.register("deleteBootstrapPackages", Delete::class.java) {
doLast {
fileTree("src/main/cpp").apply {
include("bootstrap-*.zip")
}.forEach { file ->
file.delete()
}
}
}
val bootstrapOut = project.layout.buildDirectory.file("intermediates/bootstrap-packages")
.get().asFile
tasks.withType(JavaCompile::class.java).configureEach {
dependsOn(downloadTask)
}
val files = BOOTSTRAP_PACKAGES.map { (arch, sha256) ->
val file = File(bootstrapOut, "bootstrap-${arch}.zip")
file.parentFile.mkdirs()
tasks.named("clean").configure {
dependsOn(cleanTask)
}
DownloadUtils.doDownload(file = file,
remoteUrl = PACKAGES_DOWNLOAD_URL.format(BOOTSTRAP_PACKAGES_VERSION, arch),
expectedChecksum = sha256,
logger = logger
)
return@map arch to file
}.toMap()
project.file("src/main/cpp/termux-bootstrap-zip.S").writeText(
"""
.global blob
.global blob_size
.section .rodata
blob:
#if defined __aarch64__
.incbin "${files["aarch64"]!!.absolutePath}"
#elif defined __arm__
.incbin "${files["arm"]!!.absolutePath}"
#elif defined __x86_64__
.incbin "${files["x86_64"]!!.absolutePath}"
#else
# error Unsupported arch
#endif
1:
blob_size:
.int 1b - blob
""".trimIndent()
)
}
}
}
@@ -17,7 +17,8 @@
package com.itsaky.androidide.plugins.tasks
import com.itsaky.androidide.plugins.AbstractDownloadTask
import com.itsaky.androidide.plugins.util.DownloadUtils
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
@@ -27,7 +28,7 @@ import org.gradle.api.tasks.TaskAction
/**
* @author Akash Yadav
*/
abstract class DownloadAapt2Task : AbstractDownloadTask() {
abstract class DownloadAapt2Task : DefaultTask() {
/**
* The CPU architecture for which the `aapt2` should be downloaded.
@@ -59,6 +60,7 @@ abstract class DownloadAapt2Task : AbstractDownloadTask() {
}
companion object {
private const val DEFAULT_VERSION = "34.0.4"
private val AAPT2_DOWNLOAD_URL = "https://github.com/AndroidIDEOfficial/platform-tools/releases/download/v%1\$s/aapt2-%2\$s"
}
@@ -74,6 +76,6 @@ abstract class DownloadAapt2Task : AbstractDownloadTask() {
val remoteUrl = AAPT2_DOWNLOAD_URL.format(version, arch)
doDownload(file, remoteUrl, expectedChecksum)
DownloadUtils.doDownload(file, remoteUrl, expectedChecksum, logger)
}
}
@@ -1,65 +0,0 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.plugins.tasks
import com.itsaky.androidide.plugins.AbstractDownloadTask
import org.gradle.api.GradleException
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
/**
* @author Akash Yadav
*/
abstract class TerminalBootstrapDownloadTask : AbstractDownloadTask() {
companion object {
private const val PACKAGES_DOWNLOAD_URL = "https://github.com/AndroidIDEOfficial/terminal-packages/releases/download/bootstrap-%1\$s/bootstrap-%2\$s.zip"
}
/**
* The bootstrap packages to download. The keys in this map are supported the CPU ABIs and
* the values are the `sha256sum` of the bootstrap packages.
*/
@get:Input
abstract val packages: MapProperty<String, String>
/**
* Version of the bootstrap packages. This is basically the tag name of the GitHub release.
*/
@get:Input
abstract val version: Property<String>
@TaskAction
fun downloadBootstrap() {
val packages = this.packages.get()
val version = this.version.get()
if (packages.isEmpty() || version.isNullOrBlank()) {
throw GradleException("Packages map is empty!")
}
packages.forEach { (arch, sha256) ->
val localPath = "src/main/cpp/bootstrap-${arch}.zip"
val file = project.file(localPath)
doDownload(file, PACKAGES_DOWNLOAD_URL.format(version, arch), sha256)
}
}
}
@@ -15,10 +15,10 @@
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.plugins
package com.itsaky.androidide.plugins.util
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.logging.Logger
import java.io.File
import java.math.BigInteger
import java.net.HttpURLConnection
@@ -29,13 +29,14 @@ import java.security.MessageDigest
/**
* @author Akash Yadav
*/
abstract class AbstractDownloadTask : DefaultTask() {
object DownloadUtils {
/**
* Download the file at given [URL][remoteUrl] to the given [local file][file] and verify the
* SHA-256 checksum of the downloaded file with the [expected checksum][expectedChecksum].
*/
protected fun doDownload(file: File, remoteUrl: String, expectedChecksum: String) {
fun doDownload(file: File, remoteUrl: String, expectedChecksum: String,
logger: Logger) {
logger.info("Download $remoteUrl to $file having checksum ${expectedChecksum}...")
+5 -1
View File
@@ -1 +1,5 @@
bootstrap-*
# Downloaded by TerminalBootstrapDownloadTask
bootstrap-*
# Auto generated by TerminalBootstrapDownloadTask
termux-bootstrap-zip.S
@@ -1,16 +0,0 @@
.global blob
.global blob_size
.section .rodata
blob:
#if defined __aarch64__
.incbin "bootstrap-aarch64.zip"
#elif defined __arm__
.incbin "bootstrap-arm.zip"
#elif defined __x86_64__
.incbin "bootstrap-x86_64.zip"
#else
# error Unsupported arch
#endif
1:
blob_size:
.int 1b - blob