Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to add MultiDex support - cannot find symbol context and MultiDex

After following the instructions mentioned at: https://developer.android.com/studio/build/multidex.html#mdex-gradle

I am getting an error cannot find symbol class Context variable context and variable MultiDex.

package com.mycompany.mypackage;

import android.app.Application;
import android.util.Log;

import com.facebook.react.ReactApplication;
import com.slowpath.hockeyapp.RNHockeyAppModule;
import com.slowpath.hockeyapp.RNHockeyAppPackage;
import com.microsoft.codepush.react.CodePush;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;

import com.sbugert.rnadmob.RNAdMobPackage;

import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;

import com.geektime.reactnativeonesignal.ReactNativeOneSignalPackage;
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;

import com.reactnative.ivpusic.imagepicker.PickerPackage;

import com.github.xinthink.rnmk.ReactMaterialKitPackage;

import com.learnium.RNDeviceInfo.RNDeviceInfo;

import com.burnweb.rnpermissions.RNPermissionsPackage;

import net.zubricky.AndroidKeyboardAdjust.AndroidKeyboardAdjustPackage;

// react-native-fbsdk
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.facebook.appevents.AppEventsLogger;
// react-native-fbads
import io.callstack.react.fbads.FBAdsPackage;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(context);
     MultiDex.install(this);
  }


  // react-native-fbsdk
  private static CallbackManager mCallbackManager = CallbackManager.Factory.create();
  protected static CallbackManager getCallbackManager() {
    return mCallbackManager;
  }
  @Override
  public void onCreate() {
    super.onCreate();
    FacebookSdk.sdkInitialize(getApplicationContext());
    // If you want to use AppEventsLogger to log events.
    AppEventsLogger.activateApp(this);
  }

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {

    @Override
    protected String getJSBundleFile() {
      return CodePush.getJSBundleFile();
    }

    @Override
    protected boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new RNHockeyAppPackage(MainApplication.this),
          new CodePush("mykey", MainApplication.this, BuildConfig.DEBUG),
          new ReactNativeConfigPackage(),
          new ReactNativeOneSignalPackage(),
          new ReactNativePushNotificationPackage(),
          new RNAdMobPackage(),
          new PickerPackage(),
          new ReactMaterialKitPackage(),
          new RNDeviceInfo(),
          new RNPermissionsPackage(),
          new AndroidKeyboardAdjustPackage(),
          new FBSDKPackage(mCallbackManager),
          new FBAdsPackage()
      );
    }

  };

  @Override
  public ReactNativeHost getReactNativeHost() {
      return mReactNativeHost;
  }
}

build.gradle

dependencies {
    compile project(':react-native-device-info')
    compile project(':react-native-hockeyapp')
    compile project(':react-native-code-push')
    compile project(':react-native-image-crop-picker')
    compile project(':react-native-vector-icons')
    compile project(':react-native-material-kit')
    compile project(':react-native-config')
    compile project(':RNAdMob')
    compile project(':react-native-onesignal')
    compile project(':react-native-push-notification')
    compile project(':RNPermissionsModule')
    compile project(':react-native-android-keyboard-adjust')
    compile project(':react-native-fbsdk')
    compile(project(':react-native-fbads')) {
      exclude group: "com.google.android.gms"
    }
    compile 'com.google.firebase:firebase-core:10.2.0'
    compile 'com.google.firebase:firebase-crash:10.2.0'
    compile 'com.google.firebase:firebase-ads:10.2.0'

    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
    compile "com.facebook.fresco:animated-gif:0.12.0"
    compile 'com.android.support:multidex:1.0.1'
}

Is there something I have to add or import to get this to work?

like image 509
atkayla Avatar asked Mar 05 '17 21:03

atkayla


People also ask

What is multidex support?

Multidex support for Android 5. dex files and compiles them into a single . oat file for execution by the Android device. Therefore, if your minSdkVersion is 21 or higher multidex is enabled by default, and you do not need the multidex library. For more information on the Android 5.0 runtime, read ART and Dalvik.

What is the maximum number of methods supported by Dex compiler before needing multidex?

dex file can have 65,536 methods(references) so if the number of references exceeds 65,536, you go with multidex.

Is multidex needed?

Android 5.0 (API level 21) and higher uses ART which supports multidexing. Therefore, if your minSdkVersion is 21 or higher, the multidex support library is not needed.

Do I need the multidex library?

Therefore, if your minSdkVersion is 21 or higher multidex is enabled by default, and you do not need the multidex library. For more information on the Android 5.0 runtime, read ART and Dalvik . Note: When running your app using Android Studio, the build is optimized for the target devices you deploy to.

How to enable multidex for Android app?

How to Enable Multidex for Android App 1 Keeping a tap on the number of methods, the third party libraries added to the once project 2 Removing unused dependencies. 3 Avoiding the addition of group dependencies. More ...

How to enable multidex for the flutter project?

Follow the below steps to enable multidex for the flutter project. Enable multidex. Open project/app/build.gradle and add the following lines. Enable Jetifier. Open project/ android / app /gradle.properties and add the following lines.

How to fix--multi-Dex error in Android?

You may try using --multi-dex option. To solve this error, you can optimize your code or enable Multidex support in your Android project. You can avoid muiltidex support in your Android app by reducing the number of reference methods, as well as other libraries (dependencies).


2 Answers

For those who get cannot find symbol error, the following is stated in the official android developer documents:

If your minSdkVersion is 21 or higher multidex is enabled by default, and you do not need the multidex support library.
like image 103
Mehmet Katircioglu Avatar answered Oct 20 '22 01:10

Mehmet Katircioglu


If you are using RN > 0.60 and androidx support, you may add,

import androidx.multidex.MultiDex;
like image 31
Shehan Dhaleesha Avatar answered Oct 19 '22 23:10

Shehan Dhaleesha