反应本机 Android 构建错误 MainActivity.java: 29: Error: can not find sign

当我尝试编译我的反应原生机器人应用程序时,我得到了这个错误。 Android 应用程序无法解析 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

我可以通过简单地设置为真来暂时解决这个问题。 这可能发生在我更改了应用程序名称之后(因为这也是我一直在尝试做的事情)。

我可能不得不改变的东西,以便它再次生成正确的类型的文件。

/**
* 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";
}
96423 次浏览

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.

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

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.

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

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.

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", )

Upgrading react-native solved this error -

react-native upgrade

Now build again -

react-native run-android

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

import com.facebook.react.BuildConfig;

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

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.

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.

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

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

Seems like you have updated your package name in all files but failed to change the name of folder which we will same as your package name.

android < src < main < java < com < "middlen_package_name" < "last_package_name".

ex: com.yourapp.gameapp;

Run the app again but if it still shows this error then ,

check if there is a folder with old package name. Delete this and rebuild again.

Happy Coding.