From a6a5597c289c57cdf3322bab92984eb3595d2d2b Mon Sep 17 00:00:00 2001 From: C10udburst <18114966+C10udburst@users.noreply.github.com> Date: Tue, 9 Aug 2022 18:04:41 +0200 Subject: [PATCH] add resource compilation --- .../gradle/CloudstreamExtension.kt | 1 + .../lagradost/cloudstream3/gradle/Utils.kt | 1 + .../gradle/entities/PluginManifest.kt | 3 +- .../gradle/tasks/CompileResourcesTask.kt | 57 +++++++++++++++++++ .../cloudstream3/gradle/tasks/Tasks.kt | 29 +++++++++- 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/com/lagradost/cloudstream3/gradle/tasks/CompileResourcesTask.kt diff --git a/src/main/kotlin/com/lagradost/cloudstream3/gradle/CloudstreamExtension.kt b/src/main/kotlin/com/lagradost/cloudstream3/gradle/CloudstreamExtension.kt index 326795f..e71f4bd 100644 --- a/src/main/kotlin/com/lagradost/cloudstream3/gradle/CloudstreamExtension.kt +++ b/src/main/kotlin/com/lagradost/cloudstream3/gradle/CloudstreamExtension.kt @@ -38,6 +38,7 @@ abstract class CloudstreamExtension @Inject constructor(project: Project) { internal var pluginClassName: String? = null + var requiresResources = false var description: String? = null var authors = listOf() var adult = false diff --git a/src/main/kotlin/com/lagradost/cloudstream3/gradle/Utils.kt b/src/main/kotlin/com/lagradost/cloudstream3/gradle/Utils.kt index 31d5e8e..a257ddf 100644 --- a/src/main/kotlin/com/lagradost/cloudstream3/gradle/Utils.kt +++ b/src/main/kotlin/com/lagradost/cloudstream3/gradle/Utils.kt @@ -21,6 +21,7 @@ fun Project.makeManifest(): PluginManifest { pluginClassName = extension.pluginClassName, name = this.name, version = version ?: -1, + requiresResources = extension.requiresResources ) } diff --git a/src/main/kotlin/com/lagradost/cloudstream3/gradle/entities/PluginManifest.kt b/src/main/kotlin/com/lagradost/cloudstream3/gradle/entities/PluginManifest.kt index 4c8fe29..966f02b 100644 --- a/src/main/kotlin/com/lagradost/cloudstream3/gradle/entities/PluginManifest.kt +++ b/src/main/kotlin/com/lagradost/cloudstream3/gradle/entities/PluginManifest.kt @@ -3,5 +3,6 @@ package com.lagradost.cloudstream3.gradle.entities data class PluginManifest( val pluginClassName: String?, val name: String, - val version: Int + val version: Int, + val requiresResources: Boolean ) \ No newline at end of file diff --git a/src/main/kotlin/com/lagradost/cloudstream3/gradle/tasks/CompileResourcesTask.kt b/src/main/kotlin/com/lagradost/cloudstream3/gradle/tasks/CompileResourcesTask.kt new file mode 100644 index 0000000..f1a009b --- /dev/null +++ b/src/main/kotlin/com/lagradost/cloudstream3/gradle/tasks/CompileResourcesTask.kt @@ -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() + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/lagradost/cloudstream3/gradle/tasks/Tasks.kt b/src/main/kotlin/com/lagradost/cloudstream3/gradle/tasks/Tasks.kt index fd69a23..481dae7 100644 --- a/src/main/kotlin/com/lagradost/cloudstream3/gradle/tasks/Tasks.kt +++ b/src/main/kotlin/com/lagradost/cloudstream3/gradle/tasks/Tasks.kt @@ -55,6 +55,31 @@ fun registerTasks(project: Project) { 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.tasks.register("make", Zip::class.java) { val compileDexTask = compileDex.get() @@ -81,7 +106,9 @@ fun registerTasks(project: Project) { it.from(compileDexTask.outputFile) val zip = it as Zip - //zip.dependsOn(compileResources.get()) + if (extension.requiresResources) { + zip.dependsOn(compileResources.get()) + } zip.isPreserveFileTimestamps = false zip.archiveBaseName.set(project.name) zip.archiveExtension.set("cs3")