From f960f13cd1cf4d47804863722ae3edb6da2d5e31 Mon Sep 17 00:00:00 2001 From: James Treanor Date: Thu, 6 Dec 2018 20:30:24 -0800 Subject: [PATCH] Fix build error for Android projects that use `apply plugin: "com.android.library"` (#22312) Summary: This PR allows Android projects that use `apply plugin: "com.android.library"` to build successfully. A recent regression caused by https://github.com/facebook/react-native/pull/20526 means that building one of these projects always fails with this error: ``` FAILURE: Build failed with an exception. * Where: Script '/Users/james/src/SampleLibrary/node_modules/react-native/react.gradle' line: 15 * What went wrong: A problem occurred configuring project ':app'. > Could not get unknown property 'applicationVariants' for object of type com.android.build.gradle.LibraryExtension. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 0s ``` This change updates `react.gradle` to cater to both application and library projects by selectively using `android.applicationVariants` or `android.libraryVariants`. Fixes https://github.com/facebook/react-native/issues/22310. Pull Request resolved: https://github.com/facebook/react-native/pull/22312 Differential Revision: D13373742 Pulled By: cpojer fbshipit-source-id: 64c35ab7a6d5d0d840a43729123e70dd8e0d36e0 --- react.gradle | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/react.gradle b/react.gradle index 3f41b1bc5a278f..74fd2306adb3f5 100644 --- a/react.gradle +++ b/react.gradle @@ -12,7 +12,9 @@ def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : afterEvaluate { - android.applicationVariants.all { def variant -> + def isAndroidLibrary = plugins.hasPlugin("com.android.library") + def variants = isAndroidLibrary ? android.libraryVariants : android.applicationVariants + variants.all { def variant -> // Create variant and target names def targetName = variant.name.capitalize() def targetPath = variant.dirName @@ -91,6 +93,9 @@ afterEvaluate { def packageTask = variant.hasProperty("packageApplication") ? variant.packageApplication : tasks.findByName("package${targetName}") + if (variant.hasProperty("packageLibrary")) { + packageTask = variant.packageLibrary + } def resourcesDirConfigValue = config."resourcesDir${targetName}" if (resourcesDirConfigValue) {