Skip to content

Commit

Permalink
Support Reaper for APKs
Browse files Browse the repository at this point in the history
  • Loading branch information
rbro112 committed Sep 19, 2024
1 parent 929b9f2 commit cb2a47d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,30 @@ private fun registerReaperUploadTask(
) {
val uploadReaperAabTaskName = "${EMERGE_TASK_PREFIX}UploadReaperAab${variant.name.capitalize()}"
val uploadReaperAabTask =
appProject.tasks.register(uploadReaperAabTaskName, InitializeReaper::class.java) {
appProject.tasks.register(uploadReaperAabTaskName, UploadReaperAab::class.java) {
it.artifact.set(variant.artifacts.get(SingleArtifact.BUNDLE))
it.publishableApiKey.set(extension.reaperOptions.publishableApiKey)
it.setUploadTaskInputs(extension, appProject, variant)
it.setTagFromProductOptions(extension.reaperOptions, variant)
}
// Hook the bundle tasks to run the reaper upload task after they complete.


val uploadReaperApkTaskName = "${EMERGE_TASK_PREFIX}UploadReaperApk${variant.name.capitalize()}"
val uploadReaperApkTask =
appProject.tasks.register(uploadReaperApkTaskName, UploadReaperApk::class.java) {
it.artifactDir.set(variant.artifacts.get(SingleArtifact.APK))
it.proguardMapping.set(variant.artifacts.get(SingleArtifact.OBFUSCATION_MAPPING_FILE))
it.publishableApiKey.set(extension.reaperOptions.publishableApiKey)
it.setUploadTaskInputs(extension, appProject, variant)
it.setTagFromProductOptions(extension.reaperOptions, variant)
}

// Hook the bundle & assemble tasks to run the reaper upload task after they complete.
appProject.afterEvaluate { project ->
val bundleTask = project.tasks.named("bundle${variant.name.capitalize()}")
bundleTask.configure { it.finalizedBy(uploadReaperAabTask) }

val assembleTask = project.tasks.named("assemble${variant.name.capitalize()}")
assembleTask.configure { it.finalizedBy(uploadReaperApkTask) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.emergetools.android.gradle.tasks.reaper
import com.emergetools.android.gradle.BuildConfig
import com.emergetools.android.gradle.tasks.base.ArtifactMetadata
import com.emergetools.android.gradle.tasks.base.BaseUploadTask
import com.emergetools.android.gradle.tasks.size.UploadAAB
import kotlinx.datetime.Clock
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
Expand All @@ -15,25 +16,13 @@ import org.gradle.api.tasks.TaskAction
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

abstract class InitializeReaper : BaseUploadTask() {

@get:InputFile
@get:PathSensitive(PathSensitivity.NAME_ONLY)
abstract val artifact: RegularFileProperty
abstract class UploadReaperAab : UploadAAB() {

@get:Input
abstract val publishableApiKey: Property<String>

override fun includeFilesInUpload(zos: ZipOutputStream) {
artifact.get().asFile.inputStream().use { inputStream ->
zos.putNextEntry(ZipEntry(artifact.get().asFile.name))
inputStream.copyTo(zos)
zos.closeEntry()
}
}

@TaskAction
fun execute() {
override fun execute() {
if (publishableApiKey.orNull == null) {
throw StopExecutionException("publishableApiKey must be set for Reaper to work properly. See https://docs.emergetools.com/docs/reaper-setup-android#configure-the-sdk.")
}
Expand All @@ -53,9 +42,4 @@ abstract class InitializeReaper : BaseUploadTask() {
logger.lifecycle("Note: Initial Reaper processing can take up to 10 minutes.")
}
}

companion object {
const val AAB_PROGUARD_PATH =
"BUNDLE-METADATA/com.android.tools.build.obfuscation/proguard.map"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.emergetools.android.gradle.tasks.reaper

import com.emergetools.android.gradle.BuildConfig
import com.emergetools.android.gradle.tasks.base.ArtifactMetadata
import com.emergetools.android.gradle.tasks.base.BaseUploadTask
import com.emergetools.android.gradle.tasks.size.UploadAPK
import com.emergetools.android.gradle.tasks.size.UploadAPK.Companion.APK_EXTENSION
import kotlinx.datetime.Clock
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.StopExecutionException
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

abstract class UploadReaperApk : UploadAPK() {

@get:Input
abstract val publishableApiKey: Property<String>

@TaskAction
override fun execute() {
if (publishableApiKey.orNull == null) {
throw StopExecutionException("publishableApiKey must be set for Reaper to work properly. See https://docs.emergetools.com/docs/reaper-setup-android#configure-the-sdk.")
}

val artifactName = primaryArtifact.name
val proguardMappingName = proguardMapping.asFile.orNull?.name
val artifactMetadata = ArtifactMetadata(
created = Clock.System.now(),
emergeGradlePluginVersion = BuildConfig.VERSION,
androidGradlePluginVersion = agpVersion.get(),
targetArtifactZipPath = artifactName,
proguardMappingsZipPath = proguardMappingName,
)

upload(artifactMetadata) { response ->
logger.lifecycle("Reaper initialized! View Reaper reports for this version at the following url:")
logger.lifecycle("https://emergetools.com/reaper/${response.uploadId}")
logger.lifecycle("Note: Initial Reaper processing can take up to 10 minutes.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class UploadAAB : BaseUploadTask() {
}

@TaskAction
fun execute() {
open fun execute() {
val artifactName = artifact.get().asFile.name
val artifactMetadata = ArtifactMetadata(
created = Clock.System.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class UploadAPK : BaseUploadTask() {
@get:PathSensitive(PathSensitivity.NAME_ONLY)
abstract val proguardMapping: RegularFileProperty

private val primaryArtifact: File
protected val primaryArtifact: File
get() {
val apks = artifactDir.get().asFileTree.filter { it.extension == APK_EXTENSION }
check(apks.files.size < 2) {
Expand All @@ -54,7 +54,7 @@ abstract class UploadAPK : BaseUploadTask() {
}

@TaskAction
fun doExecute() {
open fun execute() {
val artifactName = primaryArtifact.name
val proguardMappingName = proguardMapping.asFile.orNull?.name
val artifactMetadata = ArtifactMetadata(
Expand Down

0 comments on commit cb2a47d

Please sign in to comment.