在 GitHub 上完成 React Native Vector Icons 的所有安装过程后,我最终收到此错误,告诉我:
Native module RNVectorIcons tries to override RNVectorIcons. Check the getPackages() method in MainApplication.java, it might be that module is being created twice. If this was your intention, set canOverrideExsistingModule=true.
如果该包仅在
getPackages()
中出现一次,但稍后在构建时通过自动链接自动添加,则也可能出现此错误。尝试删除现有条目并重建。
我的 Application.kt 看起来像这样:
package com.project
import android.app.Application
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.soloader.SoLoader
import com.oblador.vectoricons.VectorIconsPackage;
class MainApplication : Application(), ReactApplication {
override val reactNativeHost: ReactNativeHost =
object : DefaultReactNativeHost(this) {
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
add(VectorIconsPackage())
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
}
override fun getJSMainModuleName(): String = "index"
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
}
override val reactHost: ReactHost
get() = getDefaultReactHost(applicationContext, reactNativeHost)
override fun onCreate() {
super.onCreate()
SoLoader.init(this, false)
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// If you opted-in for the New Architecture, we load the native entry point for this app.
load()
}
}
}
如果我尝试删除
add(VectorIconsPackage())
,我会收到两个错误。第一个错误如下所示:
ERROR Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'VectorIcons' could not be found. Verify that a module by this name is registered in the native binary.Bridgeless mode: false. TurboModule interop: false. Modules loaded: {"NativeModules":["PlatformConstants","LogBox","Timing","AppState","SourceCode","BlobModule","WebSocketModule","DevSettings","DevToolsSettingsManager","Networking","Appearance","DevLoadingView","HeadlessJsTaskSupport","UIManager","DeviceInfo","ImageLoader","SoundManager","IntentAndroid","DeviceEventManager"],"TurboModules":[],"NotFound":["NativePerformanceCxx","NativePerformanceObserverCxx","RedBox","BugReporting","LinkingManager","VectorIcons"]}, js engine: hermes
LOG Running "Project" with {"rootTag":11}
第二个错误如下所示:
Invariant Violation: "Project" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called., js engine: Project
依赖关系:
"dependencies": {
"@react-native-vector-icons/ionicons": "^7.4.0-alpha.16",
"@types/react-native-vector-icons": "^6.4.18",
"react": "18.2.0",
"react-native": "0.74.3",
"react-native-vector-icons": "^10.1.0"
}
我尝试按照作者在 Github 上的建议编辑 fonts.gradle,但错误仍然存在。
更新或重新安装 React Native Vector Icons 似乎根本没有帮助。