0

我试图安装expo-av到一个非博览会反应原生项目中。安装react-native-unimodules和调整android/app/build.gradle文件后,我无法再构建 android 应用程序。

这是我得到的主要错误:

终端如下所示:Java 或 Kotlin 文件MainApplication.java不包含包声明。

ENVFILE=.env react-native run-android

info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 2152 file(s) to forward-jetify. Using 16 workers...
info JS server already running.
info Installing the app...
Configuration on demand is an incubating feature.

> Configure project :app
Reading env from: .env

FAILURE: Build failed with an exception.

* Where:
Script '/Users/*REDACTED*/HF-Projects/*REDACTED*/node_modules/react-native-unimodules/gradle.groovy' line: 75

* What went wrong:
A problem occurred evaluating project ':app'.
> Java or Kotlin file /Users/*REDACTED*/HF-Projects/*REDACTED*/android/app/src/main/java/com/*REDACTED*/MainApplication.java does not include package declaration

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s

我在该文件上进行了搜索package,发现了 26 次。

这个实例上有这个packages变量。ReactNativeHost

private final ReactNativeHost mReactNativeHost =
      new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
          // Add unimodules
          List<ReactPackage> unimodules = Arrays.<ReactPackage>asList(
            new ModuleRegistryAdapter(mModuleRegistryProvider)
          );
          packages.addAll(unimodules);
          return packages;
        }

        @Override
        protected String getJSMainModuleName() {
          return "index";
        }

          @Override
          protected JSIModulePackage getJSIModulePackage() {
              return new ReanimatedJSIModulePackage(); // <- add
          }

      };

有人知道这个错误吗?

4

1 回答 1

1

简短的回答是:

这是一个简单的验证,用于查找该类的主要package内容。java在文件的顶部,应该有一个package com.yourcompany.example.

碰巧我的MainApplication.java班级在包裹之后有两个空格。

所以正则表达式没有找到它。

此验证中使用的正则表达式可在文件node_modules/react-native-unimodules/gradle.groovy第 67 行找到。

使用的正则表达式:/^package ([0-9a-zA-Z._]*);?$/

def readPackageFromJavaOrKotlinFile(String filePath) {
  def file = new File(filePath)
  def fileReader = new BufferedReader(new FileReader(file))
  def fileContent = ""
  while ((fileContent = fileReader.readLine()) != null) {
    def match = fileContent =~ /^package ([0-9a-zA-Z._]*);?$/
    if (match.size() == 1 && match[0].size() == 2) {
      fileReader.close()
      return match[0][1]
    }
  }
  fileReader.close()

  throw new GradleException("Java or Kotlin file $file does not include package declaration")
}
于 2021-09-14T22:24:06.443 回答