Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix: use prebuilt aapt2 file when building for F-Droid
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Jan 2, 2024
1 parent c815bc4 commit b0f8feb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 21 deletions.
17 changes: 14 additions & 3 deletions build-logic/ide/src/main/java/FDroidConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import org.gradle.api.Project
import java.io.File

/*
* This file is part of AndroidIDE.
*
Expand All @@ -18,6 +15,9 @@ import java.io.File
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

import org.gradle.api.Project
import java.io.File

/**
* @author Akash Yadav
*/
Expand All @@ -28,16 +28,25 @@ object FDroidConfig {

var isFDroidBuild: Boolean = false
private set
get() = hasRead && field

var fDroidBuildArch: String? = null
private set

var fDroidVersionName: String? = null
private set

var fDroidVersionCode: Int? = null
private set

var fDroidAapt2File: String? = null
private set

const val PROP_FDROID_BUILD = "ide.build.fdroid"
const val PROP_FDROID_BUILD_ARCH = "ide.build.fdroid.arch"
const val PROP_FDROID_BUILD_VERSION = "ide.build.fdroid.version"
const val PROP_FDROID_BUILD_VERCODE = "ide.build.fdroid.vercode"
const val PROP_FDROID_AAPT2FILE = "ide.build.fdroid.aapt2File"

fun load(project: Project) {
val propsFile = File(project.rootDir, "fdroid.properties")
Expand All @@ -56,7 +65,9 @@ object FDroidConfig {
hasRead = true
isFDroidBuild = properties.getProperty(PROP_FDROID_BUILD, null).toBoolean()

fDroidBuildArch = properties.getProperty(PROP_FDROID_BUILD_ARCH, null)
fDroidVersionName = properties.getProperty(PROP_FDROID_BUILD_VERSION, null)
fDroidVersionCode = properties.getProperty(PROP_FDROID_BUILD_VERCODE, null)?.toInt()
fDroidAapt2File = properties.getProperty(PROP_FDROID_AAPT2FILE, null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
package com.itsaky.androidide.plugins

import BuildConfig
import FDroidConfig
import VersionUtils
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.BaseExtension
import com.itsaky.androidide.plugins.conf.flavorsAbis
import com.itsaky.androidide.plugins.tasks.AddAndroidJarToAssetsTask
import com.itsaky.androidide.plugins.tasks.AddFileToAssetsTask
import com.itsaky.androidide.plugins.tasks.DownloadAapt2Task
Expand All @@ -31,6 +33,7 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.TaskProvider
import org.gradle.configurationcache.extensions.capitalized
import java.io.File

/**
* Handles asset copying and generation.
Expand All @@ -57,15 +60,7 @@ class AndroidIDEAssetsPlugin : Plugin<Project> {
val androidComponentsExtension = extensions.getByType(AndroidComponentsExtension::class.java)
val baseExtension = extensions.getByType(BaseExtension::class.java)

val aapt2Tasks = mutableMapOf<String, TaskProvider<DownloadAapt2Task>>()
baseExtension.productFlavors.forEach { flavor ->
aapt2Tasks[flavor.name] = tasks.register("downloadAapt2${flavor.name.capitalized()}",
DownloadAapt2Task::class.java) {
arch.set(flavor.name)
checksum.set(AAPT2_CHECKSUMS[flavor.name] ?: throw IllegalStateException(
"Checksum for aapt2-${flavor.name} not found!"))
}
}
val aapt2Tasks = getAapt2DownloadTasks(baseExtension)

val addAndroidJarTaskProvider = tasks.register("addAndroidJarToAssets",
AddAndroidJarToAssetsTask::class.java) {
Expand All @@ -74,9 +69,17 @@ class AndroidIDEAssetsPlugin : Plugin<Project> {

androidComponentsExtension.onVariants { variant ->

val downloadAapt2TaskProvider = aapt2Tasks[variant.flavorName]
?: throw IllegalStateException(
"'aapt2' task not registered for flavor '${variant.flavorName}'")
if (aapt2Tasks.isNotEmpty()) {
val downloadAapt2TaskProvider = aapt2Tasks[variant.flavorName]
?: throw IllegalStateException(
"'aapt2' task not registered for flavor '${variant.flavorName}'")

variant.sources.assets?.addGeneratedSourceDirectory(downloadAapt2TaskProvider,
DownloadAapt2Task::outputDirectory)
} else {
variant.sources.assets?.addStaticSourceDirectory(
getStaticFDroidAapt2Dir(variant.flavorName!!))
}

val variantNameCapitalized = variant.name.capitalized()

Expand All @@ -86,9 +89,6 @@ class AndroidIDEAssetsPlugin : Plugin<Project> {
variant.sources.assets?.addGeneratedSourceDirectory(addAndroidJarTaskProvider,
AddAndroidJarToAssetsTask::outputDirectory)

variant.sources.assets?.addGeneratedSourceDirectory(downloadAapt2TaskProvider,
DownloadAapt2Task::outputDirectory)

// Init script generator
val generateInitScript = tasks.register(
"generate${variantNameCapitalized}InitScript",
Expand Down Expand Up @@ -126,5 +126,64 @@ class AndroidIDEAssetsPlugin : Plugin<Project> {
}
}
}

private fun Project.getStaticFDroidAapt2Dir(flavorName: String): String {
val fdroidArch = FDroidConfig.fDroidBuildArch!!
val fdroidAapt2 = FDroidConfig.fDroidAapt2File!!

require(fdroidArch in flavorsAbis.keys) {
"F-Droid arch '${fdroidArch}' is not supported!"
}


val inFile = File(fdroidAapt2)
require(inFile.exists() && inFile.isFile) {
"F-Droid AAPT2 file does not exist or is not a file: $inFile"
}

val assetDir = project.layout.buildDirectory.dir("intermediates/fdroid-aapt2-${flavorName}")
.get().asFile

if (assetDir.exists()) {
assetDir.deleteRecursively()
}

assetDir.mkdirs()

val outFile = File(assetDir, "data/$flavorName/aapt2")
outFile.parentFile.mkdirs()

inFile.copyTo(outFile, overwrite = true)

return assetDir.absolutePath
}

private fun Project.getAapt2DownloadTasks(
baseExtension: BaseExtension
): Map<String, TaskProvider<DownloadAapt2Task>> {

if (FDroidConfig.isFDroidBuild) {
// Do not download AAPT2 when building for F-Droid
return emptyMap()
}

val aapt2Tasks = mutableMapOf<String, TaskProvider<DownloadAapt2Task>>()

baseExtension.productFlavors.forEach { flavor ->

val task = tasks.register(
"downloadAapt2${flavor.name.capitalized()}",
DownloadAapt2Task::class.java
) {
arch.set(flavor.name)
checksum.set(AAPT2_CHECKSUMS[flavor.name] ?: throw IllegalStateException(
"Checksum for aapt2-${flavor.name} not found!"))
}

aapt2Tasks[flavor.name] = task
}

return aapt2Tasks
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import projectVersionCode
* For example, if the base version code of the IDE is 270 (for v2.7.0), then for arm64-v8a
* flavor, the version code will be `100 * 270 + 1` i.e. `2701`
*/
// IMPORTANT: When changing the configuration here, make sure to update the following file:
// - <root>/scripts/setup_fdroid_build.sh
private val flavorsAbis = mapOf("arm64-v8a" to 1, "armeabi-v7a" to 2, "x86_64" to 3)
internal val flavorsAbis = mapOf("arm64-v8a" to 1, "armeabi-v7a" to 2, "x86_64" to 3)
private val disableCoreLibDesugaringForModules = arrayOf(":logsender", ":logger")

fun Project.configureAndroidModule(
Expand Down
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ buildscript {
}

subprojects {
// Always load the F-Droid config
FDroidConfig.load(project)

afterEvaluate {
apply { plugin(AndroidIDEPlugin::class.java) }
}
Expand Down

0 comments on commit b0f8feb

Please sign in to comment.