React Native中Solana移动钱包协议包未链接问题解决
问题描述
在React Native应用中使用@solana-mobile/mobile-wallet-adapter-protocol连接移动钱包时,遇到错误:
Error: The package 'solana-mobile-wallet-adapter-protocol' doesn't seem to be linked
已按错误提示检查并执行npx react-native run-android重建应用,但问题依旧。
环境信息
- React Native版本:0.76.6
- Node.js版本:v18.x
- 依赖包版本:
"@solana-mobile/mobile-wallet-adapter-protocol": "^2.2.0-new-arch-beta", "@solana-mobile/mobile-wallet-adapter-protocol-web3js": "^2.1.4"
相关代码
async function handleConnectWallet() { try { setLoading(true); // Use the transact function to handle wallet interactions const authorizationResult = await transact( async (wallet: Web3MobileWallet) => { const authResult = wallet.authorize({ chain: "solana:devnet", identity: APP_IDENTITY, }); return authResult; }, ); console.log("Connected to:", authorizationResult.accounts[0].address); setWalletAddress(authorizationResult.accounts[0].address); } catch (err) { console.error("Wallet connection failed:", err); Alert.alert("Error", "Failed to connect to the wallet."); } finally { setLoading(false); } }
解决步骤
彻底清理并重建依赖
执行以下命令清除缓存、重新安装依赖并构建:rm -rf node_modules package-lock.json yarn.lock npm install npx react-native clean npx react-native run-android完善nohoist配置
在项目根目录的package.json中添加Solana Mobile包的nohoist规则,避免依赖提升导致链接失败:"workspaces": { "nohoist": [ "@solana-mobile/**", "@solana-mobile/*" ] }添加后重新执行依赖安装和构建。
适配新架构配置
你使用的是beta版新架构包,需确保项目已启用React Native新架构:- Android:修改
android/gradle.properties,设置newArchEnabled=true - iOS:修改
ios/Podfile,在use_react_native!中添加:new_arch_enabled => true,然后执行cd ios && pod install
完成配置后重新构建应用。
- Android:修改
统一依赖版本
当前两个Solana包版本不匹配(beta版+稳定版),建议降级到同版本稳定版:npm install @solana-mobile/mobile-wallet-adapter-protocol@2.1.4版本统一后重新构建应用。
手动验证Android链接(可选)
若自动链接未生效,可检查Android配置:- 打开
android/app/src/main/java/[你的包名]/MainApplication.java - 确认是否导入
SolanaMobileWalletAdapterPackage,并在getPackages()方法中添加该包(仅旧架构需要)
- 打开
内容的提问来源于stack exchange,提问作者Gh05d
相关产品推荐
相关产品推荐

