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

引入react-native-orientation后Android构建失败,提示找不到compile()方法

React Native Android构建react-native-orientation报错解决方法

1. 替换Gradle依赖指令

报错核心原因是新版Gradle已废弃compile()方法,改用implementation()。
打开android/app/build.gradle,找到dependencies代码块,将:

compile(project(':react-native-orientation'))

替换为:

implementation(project(':react-native-orientation'))

2. 修正手动链接配置

检查settings.gradle

打开android/settings.gradle,确认已添加以下配置:

include ':react-native-orientation'
project(':react-native-orientation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-orientation/android')

检查MainApplication.java

打开android/app/src/main/java/[你的包名]/MainApplication.java:

  • 导入包:
    import com.github.yamill.orientation.OrientationPackage;
    
  • 在getPackages()方法中注册包:
    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.asList(
            new MainReactPackage(),
            new OrientationPackage() // 添加这一行
        );
    }
    

3. 清理缓存并重新构建

执行以下命令清理Gradle缓存,避免旧配置干扰:

cd android
./gradlew clean
cd ..
npx react-native run-android

4. 版本兼容性检查

如果以上步骤无效,检查package.json中react-native-orientation的版本,若版本过旧,尝试升级到与当前React Native版本兼容的最新版。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 08:40:34