React Native Android Build Error MainActivity.java:29: error: cannot find symbol

I'm getting this error when trying to compile my React Native android app. The Android app can't resolve BuildConfig.DEBUG.

:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac
/Users/amirsharif/mobile-rappad/android/app/src/main/java/com/rappadmobile/MainActivity.java:29: error: cannot find symbol
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                                        ^
  symbol:   variable BuildConfig
  location: class MainActivity
>1 error
:app:compileDebugJavaWithJavac FAILED

I can temporarily resolve it by simply setting it to true. This might've happened after I changed an application name (since that's something I've also been trying to do).

I probably have to change something with Gradle so it generates the right kind of files again.

/**
 * Automatically generated file. DO NOT MODIFY
 */
package com.app;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.rappadmobile";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
}

Solution 1:

I had the same issue and it was resolved by simply adding following import statement in MainApplication.java:

import com.facebook.react.BuildConfig;

Solution 2:

The way android knows where to find certain files, and how to connect certain files, is by using fields set in AndroidManifest.xml. Since the default setup of a React Native project, references everything with .(name-of-resource), this means that everything will be resolved with regarding to the package name set in the <manifest> tag. So for everything to work out of the box, and everything to be generated as expected, the path to MainActivity.java, should be the same as the package name.

example:

your apps package name: com.mycompanyname.myappname

location of MainActivity.java: android/app/src/main/java/com/mycompanyname/myappname/MainActivity.java

Solution 3:

I rebuilt the project with react-native upgrade. My issue was then that I had old files that were referencing the old package names (because I changed the name of the app in package.json). Once deleting those, I resolved the issue.

Solution 4:

In your MainActivity.java, you can check the first line is package com.YOU_APP_NAME;

If this line does not exist, you should add this.