facebook / react-native

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

Cannot resolve symbol 'ReactNativeFlipper' - When building release build variant #36060

Closed lorsk closed 1 year ago

lorsk commented 1 year ago

New Version

0.71.1

Old Version

0.70.6

Build Target(s)

Android - Release Build Variant

Output of react-native info

✗ react-native info
warn Package rn-fetch-blob contains invalid configuration: "dependency.hooks" is not allowed. Please verify it's properly linked using "react-native config" command and contact the package maintainers about this.
info Fetching system and libraries information...
System:
    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 40.48 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 19.3.0 - ~/.nvm/versions/node/v19.3.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v19.3.0/bin/yarn
    npm: 9.2.0 - ~/.nvm/versions/node/v19.3.0/bin/npm
    Watchman: 2023.01.23.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1
    Android SDK: Not Found
  IDEs:
    Android Studio: 2021.3 AI-213.7172.25.2113.9123335
    Xcode: 14.2/14C18 - /usr/bin/xcodebuild
  Languages:
    Java: 19.0.1 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: ^10.0.0 => 10.1.3 
    react: 18.2.0 => 18.2.0 
    react-native: 0.71.1 => 0.71.1 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Issue and Reproduction Steps

When I change Build Variant to Release, it builds successfully but this error comes up when clicking Run 'app'. And hovering over 'ReactNativeFlipper' shows Cannot resolve symbol 'ReactNativeFlipper'.

.../MainApplication.java:61: error: cannot find symbol
        ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
        ^
symbol:   variable ReactNativeFlipper
location: class MainApplication

Everything works fine on debug

Hermanyo commented 1 year ago

In my case when I need to building release app, I just make a copy ReactNativeFlipper.java to android/app/src/main/java/com/my_project/ReactNativeFlipper.java then I can building it.

ReactNativeFlipper.java content:

package com.myProject;
import android.content.Context;
import com.facebook.react.ReactInstanceManager;
public class ReactNativeFlipper {
  public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) {
    // Do nothing as we don't want to initialize Flipper on Release.
  }
}
lorsk commented 1 year ago

That works @Hermanyo

I want to mentioned, also tried creating a react native 0.71.0 project using npx react-native init my_project and moved my code there and it seems to have worked too.

I used this helper when updating manually, and double checked I didn't miss anything but without that fix you mentioned it fails, very weird: https://react-native-community.github.io/upgrade-helper/?from=0.70.6&to=0.71.1

lorsk commented 1 year ago

After some investigating, I realized that I was not adding a release folder that contained that ReactNativeFlipper.java code.

I was missing this file in src/release/com/packagename/ReactNativeFlipper.java

https://raw.githubusercontent.com/react-native-community/rn-diff-purge/release/0.71.1/RnDiffApp/android/app/src/release/java/com/rndiffapp/ReactNativeFlipper.java

Closing the issue now.

nicolascavallin commented 1 year ago

In my case when I need to building release app, I just make a copy ReactNativeFlipper.java to android/app/src/main/java/com/my_project/ReactNativeFlipper.java then I can building it.

ReactNativeFlipper.java content:

package com.myProject;
import android.content.Context;
import com.facebook.react.ReactInstanceManager;
public class ReactNativeFlipper {
  public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) {
    // Do nothing as we don't want to initialize Flipper on Release.
  }
}

This file solves the assembleRelease but crashes when I try to run react-native run-android because is a duplicated class.

Any solution at this point? I can't find it.

Hermanyo commented 1 year ago

Just comment this code when you only want to run on emulator and undo when you want to build in release

cortinico commented 1 year ago

In my case when I need to building release app, I just make a copy ReactNativeFlipper.java to android/app/src/main/java/com/my_project/ReactNativeFlipper.java then I can building it.

This is not the solution and will result in deplicated class + crash at developer time. You should have only two ReactNativeFlipper.java files:

