ionic cordova build --prod --release 失败并产生错误

Posted

技术标签:

【中文标题】ionic cordova build --prod --release 失败并产生错误【英文标题】:ionic cordova build --prod --release fails and generate errors 【发布时间】:2019-06-27 04:54:47 【问题描述】:

我正在尝试为 android 平台构建我的 ionic 3 项目。我正在使用 Windows 10。当我运行以下命令时:ionic cordova build android --prod --release,我收到此错误:

在堆限制附近的无效标记压缩分配失败 - javascript 堆内存不足

这是与错误对应的屏幕截图

这是 ionic info 命令的结果

Ionic:

   ionic (Ionic CLI)  : 4.7.0 (C:\Users\HOLLY SPIRIT\AppData\Roaming\npm\node_modules\ionic)    Ionic Framework    : ionic-angular 3.9.2    @ionic/app-scripts : 3.2.1

Cordova:

   cordova (Cordova CLI) : 7.1.0    Cordova Platforms     : android
6.4.0    Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 1.2.1, (and 22 other plugins)

System:

   NodeJS : v10.15.0 (C:\Program Files\nodejs\node.exe)    npm    :
6.4.1    OS     : Windows 10

这是package.json

的内容

"name": "app name",
  "version": "2.0.1",
  "author": "auhtors name",
  "homepage": "website url",
  "private": true,
  "scripts": 
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve"
  ,
  "dependencies": 
    "@angular/animations": "5.2.10",
    "@angular/common": "5.2.10",
    "@angular/compiler": "5.2.10",
    "@angular/compiler-cli": "5.2.10",
    "@angular/core": "5.2.10",
    "@angular/fire": "^5.0.0",
    "@angular/forms": "5.2.10",
    "@angular/http": "5.2.10",
    "@angular/platform-browser": "5.2.10",
    "@angular/platform-browser-dynamic": "5.2.10",
    "@ionic-native/admob-free": "^4.7.0",
    "@ionic-native/admob-pro": "^4.19.0",
    "@ionic-native/app-version": "^4.8.0",
    "@ionic-native/barcode-scanner": "^4.19.0",
    "@ionic-native/browser-tab": "^4.19.0",
    "@ionic-native/camera": "^4.7.0",
    "@ionic-native/core": "4.7.0",
    "@ionic-native/facebook": "^4.19.0",
    "@ionic-native/file": "^4.8.0",
    "@ionic-native/file-transfer": "^4.7.0",
    "@ionic-native/firebase": "^4.13.0",
    "@ionic-native/google-plus": "^5.0.0",
    "@ionic-native/in-app-browser": "^4.7.0",
    "@ionic-native/instagram": "^4.19.0",
    "@ionic-native/local-notifications": "^4.13.0",
    "@ionic-native/network": "^4.7.0",
    "@ionic-native/open-native-settings": "^4.7.0",
    "@ionic-native/social-sharing": "^4.7.0",
    "@ionic-native/splash-screen": "4.7.0",
    "@ionic-native/status-bar": "^4.7.0",
    "@ionic-native/youtube-video-player": "^4.7.0",
    "@ionic/storage": "2.1.3",
    "angular-pipes": "^8.0.0",
    "angularfire2": "^5.1.1",
    "cordova-admob-sdk": "^0.17.0",
    "cordova-android": "^7.1.4",
    "cordova-instagram-plugin": "^0.5.7",
    "cordova-ios": "^4.5.5",
    "cordova-open-native-settings": "^1.5.2",
    "cordova-plugin-add-swift-support": "^1.7.2",
    "cordova-plugin-admob-free": "^0.17.4",
    "cordova-plugin-admobpro": "^2.37.1",
    "cordova-plugin-app-version": "^0.1.9",
    "cordova-plugin-badge": "^0.8.7",
    "cordova-plugin-camera": "^4.0.3",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-document-viewer": "^0.9.10",
    "cordova-plugin-extension": "^1.5.4",
    "cordova-plugin-facebook4": "^4.1.0",
    "cordova-plugin-file": "^6.0.1",
    "cordova-plugin-file-transfer": "^1.7.1",
    "cordova-plugin-firebase": "^2.0.2",
    "cordova-plugin-inappbrowser": "^3.0.0",
    "cordova-plugin-ionic-keyboard": "^2.1.3",
    "cordova-plugin-ionic-webview": "^3.1.0",
    "cordova-plugin-local-notification": "^0.9.0-beta.2",
    "cordova-plugin-network-information": "^2.0.1",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "cordova-plugin-x-socialsharing": "^5.4.4",
    "cordova-plugin-youtube-video-player": "^1.0.6",
    "cordova-promise-polyfill": "0.0.2",
    "cordova-universal-clipboard": "git+https://github.com/Visigo/CordovaClipboard.git",
    "es6-promise-plugin": "^4.2.2",
    "firebase": "^5.8.2",
    "ionic-angular": "3.9.2",
    "ionic-img-viewer": "^2.9.0",
    "ionicons": "3.0.0",
    "moment": "^2.22.2",
    "ng2-pdf-viewer": "^5.1.0",
    "ng2-simple-timer": "^1.3.3",
    "ngx-moment": "^3.1.0",
    "phonegap-plugin-barcodescanner": "^8.0.0",
    "rxjs": "5.5.11",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.26"
  ,
  "devDependencies": 
    "@ionic/app-scripts": "^3.2.1",
    "@ionic/lab": "1.0.18",
    "typescript": "~2.6.2"
  ,
  "description": "app description",
  "cordova": 
    "plugins": 
      "cordova-plugin-admob-free": ,
      "cordova-plugin-x-socialsharing": 
        "ANDROID_SUPPORT_V4_VERSION": "24.1.1+"
      ,
      "cordova-instagram-plugin": ,
      "cordova-plugin-network-information": ,
      "cordova-plugin-camera": ,
      "cordova-plugin-file-transfer": ,
      "cordova-open-native-settings": ,
      "cordova-plugin-inappbrowser": ,
      "cordova-plugin-youtube-video-player": ,
      "cordova-plugin-app-version": ,
      "cordova-plugin-whitelist": ,
      "cordova-plugin-device": ,
      "cordova-plugin-splashscreen": ,
      "cordova-plugin-ionic-webview": ,
      "cordova-plugin-ionic-keyboard": ,
      "phonegap-plugin-barcodescanner": 
        "ANDROID_SUPPORT_V4_VERSION": "27.+"
      ,
      "cordova-plugin-file": ,
      "cordova-plugin-document-viewer": ,
      "cordova-plugin-statusbar": ,
      "cordova-plugin-local-notification": ,
      "cordova-plugin-firebase": ,
      "cordova-universal-clipboard": ,
      "cordova-plugin-admobpro": 
        "PLAY_SERVICES_VERSION": "16.0.0"
      ,
      "cordova-plugin-facebook4": 
        "APP_ID": "appid",
        "APP_NAME": "237actu",
        "FACEBOOK_HYBRID_APP_EVENTS": "false",
        "FACEBOOK_ANDROID_SDK_VERSION": "4.38.1"
      ,
      "cordova-plugin-googleplus": 
        "REVERSED_CLIENT_ID": "app reverse client id"
      
    ,
    "platforms": [
      "android",
      "ios"
    ]
  

