|
15 | 15 | */ |
16 | 16 | package net.bytebuddy.build.gradle.android; |
17 | 17 |
|
| 18 | +import com.android.build.api.dsl.ApplicationExtension; |
| 19 | +import com.android.build.api.variant.AndroidComponentsExtension; |
18 | 20 | import com.android.build.gradle.BaseExtension; |
19 | 21 | import net.bytebuddy.ByteBuddy; |
20 | 22 | import net.bytebuddy.ClassFileVersion; |
|
29 | 31 | import org.gradle.api.DefaultTask; |
30 | 32 | import org.gradle.api.GradleException; |
31 | 33 | import org.gradle.api.JavaVersion; |
| 34 | +import org.gradle.api.UnknownDomainObjectException; |
32 | 35 | import org.gradle.api.file.ConfigurableFileCollection; |
33 | 36 | import org.gradle.api.file.Directory; |
34 | 37 | import org.gradle.api.file.FileCollection; |
35 | 38 | import org.gradle.api.file.RegularFile; |
36 | 39 | import org.gradle.api.file.RegularFileProperty; |
37 | 40 | import org.gradle.api.logging.Logger; |
| 41 | +import org.gradle.api.plugins.ExtensionContainer; |
38 | 42 | import org.gradle.api.provider.ListProperty; |
39 | 43 | import org.gradle.api.provider.Property; |
40 | 44 | import org.gradle.api.tasks.Input; |
@@ -312,38 +316,109 @@ public static class ConfigurationAction implements Action<ByteBuddyLocalClassesE |
312 | 316 | /** |
313 | 317 | * The current variant's Byte Buddy configuration. |
314 | 318 | */ |
315 | | - private final FileCollection byteBuddyConfiguration; |
| 319 | + private final FileCollection byteBuddyClassPath; |
316 | 320 |
|
317 | 321 | /** |
318 | | - * The Android gradle extension. |
| 322 | + * The Android Gradle extension. |
319 | 323 | */ |
320 | | - private final BaseExtension androidExtension; |
| 324 | + private final ApplicationExtension applicationExtension; |
| 325 | + |
| 326 | + /** |
| 327 | + * The Android components extension. |
| 328 | + */ |
| 329 | + private final AndroidComponentsExtension<?, ?, ?> androidComponentsExtension; |
321 | 330 |
|
322 | 331 | /** |
323 | 332 | * The Byte Buddy task extension. |
324 | 333 | */ |
325 | 334 | private final ByteBuddyAndroidTaskExtension byteBuddyExtension; |
326 | 335 |
|
327 | 336 | /** |
328 | | - * @param byteBuddyConfiguration The current variant Byte Buddy configuration. |
329 | | - * @param androidExtension The Android gradle extension. |
330 | | - * @param byteBuddyExtension The Byte Buddy task extension. |
| 337 | + * @param byteBuddyClassPath The current variant Byte Buddy configuration. |
| 338 | + * @param applicationExtension The Android Gradle extension. |
| 339 | + * @param androidComponentsExtension The Android components extension. |
| 340 | + * @param byteBuddyExtension The Byte Buddy task extension. |
331 | 341 | */ |
332 | | - public ConfigurationAction(FileCollection byteBuddyConfiguration, |
333 | | - BaseExtension androidExtension, |
334 | | - ByteBuddyAndroidTaskExtension byteBuddyExtension) { |
335 | | - this.byteBuddyConfiguration = byteBuddyConfiguration; |
336 | | - this.androidExtension = androidExtension; |
| 342 | + protected ConfigurationAction(FileCollection byteBuddyClassPath, |
| 343 | + ApplicationExtension applicationExtension, |
| 344 | + AndroidComponentsExtension<?, ?, ?> androidComponentsExtension, |
| 345 | + ByteBuddyAndroidTaskExtension byteBuddyExtension) { |
| 346 | + this.byteBuddyClassPath = byteBuddyClassPath; |
| 347 | + this.applicationExtension = applicationExtension; |
| 348 | + this.androidComponentsExtension = androidComponentsExtension; |
337 | 349 | this.byteBuddyExtension = byteBuddyExtension; |
338 | 350 | } |
339 | 351 |
|
| 352 | + /** |
| 353 | + * Resolves an appropriate configuration action for the current Android platform. |
| 354 | + * @param byteBuddyClassPath The current variant Byte Buddy configuration. |
| 355 | + * @param container The extensions container to use. |
| 356 | + * @return An appropriate configuration action. |
| 357 | + */ |
| 358 | + @SuppressWarnings("unchecked") |
| 359 | + public static Action<ByteBuddyLocalClassesEnhancerTask> of(FileCollection byteBuddyClassPath, ExtensionContainer container) { |
| 360 | + try { |
| 361 | + return new ConfigurationAction(byteBuddyClassPath, |
| 362 | + container.getByType(ApplicationExtension.class), |
| 363 | + container.getByType(AndroidComponentsExtension.class), |
| 364 | + container.getByType(ByteBuddyAndroidTaskExtension.class)); |
| 365 | + } catch (UnknownDomainObjectException ignored) { |
| 366 | + return new ForLegacyAndroid(byteBuddyClassPath, |
| 367 | + container.getByType(BaseExtension.class), |
| 368 | + container.getByType(ByteBuddyAndroidTaskExtension.class)); |
| 369 | + } |
| 370 | + } |
| 371 | + |
340 | 372 | @Override |
341 | 373 | public void execute(ByteBuddyLocalClassesEnhancerTask task) { |
342 | | - task.getByteBuddyClasspath().from(byteBuddyConfiguration); |
343 | | - task.getAndroidBootClasspath().from(androidExtension.getBootClasspath()); |
344 | | - task.getJavaTargetCompatibilityVersion().set(androidExtension.getCompileOptions().getTargetCompatibility()); |
| 374 | + task.getByteBuddyClasspath().from(byteBuddyClassPath); |
| 375 | + task.getAndroidBootClasspath().from(androidComponentsExtension.getSdkComponents().getBootClasspath()); |
| 376 | + task.getJavaTargetCompatibilityVersion().set(applicationExtension.getCompileOptions().getTargetCompatibility()); |
345 | 377 | byteBuddyExtension.configure(task); |
346 | 378 | } |
| 379 | + |
| 380 | + /** |
| 381 | + * A configuration action for the {@link ByteBuddyLocalClassesEnhancerTask} task for |
| 382 | + * legacy Android platforms. |
| 383 | + */ |
| 384 | + protected static class ForLegacyAndroid implements Action<ByteBuddyLocalClassesEnhancerTask> { |
| 385 | + |
| 386 | + /** |
| 387 | + * The current variant's Byte Buddy configuration. |
| 388 | + */ |
| 389 | + private final FileCollection byteBuddyClassPath; |
| 390 | + |
| 391 | + /** |
| 392 | + * The Android Gradle extension. |
| 393 | + */ |
| 394 | + private final BaseExtension baseExtension; |
| 395 | + |
| 396 | + /** |
| 397 | + * The Byte Buddy task extension. |
| 398 | + */ |
| 399 | + private final ByteBuddyAndroidTaskExtension byteBuddyExtension; |
| 400 | + |
| 401 | + /** |
| 402 | + * @param byteBuddyClassPath The current variant Byte Buddy configuration. |
| 403 | + * @param baseExtension The Android Gradle extension. |
| 404 | + * @param byteBuddyExtension The Byte Buddy task extension. |
| 405 | + */ |
| 406 | + protected ForLegacyAndroid(FileCollection byteBuddyClassPath, |
| 407 | + BaseExtension baseExtension, |
| 408 | + ByteBuddyAndroidTaskExtension byteBuddyExtension) { |
| 409 | + this.byteBuddyClassPath = byteBuddyClassPath; |
| 410 | + this.baseExtension = baseExtension; |
| 411 | + this.byteBuddyExtension = byteBuddyExtension; |
| 412 | + } |
| 413 | + |
| 414 | + @Override |
| 415 | + public void execute(ByteBuddyLocalClassesEnhancerTask task) { |
| 416 | + task.getByteBuddyClasspath().from(byteBuddyClassPath); |
| 417 | + task.getAndroidBootClasspath().from(baseExtension.getBootClasspath()); |
| 418 | + task.getJavaTargetCompatibilityVersion().set(baseExtension.getCompileOptions().getTargetCompatibility()); |
| 419 | + byteBuddyExtension.configure(task); |
| 420 | + } |
| 421 | + } |
347 | 422 | } |
348 | 423 |
|
349 | 424 | /** |
|
0 commit comments