Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Android build.gradle doesn't work with flavors #2564

Merged
merged 2 commits into from
Oct 28, 2021

Conversation

jtoce
Copy link
Contributor

@jtoce jtoce commented Oct 21, 2021

Fixes #2412

Description

Fixed a build problem for Android users who have build flavors/variants:

FAILURE: Build failed with an exception.
* Where:
Build file '/Users/vagrant/git/node_modules/react-native-reanimated/android/build.gradle' line: 70
* What went wrong:
A problem occurred evaluating project ':react-native-reanimated'.
> Task with path ':app:mergeDebugNativeLibs' not found in project ':app'.

Changes

Inside the function for getCurrentFlavor() the regex only matches against certain tasks:

Pattern pattern = Pattern.compile("(assemble|install|generate)(\\w+)(Release|Debug)")

Added bundle to this to address the comment from @wbercx.

Note that if this match fails, the current flavor is returned as empty string. However, if you have no flavors, this also returns an empty string. This presents a problem below in the logic because it can't determine between the two. Modified this to instead return NOT-FOUND which is not a valid Android product flavor.

Since there are valid tasks (e.g. clean) that don't contain the current flavor, we don't want to execute the merge and strip tasks. So modified the if statement to ensure we only execute if there is a match. Again this is only a problem if you have flavors, since otherwise it can't the difference between an empty string match and non-match.