为了解决这个问题,我尝试了两件事:

    我在 package.json 的 scripts 部分添加了 node --max-old-space-size=2048,如下所示:

    “脚本”: "clean": "ionic-app-scripts clean", "build": "node --max-old-space-size=2048 ionic-app-scripts build", "lint": "ionic-app-scripts lint", "ionic:build": "ionic-app-scripts build", “离子:服务”:“离子应用脚本服务” ,

没有用。

    我编辑了我电脑的ionic.cmd文件如下:

    @IF EXIST "%~dp0\node.exe" (
       "%~dp0\node.exe"  "%~dp0\node_modules\ionic\bin\ionic" %*
    ) 
    ELSE (
        @SETLOCAL
    
        @SET PATHEXT=%PATHEXT:;.JS;=;% **node --max-old-space-size=2048**  "%~dp0\node_modules\ionic\bin\ionic" %*)
    

如您所见,我刚刚添加了 --max-old-space-size=2048,但它不起作用。我的计算机 RAM 3 984 MB

    我还尝试在构建之前将 firebase 降级到 4 版本,但没有成功。

    我使用以下命令更新了项目的所有依赖项:npm update -f

    我从我的项目 pluginsplatformsnodes_modules 中删除了文件夹,并且还删除了 package-lock。 json 来自我的项目。我在构建之前运行了项目中的命令: npm 安装ionic cordova 平台添加 android

这些解决方案都不适合我。

有什么帮助吗?

【问题讨论】:

试试这个ionic-team/ionic#16868 (comment) 谢谢@ViniciusLourenço,但会产生同样的错误。 【参考方案1】:

终于找到了适合我的解决方案。

使用此命令从项目中删除 android 平台:

ionic cordova platform rm android

将android平台添加到项目中:ionic cordova platform add android@latest,就我而言,我使用的是最新版本的android平台。

@angular-devkit/build-optimizer@0.6.8在项目中的添加:

npm install @angular-devkit/build-optimizer@0.6.8 --save

这个角度插件将优化项目的构建。

该命令在项目中的运行:cordova clean

现在当我运行 ionic cordova build android --prod --release 时它成功了。

【讨论】:

【参考方案2】:

试试这个

* add this to the package.json scripts
  `"ionic:build": "node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build",`
* run the command:
  `npm run ionic:build --prod`
* run the commands:
  `cordova build android --release`
  `cordova build ios --release`

【讨论】:

感谢您的回答,但是当我运行命令时:npm run ionic:build --prod 按照您上面指定的方式编辑 package.json 文件后,该命令即使在 1 小时后也不会结束,没有任何反应,也没有显示任何错误

以上是关于ionic cordova build --prod --release 失败并产生错误的主要内容,如果未能解决你的问题,请参考以下文章

ionic cordova build 显示 lint 警告

ionic cordova build android 报错 解决

Ionic Cordova Build Android 期间面临错误

ionic cordova build android - 失败

使用 CMD 行构建失败 --ionic cordova build android

安装cordova push插件后,Ionic build ios失败