裸React Native项目安装Expo失败,构建报错求助
裸React Native项目集成Expo模块Android构建失败问题
问题现象
使用npx react-native init创建裸RN项目,为集成expo-camera安装Expo模块后,执行以下命令:
npx expo install expo-constants npx expo run:android
遇到构建错误,核心报错信息:
FAILURE: Build failed with an exception. * Where: Build file 'C:\Users\sulei\Desktop\ReactNativeProjects\Instagram\node_modules\expo-application\android\build.gradle' line: 40 * What went wrong: A problem occurred evaluating project ':expo-application'. > Could not set unknown property 'classifier' for task ':expo-application:androidSourcesJar' of type org.gradle.api.tasks.bundling.Jar.
我的配置文件
android/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP. ndkVersion = "23.1.7779620" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle") classpath("com.android.tools.build:gradle:7.4.1") classpath("com.facebook.react:react-native-gradle-plugin") } } allprojects { repositories { jcenter() { content { includeModule("com.yqritc", "android-scalablevideoview") } } } }
settings.gradle
rootProject.name = 'Instagram' apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) include ':app' includeBuild('../node_modules/@react-native/gradle-plugin') apply from: new File(["node", "--print", "require.resolve('expo/package.json')"].execute(null, rootDir).text.trim(), "../scripts/autolinking.gradle") useExpoModules()
MainActivity.java
package com.instagram; import expo.modules.ReactActivityDelegateWrapper; import com.facebook.react.ReactActivity; import com.facebook.react.ReactActivityDelegate; import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint; import com.facebook.react.defaults.DefaultReactActivityDelegate; public class MainActivity extends ReactActivity { /** * Returns the name of the main component registered from JavaScript. This is used to schedule * rendering of the component. */ @Override protected ReactActivityDelegate createReactActivityDelegate() { return new ReactActivityDelegateWrapper(this, BuildConfig.IS_NEW_ARCHITECTURE_ENABLED, new DefaultReactActivityDelegate( this, getMainComponentName(), // If you opted-in for the New Architecture, we enable the Fabric Renderer. DefaultNewArchitectureEntryPoint.getFabricEnabled(), // fabricEnabled // If you opted-in for the New Architecture, we enable Concurrent React (i.e. React 18). DefaultNewArchitectureEntryPoint.getConcurrentReactEnabled() // concurrentRootEnabled )); } }
MainApplication.java
package com.instagram; import android.content.res.Configuration; import expo.modules.ApplicationLifecycleDispatcher; import expo.modules.ReactNativeHostWrapper; import android.app.Application; import com.facebook.react.PackageList; import com.facebook.react.ReactApplication; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint; import com.facebook.react.defaults.DefaultReactNativeHost; import com.facebook.soloader.SoLoader; import java.util.List; public class MainApplication extends Application implements ReactApplication { private final ReactNativeHost mReactNativeHost = new ReactNativeHostWrapper(this, new DefaultReactNativeHost(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 protected boolean isNewArchEnabled() { return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED; } @Override protected Boolean isHermesEnabled() { return BuildConfig.IS_HERMES_ENABLED; } }); @Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; } @Override public void onCreate() { super.onCreate(); SoLoader.init(this, /* native exopackage */ false); if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { // If you opted-in for the New Architecture, we load the native entry point for this app. DefaultNewArchitectureEntryPoint.load(); } ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); ApplicationLifecycleDispatcher.onApplicationCreate(this); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig); } }
package.json
{ "name": "Instagram", "version": "0.0.1", "private": true, "scripts": { "android": "react-native run-android", "ios": "react-native run-ios", "lint": "eslint .", "start": "react-native start", "test": "jest" }, "dependencies": { "expo": "^48.0.19", "react": "18.2.0", "react-native": "0.72.0", "react-native-image-picker": "^5.6.0", "react-native-vector-icons": "^9.2.0", "react-native-video": "^5.2.1", "expo-constants": "~14.2.1" }, "devDependencies": { "@babel/core": "^7.20.0", "@babel/preset-env": "^7.20.0", "@babel/runtime": "^7.20.0", "@react-native/eslint-config": "^0.72.2", "@react-native/metro-config": "^0.72.6", "@tsconfig/react-native": "^3.0.0", "@types/metro-config": "^0.76.3", "@types/react": "^18.0.24", "@types/react-native-vector-icons": "^6.4.13", "@types/react-native-video": "^5.0.14", "@types/react-test-renderer": "^18.0.0", "babel-jest": "^29.2.1", "eslint": "^8.19.0", "jest": "^29.2.1", "metro-react-native-babel-preset": "0.76.5", "prettier": "^2.4.1", "react-test-renderer": "18.2.0", "typescript": "4.8.4" }, "engines": { "node": ">=16" } }
解决方案
这个错误是Gradle版本与Expo SDK 48不兼容导致的:classifier属性在Gradle 8.x中已被废弃,而你当前使用的是Gradle 8.0.1,但Expo SDK 48需要搭配Gradle 7.x版本(对应AGP 7.4.1)。
步骤1:修正android/build.gradle
删除buildscript dependencies中重复的classpath("com.android.tools.build:gradle"),只保留指定版本的依赖:
dependencies { classpath("com.android.tools.build:gradle:7.4.1") classpath("com.facebook.react:react-native-gradle-plugin") }
步骤2:调整Gradle wrapper版本
打开android/gradle/wrapper/gradle-wrapper.properties,修改distributionUrl为Gradle 7.5:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
步骤3:清理缓存并重新构建
执行以下命令:
# 清理Android构建缓存 cd android && ./gradlew clean # 清理node_modules并重新安装 cd .. && rm -rf node_modules && npm install # 重新运行项目 npx expo run:android
验证
完成上述步骤后,项目应该能正常构建并运行。如果仍有问题,可以检查expo和expo-constants的版本是否与SDK 48完全匹配(你的package.json中版本是符合要求的)。
内容的提问来源于stack exchange,提问作者vividtheapartmentrye
相关产品推荐
相关产品推荐

