我有一个使用react native构建的应用程序,我需要启用MultiDex
支持。我的问题是我无法导入MultiDexApplication
类来扩展它,因为在编译时,在symbol not found error
中扩展它时,导入语句和类名都得到MainApplication.java
>
build.gradle
dependencies {
classpath('com.android.tools.build:gradle:3.5.3')
classpath 'com.google.gms:google-services:4.2.0'
classpath "androidx.multidex:multidex:2.0.1"
}
如果我尝试将依赖项添加为实现,则在启动应用程序时出现以下错误:
Could not find method implementation() for arguments [androidx.multidex:multidex:2.0.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
在所有在线资源中,我都看到依赖项被添加为实现,因此我想这可能是我的问题。
MainApplication.java
package com.classmanager;
import android.app.Application;
import androidx.multidex.MultiDexApplication;
import android.content.Context;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.oblador.vectoricons.VectorIconsPackage;
import com.oblador.vectoricons.VectorIconsPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class MainApplication extends MultiDexApplication implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
return packages;
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
initializeFlipper(this); // Remove this line if you don't want Flipper enabled
}
//@Override
//protected void attachBaseContext(Context base) {
//super.attachBaseContext(base);
//MultiDex.install(this);
//}
/**
* Loads Flipper in React Native templates.
*
* @param context
*/
private static void initializeFlipper(Context context) {
if (BuildConfig.DEBUG) {
try {
/*
We use reflection here to pick up the class that initializes Flipper,
since Flipper library is not available in release mode
*/
Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
这是react-native init
中的默认MainApplication,除了我导入了MultiDexApplication
类并扩展了它而不是Application
。但是,当我运行该应用程序时,出现以下错误:
import androidx.multidex.MultiDexApplication;
^
symbol: class MultiDexApplication
location: package androidx.multidex
C:\Users\meadi\WebstormProjects\ClassManager\android\app\src\main\java\com\classmanager\MainApplication.java:16: error: cannot find symbol
public class MainApplication extends MultiDexApplication implements ReactApplication {
^
symbol: class MultiDexApplication
C:\Users\meadi\WebstormProjects\ClassManager\android\app\src\main\java\com\classmanager\MainApplication.java:18: error: incompatible types: MainApplication cannot be converted to Application
new ReactNativeHost(this) {
^
有人知道为什么无法解决MultiDexApplication吗?
更新:我尝试将multidex依赖作为实现添加到应用程序级别build.gradle
。看来现在该类已解决,但出现此错误:
D8: Cannot fit requested classes in a single dex file (# methods: 100718 > 65536)
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
The number of method references in a .dex file cannot exceed 64K.
因此仍未启用MultiDex
答案 0 :(得分:1)
类路径“ androidx.multidex:multidex:2.0.1”。应该是implementation 'androidx.multidex:multidex:2.0.1'
。
build.gradle (Module)
->这定义了特定于模块的构建配置。
build.gradle (Project)
->这定义了适用于所有模块的构建配置。该文件是项目必不可少的,因此您应将其与所有其他源代码一起保持在版本控制中。
您应在模块级别 build.gradle
(android/app/build.gradle)
部分下方添加
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}
仅供参考
请从项目级别 multiDexEnabled = true
部分
build.gradle
修改 模块级 build.gradle文件以启用multidex,并将multidex库添加为依赖项,如下所示:
android {
defaultConfig {
applicationId "com.classmanager"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
multiDexEnabled true
}
}
答案 1 :(得分:0)
您是否已将此添加到应用级别build.gradle
?
android {
defaultConfig {
...
minSdkVersion ...
targetSdkVersion ...
multiDexEnabled true // <------------------------
}
...
}
(来自https://developer.android.com/studio/build/multidex#mdex-gradle的引用)