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

symbol not found #554

Open
nick-bull opened this issue Mar 3, 2021 · 2 comments
Open

symbol not found #554

nick-bull opened this issue Mar 3, 2021 · 2 comments

Comments

@nick-bull
Copy link

nick-bull commented Mar 3, 2021

I've got a fresh React Native app:

npx react-native init RNConfigDemo
cd RNConfigDemo

yarn add react-native-config
echo "SOME_VAR="something"" > .env

I tried to add a line in my ./android/app/src/main/java/com/rnconfigdemo/MainActivity.java:

...
  return BuildConfig.SOME_VAR;
...

and I always get the following:

RNConfigDemo/android/app/src/main/java/com/rnconfigdemo/MainActivity.java:40: error: cannot find symbol
    return BuildConfig.SOME_VAR;
                                                               ^
  symbol:   variable SOME_VAR
  location: class BuildConfig
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

FAILURE: Build failed with an exception

I've tried running npx react-native link react-native-config, despite this having been autolinked, as well as the troubleshooting guidelines.
I verified the manually linked additions were present in their respective files.
I tried adding import com.facebook.react.BuildConfig, and then import com.rnconfigdemo.BuildConfig, to no avail
I've followed all the steps for the Android build in the package's README, including the extra step

What's the issue here?

@FlacorLopes
Copy link

I solved this issue by manually adding the dependency to build.gradle beacuse autolinking was not doing it.

implementation project(':react-native-config')

And adding this import to MainApplication.java.

import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;

Maybe not the fix for your issue, but could help someone.

@arthurgeron-work
Copy link

arthurgeron-work commented Aug 31, 2023

I've found a solution, although a controversial one, it seems that gradle will not automatically generate BuildConfig symbol if com.lugg.RNCConfig isn't referenced anywhere, and you can't directly reference com.lugg.RNCConfig.BuildConfig from MainActivity since that causes the initial sync to fail, hence the fix is as follows:

Assuming your package name/namespace is "com.your.package" do the following
create the same file under android/app/src/release/java/com/your/package and android/app/src/debug/java/com/your/package, like ReactNativeBuildConfig.java (name can be anything), inside it import and reference the package statically:

package com.your.package;

import com.lugg.RNCConfig.BuildConfig; // Add this import statement at the top

// This class is not meant to be used directly;
// It forces Gradle to resolve BuildConfig symbol with the .env values;
// Without this you'll get "unresolved symbol" error
public class ReactNativeBuildConfig {
  // Create a static variable for storing the BuildConfig from RNCConfig, so Android Studio doesn't ask us to remove the import statement
  public static boolean rncBuildConfig = BuildConfig;

}

After that try running "clean", then gradle sync and try building to test if it resolves correctly.

That ReactNativeBuildConfig class "forces" gradlew to resolve buildconfig during gradle sync, generating a BuildConfig symbol for our namespace. Which means you can now safely reference BuildConfig in MainActivity without adding any new imports.

Here's a screenshot of my project successfully accessing a variable from my .env file:
Captura de Tela 2023-08-31 às 20 12 23

That's what worked for me, I hope it also works for everyone else. Cheers.

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

No branches or pull requests

3 participants