【问题标题】:Java or Kotlin file `MainApplication.java` does not include package declarationJava 或 Kotlin 文件“MainApplication.java”不包含包声明
【发布时间】:2021-09-14 22:24:06
【问题描述】:

我试图将 expo-av 安装到一个非博览会 react-native 项目中。安装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 次。

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

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
          }

      };

有人知道这个错误吗?

【问题讨论】:

    标签: android react-native expo expo-av react-native-unimodules


    【解决方案1】:

    简短的回答是:

    这是一个简单的验证,用于查找该 java 类的主要 package。 在文件的顶部,应该有一个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")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      • 2012-11-05
      • 1970-01-01
      • 2020-07-29
      相关资源
      最近更新 更多