facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.12k stars 24.32k forks source link

Build error "Could not get unknown property 'preDebugBuild' for extension 'android'" #33596

Closed rvasseur31 closed 2 years ago

rvasseur31 commented 2 years ago

New Version

0.68.0

Old Version

0.67.3

Build Target(s)

android

Output of react-native info

System:
    OS: macOS 12.2.1
    CPU: (8) arm64 Apple M1
    Memory: 775.20 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.10.0 - /opt/homebrew/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 7.24.0 - /opt/homebrew/bin/npm
    Watchman: 2021.09.27.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.11.2 - /opt/homebrew/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.4, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK:
      API Levels: 23, 29, 30, 31
      Build Tools: 30.0.2, 30.0.3, 31.0.0
      System Images: android-31 | Google APIs ARM 64 v8a
      Android NDK: 21.4.7075529
  IDEs:
    Android Studio: 2021.1 AI-211.7628.21.2111.8309675
    Xcode: 13.3/13E113 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.12 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: Not Found
    react-native: Not Found
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Issue and Reproduction Steps

After the update, when I run yarn android with newArchitecture enabled, I got the following error :

FAILURE: Build failed with an exception.

Suzuhana commented 2 years ago

Getting same error after setting newArchEnabled=true. upgraded to 0.68.0 with help of the Upgrade Helper.

SergioAN007 commented 2 years ago

System OS: Windows 11 RN: 0.68.0

Getting same error newArchEnabled=true. It looks like it happens during build using gradle productFlavors

LydGol90 commented 2 years ago

System OS: Windows 11 RN: 0.68.0

Getting same error newArchEnabled=true. It looks like it happens during build using gradle productFlavors

@SergioAN007 Are you able to get the build working by removing productFlavors? Thanks!

cortinico commented 2 years ago

Let's isolate the upgrade 0.67 -> 068 and the newArchEnabled=true. Are you able to build correctly after the update to 0.68 with newArchEnabled=false?

firehand33 commented 2 years ago

I'm getting the same error too after upgrading from 0.67 -> 0.68 when newArchEnabled=true. But i was able to build it when newArchEnabled=false.

SergioAN007 commented 2 years ago

Let's isolate the upgrade 0.67 -> 068 and the newArchEnabled=true. Are you able to build correctly after the update to 0.68 with newArchEnabled=false?

newArchEnabled=false. --> build normal using RN68 newArchEnabled=true. --> Could not get unknown property 'preDebugBuild' for extension 'android

This error passed when build flower main and edit app\gradle: preMainDebugBuild.dependsOn(packageReactNdkDebugLibs) preMainReleaseBuild.dependsOn(packageReactNdkReleaseLibs)

But i have many errors later ecxeption link: https://shorturl.at/fikH3

jenshandersson commented 2 years ago

Let's isolate the upgrade 0.67 -> 068 and the newArchEnabled=true. Are you able to build correctly after the update to 0.68 with newArchEnabled=false?

