Java or Kotlin file `MainApplication.java` does not include package declaration

1.4k views Asked by At

I was trying to install expo-av into a non-expo react-native project. After installing react-native-unimodules and adjusting the android/app/build.gradle file, I am not able to build the android app anymore.

This is the main error I get:

The terminal looks like this: Java or Kotlin file MainApplication.java does not include package declaration.

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

I searched for package on that file and found 26 occurrences.

There is this packages variable on this ReactNativeHost instance.

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
          }

      };

Does anybody know about this error?

1

There are 1 answers

1
ofundefined On

The short answer is:

This is a simple validation that looks for the main package of that java class. On top of the file, there should be a package com.yourcompany.example.

It happened that my MainApplication.java class had TWO BLANK SPACES right after packages.

So the regex didn't find it.

The regex used in this validation may be found on the file node_modules/react-native-unimodules/gradle.groovy line 67.

Regex used: /^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")
}