AKang123.
AKang123.

Reputation: 475

Android: error: cannot find symbol packages.add(new MyAppPackage())

Please HELP!!

I'm working on an app in react-native that involves using a native Calendar module. I'm trying to register this module with react native and I keep getting this error. enter image description here

This is how I've registered the module in MainApplication.java:

   @Override
    protected List<ReactPackage> getPackages() {
      List<ReactPackage> packages = new PackageList(this).getPackages();
      packages.add(new ModuleRegistryAdapter(mModuleRegistryProvider));
      packages.add(new MyAppPackage());
      return packages;
    }

This is the MyAppPackage.java file I've created using the tutorial here, https://reactnative.dev/docs/native-modules-android#register-the-module-android-specific.:

package com.chowtime; // replace your-app-name with your app’s name
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyAppPackage implements ReactPackage {

   @Override
   public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
       return Collections.emptyList();
   }

   @Override
   public List<NativeModule> createNativeModules(
           ReactApplicationContext reactContext) {
       List<NativeModule> modules = new ArrayList<>();

       modules.add(new CalendarModule(reactContext));

       return modules;
   }

}

Note: This app was previously created using the managed-workflow on expo and was ejected to the bare workflow. This is the directory for the relevant files:

enter image description here

Upvotes: 2

Views: 10222

Answers (2)

sam_gwd
sam_gwd

Reputation: 21

I came across this same error. It seems like the docs on how to create/use native modules in react native are missing a step.

In the MainApplication.java file you need to import 'MyAppPackage'.

import com.your-app-name.MyAppPackage;

Without importing it the 'MainApplication' file it won't know what MyAppPackage is referring to hence the cannot find symbol error.

Upvotes: 2

Atmas
Atmas

Reputation: 2393

Is this as simple as an import problem?

I noticed your MyAppPackage class is defined here:

package com.chowtime; // replace your-app-name with your app’s name

But your MainApplication error message seems indicate a sub-package, specifically "chowtime.chowtime". Did you mean for both chowtimes?

   com.chowtime.chowtime.MainApplication

Upvotes: 1

Related Questions