Correct. Works fine with fabric disabled. But we're getting the above error when enabling. We use product flavours, if that's to any help. (Works fine on iOS when enabling new arch.

jenshandersson commented 2 years ago

Looks like it's caused by product flavours. We have "staging" and "production", changing preDebugBuild to preStagingDebugBuild seems to work, ie inserting your flavour.

oblador commented 2 years ago

Can someone here validate that my fix solves your issues too?

afterEvaluate {
    // If you wish to add a custom TurboModule or component locally,
    // you should uncomment this line.
    // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
    android.applicationVariants.all { def variant ->
        def variantName = variant.name.capitalize()
        def preVariantBuildTask = tasks.findByName("pre${variantName}Build")

        // Due to a bug inside AGP, we have to explicitly set a dependency
        // between configureNdkBuild* tasks and the preBuild tasks.
        // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
        if (variantName.endsWith("Debug")) {
            preVariantBuildTask.dependsOn(packageReactNdkDebugLibs)
            configureNdkBuildDebug.dependsOn(preVariantBuildTask)
            reactNativeArchitectures().each { architecture ->
                tasks.findByName("configureNdkBuildDebug[${architecture}]")?.configure {
                    dependsOn(preVariantBuildTask.name)
                }
            }
        }
        if (variantName.endsWith("Release")) {
            preVariantBuildTask.dependsOn(packageReactNdkReleaseLibs)
            configureNdkBuildRelease.dependsOn(preVariantBuildTask)
            reactNativeArchitectures().each { architecture ->
                tasks.findByName("configureNdkBuildRelease[${architecture}]")?.configure {
                    dependsOn(preVariantBuildTask.name)
                }
            }
        }
    }
}
rvasseur31 commented 2 years ago

I confirm that this fixes the bug !

Abhishek2250 commented 2 years ago

@oblador I'm getting below issue: Could not get unknown property 'configureNdkBuildDebug' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl.

NDK version: 24.0.8215888 System: Apple M1 pro reactNativeArchitectures=x86_64 -> in gradle.properties.

1awaleed commented 2 years ago

i am having the same issue & I tried @oblador fix but i got another error from it.

* What went wrong:
Could not determine the dependencies of task ':ReactAndroid:packageReactNdkDebugLibsForBuck'.
> Task with path 'mergeDebugNativeLibs' not found in project ':ReactAndroid'.

So I am grateful for any suggestions here.

Yandamuri commented 2 years ago

@oblador I'm getting below issue: Could not get unknown property 'configureNdkBuildDebug' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl.

NDK version: 24.0.8215888 System: Apple M1 pro reactNativeArchitectures=x86_64 -> in gradle.properties.

@oblador I am also facing same issue after applying your solution

Gu1K commented 2 years ago

i am having the same issue & I tried @oblador fix but i got another error from it.

* What went wrong:
Could not determine the dependencies of task ':ReactAndroid:packageReactNdkDebugLibsForBuck'.
> Task with path 'mergeDebugNativeLibs' not found in project ':ReactAndroid'.

So I am grateful for any suggestions here.

if you have a line ndk.dir in your local.properties, try to comment this line

BritCoder96 commented 2 years ago

@oblador I'm getting below issue: Could not get unknown property 'configureNdkBuildDebug' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl. NDK version: 24.0.8215888 System: Apple M1 pro reactNativeArchitectures=x86_64 -> in gradle.properties.

@oblador I am also facing same issue after applying your solution

I'm also getting the same error, any progress on this?

freeboub commented 2 years ago

It would make sens to put @oblador patch in rnfdiff app ! but thank you !

DOMEYD commented 2 years ago

@oblador I'm getting below issue: Could not get unknown property 'configureNdkBuildDebug' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl. NDK version: 24.0.8215888 System: Apple M1 pro reactNativeArchitectures=x86_64 -> in gradle.properties.

@oblador I am also facing same issue after applying your solution

I'm also getting the same error, any progress on this?

I'm also facing this issue, do not forgot to change

android {
  defaultConfig {
   externalNativeBuild {
      ndkBuild {
          arguments "APP_PLATFORM=android-21",
              "APP_STL=c++_shared",
              "NDK_TOOLCHAIN_VERSION=clang",
              "GENERATED_SRC_DIR=$buildDir/generated/source",
              "PROJECT_BUILD_DIR=$buildDir",
              "REACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
              "REACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build"
          cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
          cppFlags "-std=c++17"
          // Make sure this target name is the same you specify inside the
          // src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
          targets "sample_appmodules"
      }
    }
  }
}

and

// We configure the NDK build only if you decide to opt-in for the New Architecture.
externalNativeBuild {
    ndkBuild {
        path "$projectDir/src/main/jni/Android.mk"
    }
}
geroale commented 2 years ago

Same issue here

fondue-tech commented 2 years ago

I also face the same issue after upgrading from 0.68.0 to 0.69.0, I followed manual upgrade instructions. Seems that everything that had to be chaged, I did change. I tried to use answer provided by @oblador but this did not solve the issue.

RodrigoSarmento commented 2 years ago

reactNativeArchitectures

It worked for me for the debug, though it still does not work for my release flavors, I have multiple releases for each flavor.

Anyone with the same problem could make it work?

christopherklint97 commented 2 years ago

@oblador I'm getting below issue: Could not get unknown property 'configureNdkBuildDebug' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl.

NDK version: 24.0.8215888 System: Apple M1 pro reactNativeArchitectures=x86_64 -> in gradle.properties.

I had the same issue and this fixed it for me. It seems like https://issuetracker.google.com/issues/207403732 is solved on AGP 7.3.0:

afterEvaluate {
        // If you wish to add a custom TurboModule or component locally,
        // you should uncomment this line.
        // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
        android.applicationVariants.all { def variant ->
                def variantName = variant.name.capitalize()
                def preVariantBuildTask = tasks.findByName("pre${variantName}Build")
        }
}
RanvijayChouhan12 commented 1 year ago

i tried all the Solutions Here but Nothing Worked for me i still get Same Error

"react": "18.1.0", "react-native": "0.70.6",

Error :

Could not get unknown property 'preDebugBuild' for extension 'android' of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.

Please Help

franzapiola commented 1 year ago

I faced this same issue. I'm configuring different product flavors on react-native 0.70.6 and new arch enabled. Macbook Pro M1, MacOS Monterey 12.3.1.

I tried @oblador 's afterEvaluate fix on android/app/build.gradle, and I got the same build error as others:

Could not get unknown property 'configureNdkBuildDebug' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl.

After reading @christopherklint97 's comment I tried updating AGP to newer versions, but I couldn't get it to work. I came to the conclusion that surely this isn't an option (at least in my case). Still using AGP 7.2.1.

I managed to get product flavors working on RN 0.70.6 by making some modifications to oblador's code. Here's the modified fix in case it helps someone else:

        afterEvaluate {
            // If you wish to add a custom TurboModule or component locally,
            // you should uncomment this line.
            // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
            android.applicationVariants.all { def variant ->
                def variantName = variant.name.capitalize()
                def preVariantBuildTask = tasks.findByName("pre${variantName}Build")

                // Due to a bug inside AGP, we have to explicitly set a dependency
                // between configureCMakeDebug* tasks and the preBuild tasks.
                // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
                if (variantName.endsWith("Debug")) {
                    preVariantBuildTask.dependsOn(packageReactNdkDebugLibs)
                    configureCMakeDebug.dependsOn(preVariantBuildTask)

                    reactNativeArchitectures().each { architecture ->
                        tasks.findByName("configureCMakeDebug[${architecture}]")?.configure {
                            dependsOn(preVariantBuildTask.name)
                        }
                    }
                }
                if (variantName.endsWith("Release")) {
                    preVariantBuildTask.dependsOn(packageReactNdkReleaseLibs)
                    configureCMakeRelWithDebInfo.dependsOn(preVariantBuildTask)
                    reactNativeArchitectures().each { architecture ->
                        tasks.findByName("configureCMakeRelWithDebInfo[${architecture}]")?.configure {
                            dependsOn(preVariantBuildTask.name)
                        }
                    }
                }
            }
        }

Not sure if this solution is entirely correct. It's working for me though. It's similar to @oblador 's fix, but switching some NDK lines with their CMake counterparts, taking into account that RN 0.70 introduced support for CMake (so it makes some sense that this would work). I'd love to hear if someone sees a problem with this solution.

cortinico commented 1 year ago

Just a heads up that this has been fully fixed in RN 0.71. If you update version everything should work without extra scripts or patches needed.