fix: generate sources for selected variant when XML files change

This commit is contained in:
Akash Yadav
2023-08-28 22:53:03 +05:30
parent 98d6cc2937
commit f3351d1867
4 changed files with 51 additions and 36 deletions
@@ -57,20 +57,9 @@ class QuickRunAction(context: Context) : BaseBuildAction() {
val activity = data.requireActivity()
val projectManager = IProjectManager.getInstance()
val info = projectManager.androidBuildVariants[module.path]
if (info == null) {
log.error(
"Cannnot run application. Failed to find selected build variant for module: '${module.path}'")
activity.flashError(activity.getString(R.string.err_no_selected_variant, module.path))
return@chooseApplication
}
val variant = module.getVariant(info.selectedVariant)
if (variant == null) {
log.error(
"Cannot run application. Build variant with name '${info.selectedVariant}' not found.")
val variant = module.getSelectedVariant() ?: run {
activity.flashError(
activity.getString(R.string.err_selected_variant_not_found, info.selectedVariant))
activity.getString(R.string.err_selected_variant_not_found))
return@chooseApplication
}
+1 -4
View File
@@ -599,8 +599,5 @@
<string name="title_discard">Discard</string>
<string name="msg_build_variants_fetch_failed">Failed to fetch build variants</string>
<string name="title_choose_application">Choose application</string>
<string name="err_no_selected_variant">Failed to find selected build variant for module: %1$s</string>
<!-- Build variant with name 'something' not found-->
<string name="err_selected_variant_not_found">Build variant with name \'%1$s\' not found.</string>
<string name="err_selected_variant_not_found">Selected build variant not found</string>
</resources>
@@ -46,6 +46,7 @@ import org.greenrobot.eventbus.ThreadMode
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.util.Locale
import kotlin.io.path.extension
import kotlin.io.path.isDirectory
import kotlin.io.path.pathString
@@ -184,33 +185,37 @@ class ProjectManagerImpl : IProjectManager, EventReceiver {
return
}
val debug = app!!.getVariant("debug")
if (debug == null) {
log.warn("No debug variant found in application project ${app!!.name}")
return
val tasks = getAndroidModules().flatMap { module ->
val variant = module.getSelectedVariant()
if (variant == null) {
log.error(
"Selected build variant for project '${module.path}' not found")
return@flatMap emptyList()
}
val mainArtifact = variant.mainArtifact
val variantNameCapitalized = variant.name.replaceFirstChar {
if (it.isLowerCase()) it.titlecase(Locale.ROOT) else it.toString()
}
return@flatMap listOf(
mainArtifact.resGenTaskName,
mainArtifact.sourceGenTaskName,
if(module.viewBindingOptions.isEnabled) "dataBindingGenBaseClasses$variantNameCapitalized" else null,
"process${variantNameCapitalized}Resources"
).mapNotNull { it?.let { "${module.path}:${it}" } }
}
val mainArtifact = debug.mainArtifact
val genResourcesTask = mainArtifact.resGenTaskName
val genSourcesTask = mainArtifact.sourceGenTaskName
val genDataBinding = // If view binding is enabled, generate the view binding classes too
if (app!!.viewBindingOptions.isEnabled) {
"dataBindingGenBaseClassesDebug"
} else {
""
}
builder
.executeProjectTasks(
app!!.path,
genResourcesTask ?: "",
genSourcesTask,
"processDebugResources",
genDataBinding
*tasks.toTypedArray()
)
.whenComplete { result, taskErr ->
if (taskErr != null || !result.isSuccessful) {
if (result == null || !result.isSuccessful || taskErr != null) {
log.warn(
"Execution for tasks '$genResourcesTask' and '$genSourcesTask' failed.",
"Execution for tasks failed: $tasks",
taskErr ?: ""
)
} else {
@@ -465,5 +465,29 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
return ResourceTableRegistry.getInstance().getFeatures(getPlatformDir() ?: return emptyList())
}
/**
* Returns the build variant that is selected by the user. This may return `null` in
* some misconfiguration scenarios.
*/
fun getSelectedVariant(): BasicAndroidVariantMetadata? {
val projectManager = IProjectManager.getInstance()
val info = projectManager.androidBuildVariants[this.path]
if (info == null) {
log.error(
"Failed to find selected build variant for module: '${this.path}'")
return null
}
val variant = this.getVariant(info.selectedVariant)
if (variant == null) {
log.error(
"Build variant with name '${info.selectedVariant}' not found.")
return null
}
return variant
}
private fun getPlatformDir() = bootClassPaths.firstOrNull { it.name == "android.jar" }?.parentFile
}