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

使用NativeScript 8.6.5开发的新应用APK体积过大原因咨询

NativeScript 8.6.5 应用APK体积过大问题排查与优化建议

我用NativeScript 8.6.5开发的新应用APK体积达121MB,其中107MB来自lib文件夹,x86、x86_64、arm64-v8a和armeabi-v7a四个架构的libNativescript.so文件每个都超过20MB。应用仅包含少量组件,assets目录无图片文件,依赖也偏基础,但就算移除部分依赖,APK体积仍不低于90MB。

项目配置文件

package.json

{
  "name": "appname",
  "main": "src/main.ts",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "@angular/animations": "~16.2.0",
    "@angular/common": "~16.2.0",
    "@angular/compiler": "~16.2.0",
    "@angular/core": "~16.2.0",
    "@angular/forms": "~16.2.0",
    "@angular/platform-browser": "~16.2.0",
    "@angular/platform-browser-dynamic": "~16.2.0",
    "@angular/router": "~16.2.0",
    "@nativescript/angular": "^16.0.0",
    "@nativescript/core": "~8.6.0",
    "@nativescript/pdf": "^2.0.0",
    "@nativescript/theme": "~3.0.2",
    "@nstudio/nativescript-cardview": "^2.0.2",
    "nativescript-ui-sidedrawer": "~15.2.0",
    "rxjs": "~7.8.0",
    "zone.js": "~0.13.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~16.2.0",
    "@angular/cli": "^17.3.2",
    "@angular/compiler-cli": "~16.2.0",
    "@nativescript/android": "8.6.2",
    "@nativescript/schematics": "^11.2.0",
    "@nativescript/types": "~8.6.0",
    "@nativescript/webpack": "~5.0.18",
    "@ngtools/webpack": "~16.2.0",
    "tslint": "^5.20.1",
    "typescript": "~5.1.6"
  }
}

angular.json

{
    "version": 1,
    "cli": {
        "defaultCollection": "@nativescript/schematics"
    },
    "projects": {
        "appname": {
            "root": "",
            "sourceRoot": "src",
            "projectType": "application",
            "prefix": "ns"
        }
    },
    "defaultProject": "appname"
}

优化方案

1. 只打包必要的CPU架构

默认NativeScript会打包4种架构,但大部分安卓设备仅需arm64-v8a和armeabi-v7a,x86架构主要用于模拟器。在nativescript.config.ts中指定架构:

export default {
  // 其他配置
  android: {
    abiFilters: ['armeabi-v7a', 'arm64-v8a']
  }
}

此操作可直接减少两个.so文件的体积,预计节省40MB左右。

2. 启用Webpack生产环境压缩

确保构建时使用生产模式,Webpack会自动压缩代码与资源。执行构建命令:

ns build android --env.production

也可检查webpack.config.js确认压缩配置:

const { composePlugins, withNativescript } = require('@nativescript/webpack');
module.exports = composePlugins(withNativescript(), (config) => {
  if (config.mode === 'production') {
    config.optimization.minimize = true;
  }
  return config;
});

3. 移除非核心依赖

检查@nativescript/pdf、nativescript-ui-sidedrawer等插件,确认是否为必需项:

  • 若侧边栏仅需基础功能,可替换为NativeScript原生RadSideDrawer或自行实现
  • 若PDF功能非核心,暂时移除后观察体积变化

4. 切换为AAB格式构建

Android App Bundle(AAB)是Google推荐格式,用户下载时仅获取对应设备架构的文件,实际下载体积会大幅降低。构建命令:

ns build android --env.production --aab

5. 移除调试符号

生产构建时确保关闭调试符号,在nativescript.config.ts中配置:

export default {
  android: {
    debuggable: false,
    removeDebugSymbols: true
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 07:17:09