Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 78 additions & 65 deletions app/src/main/java/com/itsaky/androidide/assets/BaseAssetsInstaller.kt
Original file line number Diff line number Diff line change
@@ -1,89 +1,102 @@
package com.itsaky.androidide.assets

import android.content.Context
import com.itsaky.androidide.utils.Environment
import com.itsaky.androidide.utils.FeatureFlags
import com.termux.shared.termux.TermuxConstants
import org.slf4j.LoggerFactory
import java.io.File
import java.nio.file.Path
import java.util.concurrent.TimeUnit
import com.termux.shared.termux.TermuxConstants
import com.itsaky.androidide.utils.Environment
import com.itsaky.androidide.utils.FeatureFlags
import kotlin.concurrent.thread
import kotlin.system.measureTimeMillis


abstract class BaseAssetsInstaller : AssetsInstaller {
private val logger = LoggerFactory.getLogger(BaseAssetsInstaller::class.java)
private val logger = LoggerFactory.getLogger(BaseAssetsInstaller::class.java)

override suspend fun postInstall(
context: Context,
stagingDir: Path
) {
Environment.AAPT2.setExecutable(true)
override suspend fun postInstall(
context: Context,
stagingDir: Path,
) {
for (bin in arrayOf(
"aapt",
"aapt2",
"aidl",
"dexdump",
"split-select",
"zipalign",
)) {
Environment.setExecutable(Environment.BUILD_TOOLS_DIR.resolve(bin))
}

installNdk(
File(Environment.ANDROID_HOME, Environment.NDK_TAR_XZ),
Environment.ANDROID_HOME
)
}
installNdk(
File(Environment.ANDROID_HOME, Environment.NDK_TAR_XZ),
Environment.ANDROID_HOME,
)
}

private fun installNdk(archiveFile: File, outputDir: File): Boolean {
if (!FeatureFlags.isExperimentsEnabled) return false
private fun installNdk(
archiveFile: File,
outputDir: File,
): Boolean {
if (!FeatureFlags.isExperimentsEnabled) return false

if (!archiveFile.exists()) {
logger.debug("NDK installable package not found: ${archiveFile.absolutePath}")
return false
}
if (!archiveFile.exists()) {
logger.debug("NDK installable package not found: ${archiveFile.absolutePath}")
return false
}

logger.debug("Starting installation of ${archiveFile.absolutePath}")
logger.debug("Starting installation of ${archiveFile.absolutePath}")

var exitCode: Int
var result: String
val elapsed = measureTimeMillis {
val processBuilder = ProcessBuilder(
"${TermuxConstants.TERMUX_BIN_PREFIX_DIR_PATH}/bash",
"-c",
"tar -xJf ${archiveFile.absolutePath} -C ${outputDir.absolutePath} --no-same-owner"
)
.redirectErrorStream(true)
var exitCode: Int
var result: String
val elapsed =
measureTimeMillis {
val processBuilder =
ProcessBuilder(
"${TermuxConstants.TERMUX_BIN_PREFIX_DIR_PATH}/bash",
"-c",
"tar -xJf ${archiveFile.absolutePath} -C ${outputDir.absolutePath} --no-same-owner",
).redirectErrorStream(true)

val env = processBuilder.environment()
env["PATH"] = "${TermuxConstants.TERMUX_BIN_PREFIX_DIR_PATH}:${env["PATH"]}"
val env = processBuilder.environment()
env["PATH"] = "${TermuxConstants.TERMUX_BIN_PREFIX_DIR_PATH}:${env["PATH"]}"

val process = processBuilder.start()
val process = processBuilder.start()

val output = StringBuilder()
val reader = thread(start = true, name = "ndk-extract-output") {
process.inputStream.bufferedReader().useLines { lines ->
lines.forEach { output.appendLine(it) }
}
}
val output = StringBuilder()
val reader =
thread(start = true, name = "ndk-extract-output") {
process.inputStream.bufferedReader().useLines { lines ->
lines.forEach { output.appendLine(it) }
}
}

val completed = process.waitFor(2, TimeUnit.MINUTES)
if (!completed) process.destroyForcibly()
reader.join()
result = output.toString()
exitCode = if (completed) process.exitValue() else -1
}
val completed = process.waitFor(2, TimeUnit.MINUTES)
if (!completed) process.destroyForcibly()
reader.join()
result = output.toString()
exitCode = if (completed) process.exitValue() else -1
}

return if (exitCode == 0) {
logger.debug("Extraction of ${archiveFile.absolutePath} successful took ${elapsed}ms : $result")
return if (exitCode == 0) {
logger.debug("Extraction of ${archiveFile.absolutePath} successful took ${elapsed}ms : $result")

if (archiveFile.exists()) {
val deleted = archiveFile.delete()
if (deleted) {
logger.debug("${archiveFile.absolutePath} deleted successfully.")
} else {
logger.debug("Failed to delete ${archiveFile.absolutePath}.")
}
deleted
} else {
logger.debug("Archive file not found for deletion.")
false
}
} else {
logger.error("Extraction failed with code $exitCode: $result")
false
}
}
if (archiveFile.exists()) {
val deleted = archiveFile.delete()
if (deleted) {
logger.debug("${archiveFile.absolutePath} deleted successfully.")
} else {
logger.debug("Failed to delete ${archiveFile.absolutePath}.")
}
deleted
} else {
logger.debug("Archive file not found for deletion.")
false
}
} else {
logger.error("Extraction failed with code $exitCode: $result")
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public final class Environment {
private static final String ANDROIDIDE_PROJECT_CACHE_DIR = SharedEnvironment.PROJECT_CACHE_DIR_NAME;
private static final String DATABASE_NAME = "documentation.db";

public static final String BUILD_TOOLS_VERSION = "35.0.0";

public static final String PLUGIN_API_JAR_RELATIVE_PATH = "libs/plugin-api.jar";

private static final Logger LOG = LoggerFactory.getLogger(Environment.class);
Expand Down Expand Up @@ -82,6 +84,7 @@ public final class Environment {

public static File INIT_SCRIPT;
public static File GRADLE_USER_HOME;
public static File BUILD_TOOLS_DIR;
public static File AAPT2;
public static File JAVA;
public static File BASH_SHELL;
Expand Down Expand Up @@ -161,7 +164,8 @@ public static void init(Context context) {
ANDROID_HOME = new File(DEFAULT_ANDROID_HOME);
JAVA_HOME = new File(DEFAULT_JAVA_HOME);

AAPT2 = new File(ANDROID_HOME, "build-tools/35.0.0/aapt2");
BUILD_TOOLS_DIR = new File(ANDROID_HOME, "build-tools/" + BUILD_TOOLS_VERSION);
AAPT2 = new File(BUILD_TOOLS_DIR, "aapt2");

JAVA = new File(JAVA_HOME, "bin/java");
BASH_SHELL = new File(BIN_DIR, "bash");
Expand All @@ -181,7 +185,7 @@ public static void init(Context context) {
KEYSTORE_RELEASE = new File(KEYSTORE_DIR, KEYSTORE_RELEASE_NAME);
KEYSTORE_PROPERTIES = new File(KEYSTORE_DIR, KEYSTORE_PROPERTIES_NAME);

NDK_DIR = new File(ANDROID_HOME,"ndk");
NDK_DIR = new File(ANDROID_HOME, "ndk");

isInitialized.set(true);
}
Expand Down