1
1
package app.revanced.patches.gradle
2
2
3
+ import com.android.tools.build.apkzlib.zip.ZFile
4
+ import com.android.tools.r8.CompilationMode
5
+ import com.android.tools.r8.D8
6
+ import com.android.tools.r8.D8Command
7
+ import com.android.tools.r8.OutputMode
8
+ import com.android.tools.r8.utils.ArchiveResourceProvider
3
9
import kotlinx.validation.BinaryCompatibilityValidatorPlugin
4
10
import org.gradle.api.JavaVersion
5
11
import org.gradle.api.Plugin
@@ -12,12 +18,10 @@ import org.gradle.api.publish.maven.MavenPublication
12
18
import org.gradle.api.tasks.SourceSetContainer
13
19
import org.gradle.jvm.tasks.Jar
14
20
import org.gradle.kotlin.dsl.get
15
- import org.gradle.kotlin.dsl.support.listFilesOrdered
16
21
import org.gradle.plugins.signing.SigningExtension
17
22
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
18
23
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
19
24
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
20
- import java.io.File
21
25
22
26
@Suppress(" unused" )
23
27
abstract class PatchesPlugin : Plugin <Project > {
@@ -102,32 +106,32 @@ abstract class PatchesPlugin : Plugin<Project> {
102
106
}
103
107
104
108
/* *
105
- * Adds a task to build the DEX file of the patches and add it to the patches file to use on Android,
109
+ * Adds a task to build the DEX file of the patches and adds it to the patches file to use on Android,
106
110
* adds the publishing plugin to the project to publish the patches API and
107
111
* configures the publication with the "about" information from the extension.
108
112
*/
109
113
private fun Project.configurePublishing (patchesExtension : PatchesExtension ) {
110
- tasks.register(" buildDexJar " ) {
111
- it .description = " Build and add a DEX to the JAR file"
112
- it .group = " build"
114
+ val buildAndroid = tasks.register(" buildAndroid " ) { task ->
115
+ task .description = " Builds the project for Android by compiling to DEX and adding it to the patches file. "
116
+ task .group = " build"
113
117
114
- it .dependsOn(tasks[" build " ])
118
+ task .dependsOn(tasks[" jar " ])
115
119
116
- it.doLast {
117
- val d8 = File (System .getenv(" ANDROID_HOME" )).resolve(" build-tools" )
118
- .listFilesOrdered().last().resolve(" d8" ).absolutePath
120
+ task.doLast {
121
+ val workingDirectory = layout.buildDirectory.dir(" revanced" ).get().asFile
119
122
120
- val patchesJar = configurations[ " archives " ].allArtifacts .files.files. first().absolutePath
121
- val workingDirectory = layout.buildDirectory.dir( " libs " ).get().asFile
123
+ val patchesFile = tasks[ " jar " ].outputs .files.first()
124
+ val classesZipFile = workingDirectory.resolve( " classes.zip " )
122
125
123
- exec { execSpec ->
124
- execSpec.workingDir = workingDirectory
125
- execSpec.commandLine = listOf (d8, " --release" , patchesJar)
126
- }
126
+ D8Command .builder()
127
+ .addProgramResourceProvider(ArchiveResourceProvider .fromArchive(patchesFile.toPath(), true ))
128
+ .setMode(CompilationMode .RELEASE )
129
+ .setOutput(classesZipFile.toPath(), OutputMode .DexIndexed )
130
+ .build()
131
+ .let (D8 ::run)
127
132
128
- exec { execSpec ->
129
- execSpec.workingDir = workingDirectory
130
- execSpec.commandLine = listOf (" zip" , " -u" , patchesJar, " classes.dex" )
133
+ ZFile .openReadWrite(patchesFile).use { zFile ->
134
+ zFile.mergeFrom(ZFile .openReadOnly(classesZipFile)) { false }
131
135
}
132
136
}
133
137
}
@@ -167,7 +171,7 @@ abstract class PatchesPlugin : Plugin<Project> {
167
171
// Used by gradle-semantic-release-plugin.
168
172
// Tracking: https://github.com/KengoTODA/gradle-semantic-release-plugin/issues/435
169
173
tasks[" publish" ].apply {
170
- dependsOn(" buildDexJar " )
174
+ dependsOn(buildAndroid )
171
175
}
172
176
}
173
177
@@ -232,4 +236,3 @@ private fun Project.configureJarTask(patchesExtension: PatchesExtension) {
232
236
}
233
237
}
234
238
}
235
-
0 commit comments