You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expo发布版APK出现TypeError: null is not an object (evaluating 'RNPurchases.isConfigured')错误

问题描述
  • Expo版本:^45.0.0
  • react-native-purchases版本:5.0.0-beta.6 / 4.6.1
  • 开发环境运行正常,但生成谷歌应用商店发布版APK时,启动触发错误:TypeError: null is not an object (evaluating 'RNPurchases.isConfigured')
  • 初始化代码:
    // 5.0.0-beta.6版本
    Purchases.configure({ apiKey: "goog_xxxxxxxxxxxxxxxxxxxxxxxxxxx", appUserID: null, observerMode: false });
    // 4.6.1版本
    Purchases.setup("goog_xxxxxxxxxxxxxxxxxxxxxxxxxxx");
    
解决步骤

1. 补全app.json的插件配置

Expo managed workflow需要通过插件配置注入原生模块参数,确保app.json中已添加react-native-purchases的配置:

{
  "expo": {
    "plugins": [
      [
        "react-native-purchases",
        {
          "android": {
            "apiKey": "goog_xxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "appUserID": null
          }
        }
      ]
    ]
  }
}

2. 处理ProGuard混淆问题

发布版APK默认开启的ProGuard会混淆RevenueCat原生代码,导致RNPurchases对象无法被正确识别:

  • 临时禁用ProGuard(快速验证):
    {
      "expo": {
        "android": {
          "buildConfig": {
            "minifyEnabled": false,
            "proguardEnabled": false
          }
        }
      }
    }
    
  • 或添加混淆规则(长期方案):
    创建proguard-rules.pro文件,写入以下内容:
    -keep class com.revenuecat.purchases.** { *; }
    -keep class com.revenuecat.purchases.interfaces.** { *; }
    -keep class com.revenuecat.purchases.models.** { *; }
    
    然后在app.json中指定规则文件:
    {
      "expo": {
        "android": {
          "proguardRules": "proguard-rules.pro"
        }
      }
    }
    

3. 使用EAS Build构建发布版

Expo 45已弃用旧版expo build:android,改用EAS Build能更可靠地处理原生模块配置:

eas build -p android --profile production

4. 延迟初始化Purchases

发布版中原生模块加载速度可能慢于JS代码执行,可延迟初始化确保模块就绪:

import { useEffect } from 'react';
import Purchases from 'react-native-purchases';

function App() {
  useEffect(() => {
    const initTimer = setTimeout(() => {
      // 对应版本的初始化代码
      Purchases.configure({ 
        apiKey: "goog_xxxxxxxxxxxxxxxxxxxxxxxxxxx", 
        appUserID: null, 
        observerMode: false 
      });
    }, 100);

    return () => clearTimeout(initTimer);
  }, []);

  // 剩余组件代码
}

5. 清除缓存后重新构建

缓存异常可能导致打包错误,执行以下命令清理缓存:

expo r -c
eas build --clear-cache -p android --profile production

内容的提问来源于stack exchange,提问作者Chintan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 05:48:18