add resource compilation

This commit is contained in:
C10udburst 2022-08-09 18:04:41 +02:00
parent 7f27d97abb
commit a6a5597c28
5 changed files with 89 additions and 2 deletions

View file

@ -38,6 +38,7 @@ abstract class CloudstreamExtension @Inject constructor(project: Project) {
internal var pluginClassName: String? = null internal var pluginClassName: String? = null
var requiresResources = false
var description: String? = null var description: String? = null
var authors = listOf<String>() var authors = listOf<String>()
var adult = false var adult = false

View file

@ -21,6 +21,7 @@ fun Project.makeManifest(): PluginManifest {
pluginClassName = extension.pluginClassName, pluginClassName = extension.pluginClassName,
name = this.name, name = this.name,
version = version ?: -1, version = version ?: -1,
requiresResources = extension.requiresResources
) )
} }

View file

@ -3,5 +3,6 @@ package com.lagradost.cloudstream3.gradle.entities
data class PluginManifest( data class PluginManifest(
val pluginClassName: String?, val pluginClassName: String?,
val name: String, val name: String,
val version: Int val version: Int,
val requiresResources: Boolean
) )

View file

@ -0,0 +1,57 @@
package com.lagradost.cloudstream3.gradle.tasks
import com.android.build.gradle.BaseExtension
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.*
import org.gradle.internal.os.OperatingSystem
import java.io.File
abstract class CompileResourcesTask : Exec() {
@get:InputDirectory
@get:SkipWhenEmpty
@get:IgnoreEmptyDirectories
abstract val input: DirectoryProperty
@get:InputFile
abstract val manifestFile: RegularFileProperty
@get:OutputFile
abstract val outputFile: RegularFileProperty
override fun exec() {
val android = project.extensions.getByName("android") as BaseExtension
val aaptExecutable = android.sdkDirectory.resolve("build-tools")
.resolve(android.buildToolsVersion)
.resolve(if (OperatingSystem.current().isWindows) "aapt2.exe" else "aapt2")
val tmpRes = File.createTempFile("res", ".zip")
execActionFactory.newExecAction().apply {
executable = aaptExecutable.path
args("compile")
args("--dir", input.asFile.get().path)
args("-o", tmpRes.path)
execute()
}
execActionFactory.newExecAction().apply {
executable = aaptExecutable.path
args("link")
args(
"-I",
android.sdkDirectory
.resolve("platforms")
.resolve(android.compileSdkVersion!!)
.resolve("android.jar")
)
args("-R", tmpRes.path)
args("--manifest", manifestFile.asFile.get().path)
args("-o", outputFile.asFile.get().path)
execute()
}
tmpRes.delete()
}
}

View file

@ -55,6 +55,31 @@ fun registerTasks(project: Project) {
it.outputFile.set(intermediates.resolve("classes.dex")) it.outputFile.set(intermediates.resolve("classes.dex"))
} }
val compileResources = project.tasks.register("compileResources", CompileResourcesTask::class.java) {
it.group = TASK_GROUP
val processManifestTask = project.tasks.getByName("processDebugManifest") as ProcessLibraryManifest
it.dependsOn(processManifestTask)
val android = project.extensions.getByName("android") as BaseExtension
it.input.set(android.sourceSets.getByName("main").res.srcDirs.single())
it.manifestFile.set(processManifestTask.manifestOutputFile)
it.outputFile.set(intermediates.resolve("res.apk"))
it.doLast { _ ->
val resApkFile = it.outputFile.asFile.get()
if (resApkFile.exists()) {
project.tasks.named("make", AbstractCopyTask::class.java) {
it.from(project.zipTree(resApkFile)) { copySpec ->
copySpec.exclude("AndroidManifest.xml")
}
}
}
}
}
project.afterEvaluate { project.afterEvaluate {
project.tasks.register("make", Zip::class.java) { project.tasks.register("make", Zip::class.java) {
val compileDexTask = compileDex.get() val compileDexTask = compileDex.get()
@ -81,7 +106,9 @@ fun registerTasks(project: Project) {
it.from(compileDexTask.outputFile) it.from(compileDexTask.outputFile)
val zip = it as Zip val zip = it as Zip
//zip.dependsOn(compileResources.get()) if (extension.requiresResources) {
zip.dependsOn(compileResources.get())
}
zip.isPreserveFileTimestamps = false zip.isPreserveFileTimestamps = false
zip.archiveBaseName.set(project.name) zip.archiveBaseName.set(project.name)
zip.archiveExtension.set("cs3") zip.archiveExtension.set("cs3")