Skip to content

Commit fba0e24

Browse files
authored
Support App Bundles (aab) and AGP 3.6.x, bug fix (#820)
1 parent e7e150c commit fba0e24

1 file changed

Lines changed: 80 additions & 22 deletions

File tree

sentry-android-gradle-plugin/src/main/groovy/io/sentry/android/gradle/SentryPlugin.groovy

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,19 @@ class SentryPlugin implements Plugin<Project> {
118118
}
119119

120120
/**
121-
* Returns the proguard task for the given project and variant.
121+
* Returns the transformer task for the given project and variant.
122+
* It could be either ProGuard or R8
122123
*
123-
* @param project
124-
* @param variant
125-
* @return
124+
* @param project the given project
125+
* @param variant the given variant
126+
* @return the task or null otherwise
126127
*/
127-
static Task getProguardTask(Project project, ApplicationVariant variant) {
128+
static Task getTransformerTask(Project project, ApplicationVariant variant) {
128129
def names = [
129130
// Android Studio 3.3 includes the R8 shrinker.
130131
"transformClassesAndResourcesWithR8For${variant.name.capitalize()}",
131-
"transformClassesAndResourcesWithProguardFor${variant.name.capitalize()}"
132+
"transformClassesAndResourcesWithProguardFor${variant.name.capitalize()}",
133+
"minify${variant.name.capitalize()}WithR8"
132134
]
133135

134136
return names.findResult { project.tasks.findByName(it) } ?: project.tasks.findByName("proguard${names[1]}")
@@ -152,16 +154,27 @@ class SentryPlugin implements Plugin<Project> {
152154
}
153155

154156
/**
155-
* Returns the bundle task for the given project and variant.
157+
* Returns the pre bundle task for the given project and variant.
156158
*
157159
* @param project
158160
* @param variant
159161
* @return
160162
*/
161-
static Task getBundleTask(Project project, ApplicationVariant variant) {
163+
static Task getPreBundleTask(Project project, ApplicationVariant variant) {
162164
return project.tasks.findByName("build${variant.name.capitalize()}PreBundle")
163165
}
164166

167+
/**
168+
* Returns the pre bundle task for the given project and variant.
169+
*
170+
* @param project
171+
* @param variant
172+
* @return
173+
*/
174+
static Task getBundleTask(Project project, ApplicationVariant variant) {
175+
return project.tasks.findByName("bundle${variant.name.capitalize()}")
176+
}
177+
165178
/**
166179
* Returns the path to the debug meta properties file for the given variant.
167180
*
@@ -221,7 +234,7 @@ class SentryPlugin implements Plugin<Project> {
221234
}
222235

223236
def mappingFile = variant.getMappingFile()
224-
def proguardTask = getProguardTask(project, variant)
237+
def transformerTask = getTransformerTask(project, variant)
225238

226239
def dexTask = getDexTask(project, variant)
227240
if (dexTask != null) {
@@ -230,18 +243,25 @@ class SentryPlugin implements Plugin<Project> {
230243
project.logger.info("dexTask is null")
231244
}
232245

246+
def preBundleTask = getPreBundleTask(project, variant)
247+
if (preBundleTask != null) {
248+
project.logger.info("preBundleTask ${preBundleTask.path}")
249+
} else {
250+
project.logger.info("preBundleTask is null")
251+
}
252+
233253
def bundleTask = getBundleTask(project, variant)
234254
if (bundleTask != null) {
235255
project.logger.info("bundleTask ${bundleTask.path}")
236256
} else {
237257
project.logger.info("bundleTask is null")
238258
}
239259

240-
if (proguardTask == null) {
241-
project.logger.info("proguardTask is null")
260+
if (transformerTask == null) {
261+
project.logger.info("transformerTask is null")
242262
return
243263
} else {
244-
project.logger.info("proguardTask ${proguardTask.path}")
264+
project.logger.info("transformerTask ${transformerTask.path}")
245265
}
246266

247267
// create a task to configure proguard automatically unless the user disabled it.
@@ -253,7 +273,7 @@ class SentryPlugin implements Plugin<Project> {
253273
SentryProguardConfigTask)
254274
proguardConfigTask.group = GROUP_NAME
255275
proguardConfigTask.applicationVariant = variant
256-
proguardTask.dependsOn proguardConfigTask
276+
transformerTask.dependsOn proguardConfigTask
257277
}
258278
}
259279

@@ -371,19 +391,35 @@ class SentryPlugin implements Plugin<Project> {
371391
enabled true
372392
}
373393

394+
374395
// and run before dex transformation. If we managed to find the dex task
375396
// we set ourselves as dependency, otherwise we just hack outselves into
376397
// the proguard task's doLast.
377398
if (dexTask != null) {
378399
dexTask.dependsOn persistIdsTask
379-
} else {
380-
proguardTask.finalizedBy persistIdsTask
381400
}
401+
402+
if (transformerTask != null) {
403+
transformerTask.finalizedBy persistIdsTask
404+
}
405+
382406
// To include proguard uuid file into aab, run before bundle task.
383-
if (bundleTask != null) {
384-
bundleTask.dependsOn persistIdsTask
407+
if (preBundleTask != null) {
408+
preBundleTask.dependsOn persistIdsTask
409+
}
410+
411+
// find the package task
412+
def packageTask = getPackageTask(project, variant)
413+
if (packageTask != null) {
414+
project.logger.info("packageTask ${packageTask.path}")
415+
} else {
416+
packageTask.logger.info("packageTask is null")
417+
}
418+
419+
// the package task will only be executed if the persistIdsTask has already been executed.
420+
if (packageTask != null) {
421+
packageTask.dependsOn persistIdsTask
385422
}
386-
persistIdsTask.dependsOn proguardTask
387423

388424
// find the assemble task
389425
def assembleTask = findAssembleTask(variant)
@@ -395,10 +431,17 @@ class SentryPlugin implements Plugin<Project> {
395431

396432
// uploadNativeSymbolsTask only will be executed after the assemble task
397433
// and also only if uploadNativeSymbols is enabled, this is opt-in feature
398-
if (assembleTask != null && extension.uploadNativeSymbols) {
399-
assembleTask.finalizedBy uploadNativeSymbolsTask
400-
} else {
401-
assembleTask.logger.info("uploadNativeSymbolsTask won't be executed")
434+
if (assembleTask != null) {
435+
if (extension.uploadNativeSymbols) {
436+
assembleTask.finalizedBy uploadNativeSymbolsTask
437+
438+
// if its a bundle aab, assemble might not be executed, so we hook into bundle task
439+
if (bundleTask != null) {
440+
bundleTask.finalizedBy uploadNativeSymbolsTask
441+
}
442+
} else {
443+
assembleTask.logger.info("uploadNativeSymbolsTask won't be executed")
444+
}
402445
}
403446
}
404447
}
@@ -505,4 +548,19 @@ class SentryPlugin implements Plugin<Project> {
505548

506549
return propsFile
507550
}
551+
552+
/**
553+
* Returns the package task
554+
* @param project the given project
555+
* @param variant the given variant
556+
* @return the package task or null if not found
557+
*/
558+
static Task getPackageTask(Project project, ApplicationVariant variant) {
559+
def names = [
560+
"package${variant.name.capitalize()}",
561+
"package${variant.name.capitalize()}Bundle"
562+
]
563+
564+
return names.findResult { project.tasks.findByName(it) }
565+
}
508566
}

0 commit comments

Comments
 (0)