-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathEmbraceGradlePluginDelegate.kt
More file actions
138 lines (120 loc) · 5.6 KB
/
EmbraceGradlePluginDelegate.kt
File metadata and controls
138 lines (120 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
@file:Suppress("DEPRECATION")
package io.embrace.android.gradle.plugin
import io.embrace.android.gradle.plugin.agp.AgpUtils
import io.embrace.android.gradle.plugin.agp.AgpVersion
import io.embrace.android.gradle.plugin.agp.AgpWrapper
import io.embrace.android.gradle.plugin.agp.AgpWrapperImpl
import io.embrace.android.gradle.plugin.api.EmbraceExtension
import io.embrace.android.gradle.plugin.buildreporter.BuildTelemetryService
import io.embrace.android.gradle.plugin.config.PluginBehaviorImpl
import io.embrace.android.gradle.plugin.config.variant.EmbraceVariantConfigurationBuilder
import io.embrace.android.gradle.plugin.gradle.getProperty
import io.embrace.android.gradle.plugin.instrumentation.config.model.VariantConfig
import io.embrace.android.gradle.plugin.instrumentation.registerAsmTasks
import io.embrace.android.gradle.plugin.tasks.registration.TaskRegistrar
import io.embrace.android.gradle.swazzler.plugin.extension.SwazzlerExtension
import org.gradle.api.Project
import org.gradle.api.provider.ListProperty
/**
* Entry point for Android-specific code in the Embrace gradle plugin.
*/
class EmbraceGradlePluginDelegate {
private val logger: Logger<EmbraceGradlePluginDelegate> = Logger(EmbraceGradlePluginDelegate::class.java)
fun onAndroidPluginApplied(
project: Project,
variantConfigurationsListProperty: ListProperty<VariantConfig>,
extension: SwazzlerExtension,
embrace: EmbraceExtension,
) {
val agpWrapper = AgpWrapperImpl(project)
validateMinAgpVersion(agpWrapper)
val behavior = PluginBehaviorImpl(project, extension, embrace)
BuildTelemetryService.register(
project,
variantConfigurationsListProperty,
behavior,
agpWrapper
)
// bytecode instrumentation must be registered before project evaluation
registerAsmTasks(project, behavior, variantConfigurationsListProperty)
val embraceVariantConfigurationBuilder =
EmbraceVariantConfigurationBuilder(
project.layout.projectDirectory,
project.providers
)
val taskRegistrar = TaskRegistrar(
project,
behavior,
embraceVariantConfigurationBuilder,
variantConfigurationsListProperty,
agpWrapper
)
taskRegistrar.registerTasks()
project.afterEvaluate { evaluatedProject ->
onProjectEvaluated(evaluatedProject, agpWrapper)
}
}
/**
* It gets called once this project has been evaluated.
*/
private fun onProjectEvaluated(evaluatedProject: Project, agpWrapper: AgpWrapper) {
verifyDesugaringForOldDevices(agpWrapper)
verifySemConvWorkaround(evaluatedProject, agpWrapper)
}
private fun verifyDesugaringForOldDevices(agpWrapper: AgpWrapper) {
var enableDesugaring = false
try {
val shouldEnableDesugaring = when (val minSdk = agpWrapper.minSdk) {
null -> true
else -> AgpUtils.isDesugaringRequired(minSdk)
}
if (shouldEnableDesugaring) {
enableDesugaring = !agpWrapper.isCoreLibraryDesugaringEnabled
}
} catch (e: Throwable) {
logger.info(
"There was an exception while checking if desugaring is required. " +
"We will consider desugaring is not required. Build will continue normally."
)
}
if (enableDesugaring) {
error(
"Desugaring must be enabled when minSdk is < 24, " +
"otherwise devices on Android API version < 24 will fail at runtime.\n" +
"You can enable desugaring by adding the following:\n" +
"compileOptions\n" +
"{\n" +
" coreLibraryDesugaringEnabled = true\n" +
"}\n" +
"dependencies {\n" +
" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'\n" +
"}\n" +
"in your app\'s module build.gradle file. Use a newer version of the desugaring library if required.\n" +
"You can find more info at: \n" +
"https://developer.android.com/studio/write/java8-support#library-desugaring"
)
}
}
private fun verifySemConvWorkaround(project: Project, agpWrapper: AgpWrapper) {
val minSdk = agpWrapper.minSdk ?: return
if (minSdk < 24) {
if (agpWrapper.version <= AgpVersion.AGP_8_3_0 ||
project.getProperty("android.useFullClasspathForDexingTransform").orNull != "true"
) {
error(
"To use the Embrace SDK when your minSdk is lower than 24 " +
"you must use AGP 8.3.0+ and add android.useFullClasspathForDexingTransform=true to " +
"gradle.properties.\nAlternatively you can set your minSdk to 24 or higher.\n" +
"This avoids a desugaring bug in old AGP versions that will lead to runtime crashes on old devices.\n" +
"For the full context for this workaround, please see the following issue:" +
" https://issuetracker.google.com/issues/230454566#comment18"
)
}
}
}
private fun validateMinAgpVersion(agpWrapper: AgpWrapper) {
if (agpWrapper.version < AgpVersion.MIN_VERSION) {
error("Embrace requires AGP version ${AgpVersion.MIN_VERSION} or higher. Please update your AGP version.")
}
}
}