简体   繁体   English

尝试添加 MultiDex 支持 - 找不到符号上下文和 MultiDex

[英]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按照以下说明操作后: 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 .我收到错误cannot find symbol class Context variable contextvariable 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构建.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?有什么我必须添加或导入才能让它工作的吗?

Thanks to @Gabe Sechan for the help.感谢@Gabe Sechan 的帮助。 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.我知道 React Native/JavaScript,对 Android/Java 一无所知,所以我只是按照https://developer.android.com/studio/build/multidex.html#mdex-gradle 上的说明进行操作,其中没有提到任何其他进口。 I learned to look up the package I needed here: https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html .我学会了在这里查找我需要的包: 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.应用程序似乎可以在 Android 4.4.4+ 设备上成功构建和运行。 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?但是,在运行 4.3 的三星 Galaxy S3 模拟器上,我在应用程序启动时崩溃: WIN DEATH: android.osDeadObjectException 是什么意思? , which seems like another issue altogether. ,这似乎完全是另一个问题。

如果您使用 RN > 0.60 和 androidx 支持,您可以添加,

import androidx.multidex.MultiDex;

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.您应该了解使用每个 Multidex 选项的不同情况。 Kindly refer to https://developer.android.com/studio/build/multidex#java for all information relating to MultiDexing有关 MultiDexing 的所有信息,请参阅https://developer.android.com/studio/build/multidex#java

对于那些得到找不到符号错误的人,在官方android开发者文档中说明如下:

 If your minSdkVersion is 21 or higher multidex is enabled by default, and you do not need the multidex support library.

Adding dependencies: { implementation 'com.android.support:multidex:1.0.3' } into android/app/build.gradle fixed my problem.添加dependencies: { implementation 'com.android.support:multidex:1.0.3' }到 android/app/build.gradle 修复了我的问题。 It happened when I bumped minSDKversion to 21.当我将 minSDKversion 提高到 21 时发生了这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM