使用expo-local-authentication遇双错误:模块找不到+Gradle构建失败
Expo Local Authentication 构建错误解决指南
问题描述
- 集成
expo-local-authentication后,在物理设备运行Expo开发构建时触发错误:Error: Cannot find native module 'ExpoLocalAuthentication', js engine: hermes - 尝试重新构建,执行
gradlew命令时再次报错:Could not set unknown property 'classifier' for task ':expo-local-authentication:androidSourcesJar' of type org.gradle.api.tasks.bundling.Jar
解决方案
解决「找不到原生模块」错误
- 确保已执行
npx expo prebuild生成原生项目文件,开发构建依赖预构建的原生代码,无法直接使用Expo Go。 - 重新安装依赖:执行
npm install expo-local-authentication,完成后重新运行npx expo prebuild。 - 清理缓存:删除
node_modules、android、ios目录,依次执行npm install、npx expo prebuild、expo run:android。 - 确认物理设备与开发机处于同一网络,开发服务器地址配置正确。
解决Gradle「classifier属性未知」错误
该错误源于Gradle 7.0+不再支持直接在Jar任务中设置classifier属性,而当前使用的expo-local-authentication@~13.4.1版本未适配此变更。可通过以下方式解决:
- 升级依赖版本:执行
npm install expo-local-authentication@~13.4.2(此版本已修复Gradle配置问题,兼容Expo 49)。 - 手动修改Gradle配置:打开
node_modules/expo-local-authentication/android/build.gradle,找到androidSourcesJar任务,将
修改为task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.srcDirs }
修改完成后,在task androidSourcesJar(type: Jar) { archiveClassifier = 'sources' from android.sourceSets.main.java.srcDirs }android目录下执行./gradlew clean build。 - 检查项目根目录
android/build.gradle中的Gradle版本,确保使用Expo 49推荐的Gradle 7.5.1及插件7.2.2。
配置文件参考
app.json
{ "expo": { "name": "gctimes_app", "slug": "gctimes_app", "version": "1.0.0", "orientation": "portrait", "icon": "./assets/icon.png", "userInterfaceStyle": "light", "splash": { "image": "./assets/splash.png", "resizeMode": "contain", "backgroundColor": "#ffffff" }, "assetBundlePatterns": [ "**/*" ], "ios": { "supportsTablet": true }, "android": { "googleServicesFile": "./google-services.json", "adaptiveIcon": { "foregroundImage": "./assets/adaptive-icon.png", "backgroundColor": "#ffffff" }, "package": "com.example.example_app" }, "web": { "favicon": "./assets/favicon.png" }, "extra": { "eas": { "projectId": "project_id" } } } }
package.json
{ "name": "gctimes_app", "version": "1.0.0", "main": "node_modules/expo/AppEntry.js", "scripts": { "start": "expo start", "android": "expo run:android", "ios": "expo run:ios", "web": "expo start --web" }, "dependencies": { "@expo/vector-icons": "^13.0.0", "@react-native-community/masked-view": "^0.1.11", "@react-native-firebase/analytics": "^18.5.0", "@react-native-firebase/app": "^18.5.0", "@react-native-firebase/messaging": "^18.5.0", "@react-navigation/native": "^6.1.7", "@react-navigation/stack": "^6.3.17", "axios": "^1.5.0", "base-64": "^1.0.0", "expo": "~49.0.8", "expo-dev-client": "~2.4.11", "expo-font": "^11.4.0", "expo-splash-screen": "~0.20.5", "expo-status-bar": "~1.6.0", "i18next": "^23.5.1", "react": "18.2.0", "react-i18next": "^13.2.2", "react-native": "0.72.4", "react-native-date-picker": "^4.3.3", "react-native-date-ranges": "^2.5.0", "react-native-dropdown-picker": "^5.4.6", "react-native-gesture-handler": "~2.12.0", "react-native-paper": "^5.10.6", "react-native-reanimated": "^3.5.2", "react-native-safe-area-context": "4.6.3", "react-native-screens": "~3.22.0", "react-native-table-component": "^1.2.2", "react-native-vector-icons": "^10.0.0", "uuid": "^9.0.1", "expo-local-authentication": "~13.4.1" }, "devDependencies": { "@babel/core": "^7.20.0" }, "private": true }
内容的提问来源于stack exchange,提问作者Georgios Kourounis
相关产品推荐
相关产品推荐