final NOTFOUND = "NOT-FOUND"
if(!NOTFOUND.equals(getCurrentFlavor()) && (!projectProperties.get("reanimated")
        || (projectProperties.get("reanimated") && projectProperties.get("reanimated").get("enablePackagingOptions")))
) {

Test code and steps to reproduce

See https://developer.android.com/studio/build/build-variants

Repro Steps:

  1. Create Android build variants in app/build.gradle (e.g. demo, full)
  2. Run ./gradlew clean
    (Alternatively, go to Android Studio > File > Sync Project with Gradle Files)

Checklist

  • Included code example that can be used to test this change
  • Updated TS types
  • Added TS types tests
  • Added unit / integration tests
  • Updated documentation
  • Ensured that CI passes

@piaskowyk piaskowyk self-assigned this Oct 22, 2021
@piaskowyk piaskowyk self-requested a review October 22, 2021 09:17
Copy link
Member

@piaskowyk piaskowyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for PR and good explanation! Of course, your idea is reasonable, but with the current form of regex, it doesn't work properly because you return NOT-FOUND also if someone doesn't have any flavor name. It is no difference between ranromTaskName and assembleDebug, in both ways it will be returned NOT-FOUND. You need to change the regex

(assemble|bundle|install|generate)(\w+)(Release|Debug)

image

to

(assemble|bundle|install|generate)(\w*)(Release|Debug)

image

@piaskowyk
Copy link
Member

If you don't mind, I will correct this regex

Copy link
Member

@piaskowyk piaskowyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help! 🚀

@piaskowyk piaskowyk merged commit ca1b0f1 into software-mansion:master Oct 28, 2021
@pke
Copy link

pke commented Oct 29, 2021

Has this been released already? @next doesn't seem to have it

edit: it hasn't yet ;)

@saintpepsicola
Copy link

I still have this issue on 2.2.4 :(

@rawatnaresh
Copy link

Not able to clean the build, is it because of this? I've flavours in my project
Screen Shot 2021-11-02 at 09 07 53

@pke
Copy link

pke commented Nov 2, 2021

Yes it is related. Had this too.

@CuongNguyenLD
Copy link

CuongNguyenLD commented Nov 3, 2021

Fixed on 2.2.4
Using yarn add react-native-reanimated@2.2.4 to install. @next doesn't seem to have it yet.

@ULazdins
Copy link

Hi!

It seems like I'm experiencing an error that might be related to these changes. The error appeared recently and the same command was working before and it seems that react-native-reanimated might have caused that.

./gradlew bundleRelease fails with an error:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/ula/Repos/lolo/node_modules/react-native-reanimated/android/build.gradle' line: 71

* What went wrong:
A problem occurred evaluating project ':react-native-reanimated'.
> Task with path ':app:mergeDebugNativeLibs' not found in project ':app'.

* 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

The error can be circumvented by explicitly using the flavor name in the bundle command, like ./gradlew bundleStageRelease of ./gradlew bundleProdRelease (or whatever your flavor names are).

@sami-99
Copy link

sami-99 commented Dec 8, 2021

Hi!

It seems like I'm experiencing an error that might be related to these changes. The error appeared recently and the same command was working before and it seems that react-native-reanimated might have caused that.

./gradlew bundleRelease fails with an error:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/ula/Repos/lolo/node_modules/react-native-reanimated/android/build.gradle' line: 71

* What went wrong:
A problem occurred evaluating project ':react-native-reanimated'.
> Task with path ':app:mergeDebugNativeLibs' not found in project ':app'.

* 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

The error can be circumvented by explicitly using the flavor name in the bundle command, like ./gradlew bundleStageRelease of ./gradlew bundleProdRelease (or whatever your flavor names are).

same issue for me. I'm using react native 0.64.2 react native reanimated 2.2.4. Tried multiple versions but all got me the same error: A problem occurred evaluating project ':react-native-reanimated'.

Task with path ':app:mergeDebugNativeLibs' not found in project ':app'.

@sergeymorkovkin
Copy link

sergeymorkovkin commented Feb 7, 2022

Confirm: this is still reproducing for anything above 2.2.0 with an official patch. Looks like it's only allowing a small subset of flavor names (those that are ending "Debug" and "Release"), which will completely crash for arbitrary naming system. Fixing regex did the trick, but the question is: should we force anyone who uses reanimated 2.2.0+ follow that naming convention for naming Android build flavours? I believe, no.

Here is the patch I'm using:

diff --git a/node_modules/react-native-reanimated/android/build.gradle b/node_modules/react-native-reanimated/android/build.gradle
index 5bb494f..8141206 100644
--- a/node_modules/react-native-reanimated/android/build.gradle
+++ b/node_modules/react-native-reanimated/android/build.gradle
@@ -10,7 +10,7 @@ def (major, minor, patch) = reactNativeVersion.tokenize('.')
 
 def engine = "jsc"
 
-abstract class replaceSoTask extends DefaultTask {
+abstract class ReplaceSoTask extends DefaultTask {
     public static String appName = ":app"
     public static String buildDir = "../../../android/app/build"
 
@@ -31,23 +31,24 @@ abstract class replaceSoTask extends DefaultTask {
 def getCurrentFlavor() {
     Gradle gradle = getGradle()
     String taskRequestName = gradle.getStartParameter().getTaskRequests().toString()
-    Pattern pattern = Pattern.compile("(assemble|bundle|install|generate)(\\w*)(Release|Debug)")
+    Pattern pattern = Pattern.compile("(assemble|bundle|install|generate)(\\w+)")
     Matcher matcher = pattern.matcher(taskRequestName)
 
     if(matcher.find()) {
+        project.logger.lifecycle('FOUND!')
+        project.logger.lifecycle(matcher.group(2))
         return matcher.group(2)
     }
 
+    project.logger.lifecycle('NOT-FOUND')
+
     return "NOT-FOUND"
 }
 
-def replaceSoTaskDebug
-def replaceSoTaskRelease
+def replaceSoTask
 if(Integer.parseInt(minor) < 65) {
-    tasks.register("replaceSoTaskDebug", replaceSoTask)
-    tasks.register("replaceSoTaskRelease", replaceSoTask)
-    replaceSoTaskDebug = project.getTasks().getByPath(":react-native-reanimated:replaceSoTaskDebug")
-    replaceSoTaskRelease = project.getTasks().getByPath(":react-native-reanimated:replaceSoTaskRelease")
+    tasks.register("replaceSoTask", ReplaceSoTask)
+    replaceSoTask = project.getTasks().getByPath(":react-native-reanimated:replaceSoTask")
 }
 
 rootProject.getSubprojects().forEach({project ->
@@ -67,17 +68,12 @@ rootProject.getSubprojects().forEach({project ->
                 replaceSoTask.buildDir = project.getProperties().buildDir
                 def appName = project.getProperties().path
 
-                replaceSoTaskDebug.dependsOn(
-                        project.getTasks().getByPath("${appName}:merge${flavorString}DebugNativeLibs"),
-                        project.getTasks().getByPath("${appName}:strip${flavorString}DebugDebugSymbols")
+                replaceSoTask.dependsOn(
+                        project.getTasks().getByPath("${appName}:merge${flavorString}NativeLibs"),
+                        project.getTasks().getByPath("${appName}:strip${flavorString}DebugSymbols")
                 )
-                project.getTasks().getByPath("${appName}:package${flavorString}Debug").dependsOn(replaceSoTaskDebug)
+                project.getTasks().getByPath("${appName}:package${flavorString}").dependsOn(replaceSoTask)
 
-                replaceSoTaskRelease.dependsOn(
-                        project.getTasks().getByPath("${appName}:merge${flavorString}ReleaseNativeLibs"),
-                        project.getTasks().getByPath("${appName}:strip${flavorString}ReleaseDebugSymbols")
-                )
-                project.getTasks().getByPath("${appName}:package${flavorString}Release").dependsOn(replaceSoTaskRelease)
             }
         }
     }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build on android fails for version 2.2.1