Expo项目在本地打包apk的问题

没想到在新公司又用到了React Native,于是配置安卓环境又浪费了我一天时间。

本来我们用的是expo,它可以完全跳过安卓环境来打包项目,走EAS Build。但是官网打包它慢,有时还要排队,我最长打过1个多小时的包,真的是不太能忍。

另一点是因为新项目很可能需要用原生代码去实现某些功能,所以研究了npx expo prebuild,把原项目的android文件夹暴露出来。

然后就开始了痛苦的一天,虽然app\android\gradle\wrapper\gradle-wrapper.properties里替换了distributionUrl=https://mirrors.cloud.tencent.com/gradle/gradle-8.13-bin.zip

app\android\build.gradle里也增加了阿里云的路径:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
  repositories {
    maven{ url 'https://maven.aliyun.com/repository/central' }
    maven{ url 'https://maven.aliyun.com/repository/public' }
    maven{ url 'https://maven.aliyun.com/repository/google' }
    maven{ url 'https://maven.aliyun.com/repository/gradle-plugin'}
    
    google()
    mavenCentral()
  }
  dependencies {
    classpath('com.android.tools.build:gradle')
    classpath('com.facebook.react:react-native-gradle-plugin')
    classpath('org.jetbrains.kotlin:kotlin-gradle-plugin')
  }
}

def reactNativeAndroidDir = new File(
  providers.exec {
    workingDir(rootDir)
    commandLine("node", "--print", "require.resolve('react-native/package.json')")
  }.standardOutput.asText.get().trim(),
  "../android"
)

allprojects {
  repositories {
    maven{ url 'https://maven.aliyun.com/repository/central' }
    maven{ url 'https://maven.aliyun.com/repository/public' }
    maven{ url 'https://maven.aliyun.com/repository/google' }
    maven{ url 'https://maven.aliyun.com/repository/gradle-plugin'}
    maven {
      // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
      url(reactNativeAndroidDir)
    }

    google()
    mavenCentral()
    maven { url 'https://www.jitpack.io' }
  }
}

apply plugin: "expo-root-project"
apply plugin: "com.facebook.react.rootproject"

可真跑起gradlew assembleRelease来,还是慢慢吞吞,下载了一个多小时的.gradle包后跟我说,jdk版本太低,至少应该为11。

晕,原电脑的同事用的是java8…… 换完java11后,又告诉我至少得用java17…… 算了,我直接下java21得了。

在javac -version显示javac 21.0.7的情况下,使用gradlew assembleRelease,还报错找不到java11……

我以为是项目的问题,重来了几次依旧不行。后来才查到,原来是C:\Users\用户\.gradle捣的鬼,所以一上头,直接把这下了一个多小时几个G的.gradle删除。

不管是gradlew assembleRelease还是gradlew clean,还是会继续下载该包的,但鉴于经常有些包请求不到,于是考虑走本地代理,在app\android\gradle.properties中增加配置:

systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=7897
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=7897

解决了下载gradle包超级慢的问题,它依旧会报错:缺少ANDROID_HOME配置环境。

果然,还是得把安卓的SDK弄一遍。理论上可以只下载SDK不用下载Android Studio,然而实际官网那个SDK包我是真的不大会用,里面虽然看到个sdkmanager.bat,但不是可视化操作真不如jdk那样安装方便。

退而求其次,干脆还是下载Android Studio吧,结果下载安装时,需要配置代理才能安装SDK。什么mirrors.neusoft.edu.cn、mirrors.aliyun.com都试过,看似通了其实并没有。

The Android SDK location cannot be at the filesystem root

在sdk components setup根本就无法勾选SDK,折腾了几个小时,最后还是换成本地代理解决了。

配置好ANDROID_HOME,终于能让expo项目在本地生成apk了,除了第一次生成有些慢之外,第二次以后生成时间也就一两分钟有点超出我的意料。因为之前用RN写的项目,打个包也至少得跑几分钟的代码,Expo居然能加速这么多?!

Expo是一个基于React Native的开源框架,它简化了原生应用开发流程,并提供了热更新、调试等便利功能。要在 Expo 中实现本地打包 Android 应用,你需要按照以下步骤操作: 1. **确保已安装 Expo CLI**: 首先需要确保你在本地已经安装了 Expo CLI,可以通过 npm 或 yarn 安装: ``` npm install -g expo-cli ``` 2. **创建项目**: 使用 `expo init` 创建一个新的 React Native 项目,选择非模板项目,因为我们需要手动管理构建过程: ``` expo init my-android-app cd my-android-app ``` 3. **更改配置**: 进入项目目录后,编辑 `.gitignore` 文件,移除 `android` 和 `ios` 目录,以便版本控制只包含核心代码和库依赖。 4. **添加 Android 配置**: 手动克隆 ExpoAndroid 平台到 `android` 目录: ``` git clone https://github.com/expo/expo.git android/app ``` 5. **替换 dependencies**:将 Expo 的 `node_modules` 替换为你需要的第三方库,特别是对于 Android。 6. **准备 build.gradle 文件**: 在 `android/app/build.gradle` 中,配置 Gradle 版本和插件,以及添加必要的 AndroidManifest.xml 编译设置。 7. **运行 app:** 确保安装了 Android SDK Tools 和 Android Studio,然后运行 `cd android && ./gradlew assembleDebug` 来生成 APK。 8. **签名和发布**: 如果需要发布,你需要创建一个 Keystore 文件并签名 APK。可以参考 Expo 文档中的 [Signing Your App](https://docs.expo.dev/workflow/releases/#signing-your-app)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值