android/app/src/debug/java/com/helloworld/ReactNativeFlipper.java
android/app/src/release/java/com/helloworld/ReactNativeFlipper.java

You can copy the content from the template. If the issue persists open a separate issue.

VoHoangThien commented 1 year ago

In my case when I need to building release app, I just make a copy ReactNativeFlipper.java to android/app/src/main/java/com/my_project/ReactNativeFlipper.java then I can building it.

ReactNativeFlipper.java content:

package com.myProject;
import android.content.Context;
import com.facebook.react.ReactInstanceManager;
public class ReactNativeFlipper {
  public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) {
    // Do nothing as we don't want to initialize Flipper on Release.
  }
}

thanks work for me.

danielbrubeck commented 1 year ago

I think the appropriate location is in android/app/src/release/java/com/my_project/ReactNativeFlipper.java that way it is only appended in release builds and not in debug mode.

LuongTruong commented 1 year ago

Hi guys, I have the same issue and this is the way I fix it.

People who create a project using npx react-native init this_is_package_name.

  1. If you guys haven't changed the package name, please feel free to try the solution from Hermanyo comment
  2. If you guys changed the package name, please pay attention at the top import of ReactNativeFlipper.java in release folder
import this_is_NEW_package_name; // <= please change this import

Hope my comment can help people that have the same issue as me

PunainenAurinko commented 1 year ago

I had the same issue and @LuongTruong comment helped me solve it, so even if this issue is closed, I'm going to write this down in case someone else has this issue.

I had everything set up correctly between main, debug and release folders. My issue was that my app has flavors, and app package ids end with .dev, .qa and .prod. I was not changing app package id inReactNativeFlipper.java file correctly for .qa and .prod release builds, so the release build did not now what ReactNativeFlipper class was as it had a different package id. When I added modifying package id for that file in my build scripts, everything started working again.

luicfrr commented 1 year ago

I had the same issue and @LuongTruong comment helped me solve it, so even if this issue is closed, I'm going to write this down in case someone else has this issue.

I had everything set up correctly between main, debug and release folders. My issue was that my app has flavors, and app package ids end with .dev, .qa and .prod. I was not changing app package id inReactNativeFlipper.java file correctly for .qa and .prod release builds, so the release build did not now what ReactNativeFlipper class was as it had a different package id. When I added modifying package id for that file in my build scripts, everything started working again.

can you please provide your script? I'm facing same issue with wrong package name inside ReactNativeFlipper.java

Jonathanfd commented 1 year ago

For me the solution was to replace ReactNativeFlipper.java file on path: '/android/app/src/debug/java/com/terapify/ReactNativeFlipper.java' with the ReactNativeFlipper.java file we have in this path: android/app/src/release/java/com/terapify/ReactNativeFlipper.java

This file has this content:

/**

I'm using RN "0.71.8".

Hope it helps

fukemy commented 1 year ago

any solution without create copy?

TarifEzaz commented 1 year ago

@Jonathanfd : I did not have to copy the version of android/app/src/release/java/com/terapify/ReactNativeFlipper.java to '/android/app/src/debug/java/com/terapify/ReactNativeFlipper.java'. After a lot of juggling, I just removed the ReactNativeFlipper.java from the "main" path. There are some suggestions to include the file in the "main" directory path, but I recommend against it as it starts throwing duplicate class errors.

gh3s commented 1 year ago

In my case I've used the react-native-rename package to rename my project but, I don't know why, both release and debug folders (src/debug/java/com/... and src/release/java/com...) have not been renamed, including the entries in the respective ReactNativeFlipper.java files.

thijs-qv commented 11 months ago

My issue was that I renamed my package, but did not update the package structure in the release directory (note that there are debug, main AND release directories).

CoderSufiyan commented 5 months ago

My issue was that I renamed my package, but did not update the package structure in the release directory (note that there are debug, main AND release directories).

This worked for me. Thank you.