13

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?

atkayla
  • 8,143
  • 17
  • 72
  • 132
  • 2
    Did you add the mutidex to your gradle? Also, you don't need to do this- just extend MultiDexApplication instead of Application. – Gabe Sechan Mar 05 '17 at 21:21
  • @GabeSechan Hi, thank you for your help. Yes, I edited my post with my build.gradle. Extending MultiDexApplication seems to give me a lot more errors, so I think I have to do the MultiDex.install method instead. – atkayla Mar 05 '17 at 21:25
  • 1
    Did you sync gradle and do a full rebuild? Using the other method isn't going to help. Your problem is a syntax error, not a logic error. Also, you cut them out but do you have all the proper imports? – Gabe Sechan Mar 05 '17 at 21:26
  • 1
    Yes I did the sync gradle and rebuild. The imports are there for everything else, but do I need any imports for Context and MultiDex? I didn't add those if I do. – atkayla Mar 05 '17 at 21:34
  • 1
    Yes. Any class mentioned by name not in your current package must be imported. – Gabe Sechan Mar 05 '17 at 21:35

5 Answers5

26

Thanks to @Gabe Sechan for the help. I know React Native/JavaScript, kind of clueless about Android/Java, so I simply followed the instructions at https://developer.android.com/studio/build/multidex.html#mdex-gradle, which did not mention any additional imports. I learned to look up the package I needed here: https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html. After adding:

import android.support.multidex.MultiDexApplication;

public class MainApplication extends MultiDexApplication implements ReactApplication {
...

App seems to build and run successfully on Android 4.4.4+ devices. On my Samsung Galaxy S3 simulator running 4.3 however, I am getting a crash upon app start: What does WIN DEATH: android.osDeadObjectException mean?, which seems like another issue altogether.

Community
  • 1
  • 1
atkayla
  • 8,143
  • 17
  • 72
  • 132
  • 5
    If you are using Android X, you will need to import `import androidx.multidex.MultiDexApplication;` – Aung Myint Thein Jul 30 '19 at 11:20
  • 1
    Thanks a lot @AungMyintThein !! RN > 0.60 uses AndroidX, so your line did the work. Also you don't need to add the `@override` part from the Android docs; just `extends` the class and import it like you said and it's all good. – betoharres Dec 18 '19 at 18:27
4

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

import androidx.multidex.MultiDex;
Shehan Dhaleesha
  • 627
  • 1
  • 10
  • 30
4

Adding dependencies: { implementation 'com.android.support:multidex:1.0.3' } into android/app/build.gradle fixed my problem. It happened when I bumped minSDKversion to 21.

Paul Oskar Soe
  • 204
  • 1
  • 4
3

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.
Mehmet Katircioglu
  • 1,200
  • 1
  • 11
  • 20
1

The reason you are having this "symbol not found" issue is because you haven't imported the required imports. Why not simply add the following imports

import android.support.multidex.MultiDex;

import android.content.Context;

You should know the different cases for using each Multidex option. Kindly refer to https://developer.android.com/studio/build/multidex#java for all information relating to MultiDexing

herlarby
  • 111
  • 2
  • 4