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

React Native

React Native Problem Overview


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";
}

React Native Solutions


Solution 1 - React Native

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 - React Native

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 - React Native

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 - React Native

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.

Solution 5 - React Native

A combination of some answers helped me, summary:

  • Make sure your java package names are correct (MainApplication.java etc.)
  • update the package name in the gradle build file
  • update the package name in the manifest file
  • update the package name in the BUCK file
  • make sure the path to you javascript sources is a folder structure that matches your package name

Solution 6 - React Native

In my case, I changed the package name in both the BUCK file and Manifest file.

In the BUCK file, change as following.

android_build_config(
    name = "build_config",
    package = "NEW_PACKAGE_NAME",
)

android_resource(
    name = "res",
    package = "NEW_PACKAGE_NAME",
    res = "src/main/res", )

Solution 7 - React Native

If you renamed your bundle identifier you will likely need to change:

Your android/app/src/main/....../MainApplication.java package and imports.

Even though I used react-native-rename npm package, it did not take care of some files MainApplication.java and MainActivity.java.

Solution 8 - React Native

Upgrading react-native solved this error -

react-native upgrade

Now build again -

react-native run-android

Solution 9 - React Native

Delete the old files that might be there in android/app/src/main/java/com Just keep the file with the new name of the app. Then do react-native upgrade.

Solution 10 - React Native

My case was that I renamed the package in one place(AndroidManifest.xml) Ex: The original: com.example Changed it to: com.example.app

Renaming it again solved the problem

Solution 11 - React Native

Check if you have missed installing the react-native-gesture-handler.

with yarn

yarn add react-native-gesture-handler

or with npm

npm install --save react-native-gesture-handler

Then

react-native link react-native-gesture-handler

Solution 12 - React Native

I experienced this while following https://dev.to/karanpratapsingh/quick-guide-for-updating-package-name-in-react-native-3ei3. I was renaming my application from com.company_app to com.mycompany.myapp, apparently, there were some files that I still needed to update but was not included in the guide I was following, I think the guide is only for general, like a fresh react-native app, I solved this by doing a global search for com.company_app and just replacing each one to com.mycompany.myapp whenever it makes sense.

Solution 13 - React Native

I fixed it by following these steps: Android Version: From Terminal go to android folder and run this command: gradlew clean

From main project folder delete this folder: node_modules

From menu file: Invalidate caches and restart IDE

Find your MainActivity.java file and add this import line: import com.facebook.react.BuildConfig;

In terminal, go to android folder and run this: gradlew

in terminal, go to main root and run this: yarn install

Now you can start the metro and run the app.

P.S.: Make sure that into the MainActivity.java file the bundleID name is correct. You will find it into the first row. Then, make sure that the MainActivity.java file is located into the correct path like this: project_name/Android/App/src/main/java/1/2/3/MainActivity.java where 1/2/3 must be the name of your bundleID, for example, com/bundle/id

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOverload119View Question on Stackoverflow
Solution 1 - React NativeManoj GoelView Answer on Stackoverflow
Solution 2 - React NativegorjanzView Answer on Stackoverflow
Solution 3 - React NativeOverload119View Answer on Stackoverflow
Solution 4 - React NativeRom1View Answer on Stackoverflow
Solution 5 - React NativeWiRaView Answer on Stackoverflow
Solution 6 - React NativerakeshNSView Answer on Stackoverflow
Solution 7 - React NativeofundefinedView Answer on Stackoverflow
Solution 8 - React NativeTamandeep SinghView Answer on Stackoverflow
Solution 9 - React NativeAnkur KediaView Answer on Stackoverflow
Solution 10 - React NativeGeekDevView Answer on Stackoverflow
Solution 11 - React NativeNadeeshan HerathView Answer on Stackoverflow
Solution 12 - React NativeaprilmintacpinedaView Answer on Stackoverflow
Solution 13 - React NativeInvestiSocialView Answer on Stackoverflow