How to fix missing ReactContextBaseJavaModule symbol?

1.2k Views Asked by At

I wan't to create a custom native module for my app only - so no lib. Following the docs it gives me an error:

import com.facebook.react.bridge.ReactContextBaseJavaModule;

Cannot resolve symbol 'ReactContextBaseJavaModule'

  • My app's android/app/build.gradle includes this line, which is included in all react-native modules I have used so far as well.

    "com.facebook.react:react-native:+" // From node_modules

  • My android/build.gradle:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
        repositories {
            jcenter()
            google()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.4'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            mavenLocal()
            jcenter()
            mavenCentral()
            maven {
                // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
                url "$rootDir/../node_modules/react-native/android"
            }
            maven { url 'https://maven.fabric.io/public' }
            maven { url 'https://maven.google.com' }
        }
    }
    
1

There are 1 best solutions below

0
On

Below are the steps I encountered and used to solve importing all the libraries related to React Native Libraries in the Android Studio:

  1. First when I created a brand new react native library project using react-native-create-library command, the command generated iOS and Android files.
  2. I opened the android folder using Android Studio.
  3. The gradle build reported some errors and fixed the gradle issue by updating some values in build.gradle and gradle-wrapper.properties.

build.gradle


buildscript {
   repositories {
       jcenter()
       maven {
           url 'https://maven.google.com/'
           name 'Google'
       }
   }

   dependencies {
       classpath 'com.android.tools.build:gradle:2.3.0'
   }
}

apply plugin: 'com.android.library'

android {
   compileSdkVersion 23
   buildToolsVersion "27.0.1"

   defaultConfig {
       minSdkVersion 16
       targetSdkVersion 22
       versionCode 1
       versionName "1.0"
   }
   lintOptions {
       abortOnError false
   }
}

repositories {
   mavenCentral()
   maven {
       url 'https://maven.google.com/'
       name 'Google'
   }
}

dependencies {
   compile 'com.facebook.react:react-native:+'
}  

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
  1. After adding the above 2 changes, Click Sync Now or Try again. In this step if it asks to install the build version, please install.

  2. Now Clean and Rebuild the project, On top status bar of the Android Studio:

    Click Build --> Clean Project then

    Click Build --> Rebuild Project

  3. After step #5, React Native import errors should disappear and the libraries related to React Native must be imported.

Thanks.