React Native之打包安卓修改Apk文件名
在 React Native 项目中,修改打包生成的 APK 文件名可以通过配置 Android 的 Gradle 构建脚本实现。以下是具体步骤:
1、打开 Gradle 配置文件
进入项目的 Android 模块目录,找到 android/app/build.gradle 文件。
2、修改构建脚本
在 android 配置块内添加以下代码,动态设置输出的 APK 文件名:
android {
...
applicationVariants.all { variant ->
variant.outputs.all {
def versionName = variant.versionName
def versionCode = variant.versionCode
def date = new Date().format("yyyyMMdd")
def buildType = variant.buildType.name
outputFileName = "AppName_${buildType}_v${versionName}_${date}.apk"
}
}
}
2
3
4
5
6
7
8
9
10
11
12
参数说明:
- versionName: 应用版本名称(在 defaultConfig 中定义)
- versionCode: 应用版本代码(在 defaultConfig 中定义)
- date: 当前日期,格式化为年月日(例如 20231023)
- buildType: 构建类型(如 debug、release)
现在打包后的 apk 文件名如: AppName_release_v1.0.0_20231023.apk
提示
Gradle 版本兼容性:使用 variant.outputs.all 代替旧版的 each 方法,确保兼容 Gradle 3.0+。
按照如上操作完,再次执行打包命令可能会出现如下错误,错误信息显示 Task :app:packageRelease 失败,具体是执行 PackageAndroidArtifact$IncrementalSplitterRunnable 时出现问题:
这可能是旧构建缓存导致冲突,需要清楚缓存
# 进入安卓项目目录
cd android
# 执行清理命令
./gradlew clean
# 重新打包
./gradlew assembleRelease
2
3
4
5
6
7
8
此时,能够正常打包了。
此时,按照上述配置打包后确实生成了一个 apk 文件,但是只生成了一个,按照此前拆分 cpu 架构(ABI 分包)的情况下应该生成多个 apk 文件才对啊,因此,需要进一步优化上述配置,
1、启用 ABI 分包配置
在 android/app/build.gradle 中配置 ABI 分包策略:
android {
splits {
abi {
enable true // 启用ABI分包
reset() // 重置默认配置
include "armeabi-v7a", "arm64-v8a", "x86", "x86_64" // 指定拆分的ABI类型
universalApk true // 生成通用APK
}
}
}
2
3
4
5
6
7
8
9
10
2、动态获取 ABI 架构类型
android {
applicationVariants.all { variant ->
variant.outputs.all { output ->
// 获取当前输出的ABI类型(如arm64-v8a)
def abi = output.getFilter(OutputFile.ABI)
def abiName = abi ? "_${abi}" : "_universal" // 处理无ABI的情况
def versionName = variant.versionName
def versionCode = variant.versionCode
def date = new Date().format("yyyyMMdd")
def buildType = variant.buildType.name
// 组合最终文件名
outputFileName = "MyApp_${buildType}_v${versionName}_${date}${abiName}.apk"
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
此时,再打包,如果正确的下,应该会生成类似 MyApp_release_v1.2.0_20240101_arm64-v8a.apk 这种格式的 apk 文件的。
此时再次执行清理构建缓存操作,不出意外的话意外要出现了,又报错了,提示 “MissingPropertyException: No such property: ABI for class: org.gradle.api.tasks.OutputFile”
这是因为我们还没有引入 OutputFile。
解决办法:在 android/app/build.gradle 文件顶部添加以下导入语句
import com.android.build.OutputFile
至此,再执行打包命令,就能成功打包了。
附上 android/app/build.gradle 文件的完整配置:
import com.android.build.OutputFile
apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"
react {
/* Autolinking */
autolinkLibrariesWithApp()
}
def enableProguardInReleaseBuilds = true
def enableSeparateBuildPerCPUArchitecture = true
def jscFlavor = 'io.github.react-native-community:jsc-android:2026004.+'
project.ext.react = [
enableHermes: true, // 启用 Hermes
bundleInDebug: false,
bundleInRelease: true,
devDisabledInRelease: true
]
android {
ndkVersion rootProject.ext.ndkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdk rootProject.ext.compileSdkVersion
namespace "com.videoapp"
defaultConfig {
applicationId "com.videoapp"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
release {
if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
minifyEnabled true // 启用代码压缩
shrinkResources true // 移除未使用的资源
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.release
}
}
// 自定义apk文件名
applicationVariants.all { variant ->
variant.outputs.all { output ->
// 动态获取ABI架构类型
def abi = output.getFilter(OutputFile.ABI)
def abiSuffix = abi ? "_${abi}" : "_universal"
// 其他参数
def projectName = "VideoApp"
def buildType = variant.buildType.name
def versionName = variant.versionName
def date = new Date().format("yyyyMMdd")
// 生成最终文件名
outputFileName = "${projectName}_${buildType}_v${versionName}_${date}${abiSuffix}.apk"
}
}
// 拆分cpu架构(ABI分包)
splits {
abi {
enable true
reset()
include "arm64-v8a", "x86", "x86_64" // 按需选择
universalApk true
}
}
}
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle") // 使用react-native-vector-icons图标是需要添加
dependencies {
// The version of react-native is set by the React Native Gradle Plugin
implementation("com.facebook.react:react-android")
if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
} else {
implementation jscFlavor
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109