2017-04-13 45 views
0

我的ionic2應用程序需要15秒才能在設備中啓動。我嘗試了所有的建議(縮小,醜化,enableProd,--prod --release等) 我最後的APK是4.3MBIonic2性能問題

的應用程序沒有任何圖像除了從圖標,然後從splashcreen,它有更多的少20個屏幕,而只是文字和列表和按鈕(這是一個支付應用程序,所以沒有什麼奇怪)

我的package.json是:

{ 
    "name": "xxxxx", 
    "author": "xxxx", 
    "homepage": "http://ionicframework.com/", 
    "private": true, 
    "scripts": { 
    "ionic:build": "ionic-app-scripts build", 
    "ionic:serve": "ionic-app-scripts serve" 
    }, 
    "dependencies": { 
    "@angular/common": "2.2.1", 
    "@angular/compiler": "2.2.1", 
    "@angular/compiler-cli": "2.2.1", 
    "@angular/core": "2.2.1", 
    "@angular/forms": "2.2.1", 
    "@angular/http": "2.2.1", 
    "@angular/platform-browser": "2.2.1", 
    "@angular/platform-browser-dynamic": "2.2.1", 
    "@angular/platform-server": "2.2.1", 
    "@ionic/cloud-angular": "^0.8.0", 
    "@ionic/storage": "1.1.6", 
    "@ngtools/webpack": "1.1.9", 
    "angularfire2": "^2.0.0-beta.6", 
    "firebase": "^3.6.4", 
    "ionic-angular": "2.0.0-rc.4", 
    "ionic-native": "2.2.11", 
    "ionicons": "3.0.0", 
    "rxjs": "5.0.0-beta.12", 
    "zone.js": "0.6.26" 
    }, 
    "devDependencies": { 
    "@ionic/app-scripts": "^1.1.0", 
    "@ngtools/webpack": "^1.1.9", 
    "@types/request": "0.0.30", 
    "ionic-minify": "^2.0.10", 
    "typescript": "2.0.9" 
    }, 
    "cordovaPlugins": [ 
    "cordova-plugin-whitelist", 
    "cordova-plugin-statusbar", 
    "cordova-plugin-splashscreen", 
    "cordova-plugin-device", 
    "ionic-plugin-keyboard", 
    "phonegap-plugin-push" 
    ], 
    "cordovaPlatforms": [], 
    "description": "xxxxx: An Ionic project" 
} 

你看到任何問題與相關性或與插件嗎?

最小化的main.js文件是5.8MB。

我app.module是:

import { NgModule, ErrorHandler } from '@angular/core'; 
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular'; 
import { MyApp } from './app.component'; 
import { NextPayments } from './payment/nextPayments/nextPayments'; 
import { Login } from './user/login/login'; 
import { SignUp } from './user/signup/signup'; 
import { Terms } from './user/terms/terms'; 
import { Contactus } from './user/contactus/contactus'; 
import { ChangeEmail } from './user/changeEmail/changeEmail'; 
import { ChangePassword } from './user/changePassword/changePassword'; 
import { ResetPassword } from './user/resetPassword/resetPassword'; 
import { PaymentInformation } from  './payment/paymentInformation/paymentInformation'; 
import { UnidadesList } from './payment/unidadesList/unidadesList'; 
import { UnidadInformation } from  './payment/unidadInformation/unidadInformation'; 
import { PaymentConfirmation } from './payment/paymentConfirmation/paymentConfirmation'; 
import { PaymentHistory } from './payment/paymentHistory/paymentHistory'; 
import { OneClickPayment } from './payment/oneClickPayment/oneClickPayment'; 
import { HistoryInformation } from './payment/historyInformation/historyInformation'; 
import { AddPayment } from './payment/addPayment/addPayment'; 
import { AccountList } from './account/accountList/accountList'; 
import { AddAccount } from './account/addAccount/addAccount'; 
import { AccountInformation } from './account/accountInformation/accountInformation'; 
import { EqualValidator } from './directives/equalValidator'; 
import { AngularFireModule } from 'angularfire2'; 
import { CloudSettings, CloudModule } from '@ionic/cloud-angular'; 

export const firebaseConfig = { 
    apiKey: "xxxxxx", 
    authDomain: "xxxxxxxx", 
    databaseURL: "xxxxxxxxxx", 
    storageBucket: "xxxxxxxx", 
    messagingSenderId: "xxx" 
} 

const cloudSettings: CloudSettings = { 
    'core': { 
    'app_id': 'xxxxx' 
    }, 
    'push': { 
    'sender_id': 'xxxxx', 
    'pluginConfig': { 
     'ios': { 
     'badge': true, 
     'sound': true 
     }, 
     'android': { 
     'iconColor': '#921F67' 
     } 
    } 
    } 
}; 

@NgModule({ 
    declarations: [ 
    MyApp, 
    NextPayments, 
    PaymentInformation, 
    Login, 
    PaymentConfirmation, 
    AddPayment, 
    EqualValidator, 
    AccountList, 
    AddAccount, 
    HistoryInformation, 
    Terms, 
    Contactus, 
    PaymentHistory, 
    AccountInformation, 
    UnidadInformation, 
    UnidadesList, 
    SignUp, 
    ResetPassword, 
    OneClickPayment, 
    ChangeEmail, 
    ChangePassword 
    ], 
    imports: [ 
    IonicModule.forRoot(MyApp, { 
     monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre' ], 
     monthShortNames: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic' ], 
     dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves',  'Viernes', 'Sabado' ], 
     dayShortNames: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab' ], 
    }), 
    AngularFireModule.initializeApp(firebaseConfig), 
    CloudModule.forRoot(cloudSettings) 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
    MyApp, 
    NextPayments, 
    PaymentInformation, 
    Login, 
    UnidadInformation, 
    UnidadesList, 
    PaymentConfirmation, 
    AddPayment, 
    Terms, 
    Contactus, 
    AccountList, 
    AddAccount, 
    AccountInformation, 
    HistoryInformation, 
    PaymentHistory, 
    OneClickPayment, 
    SignUp, 
    ResetPassword, 
    ChangeEmail, 
    ChangePassword 
    ], 
    providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}] 
}) 
export class AppModule {} 

而且我app.components:

import { Component, ViewChild } from '@angular/core'; 
import { Nav, Platform, AlertController, IonicApp, ToastController,  MenuController } from 'ionic-angular'; 
import { StatusBar, Splashscreen } from 'ionic-native'; 

import { NextPayments } from './payment/nextPayments/nextPayments'; 
import { UnidadesList } from './payment/unidadesList/unidadesList'; 
import { PaymentHistory } from './payment/paymentHistory/paymentHistory'; 
import { PaymentInformation } from './payment/paymentInformation/paymentInformation'; 
import { Login } from './user/login/login'; 
import { ChangeEmail } from './user/changeEmail/changeEmail'; 
import { OneClickPayment } from './payment/oneClickPayment/oneClickPayment'; 
import { ChangePassword } from './user/changePassword/changePassword'; 
import { Contactus } from './user/contactus/contactus'; 
import { Terms } from './user/terms/terms'; 
import { Response } from './models/response'; 
import { Payment } from './models/payment'; 
import { Session } from './session/session'; 
import { Push, PushToken } from '@ionic/cloud-angular'; 
import { AccountList } from './account/accountList/accountList'; 
import { UserService } from '../app/user/userService'; 
import { ServerService } from './server/server.service'; 
import { LoadingController } from 'ionic-angular'; 
import { AccountService } from './account/accountService'; 
import { PaymentService } from './payment/paymentService'; 

declare var navigator: any; 

@Component({ 
    templateUrl: 'app.html', 
    providers: [Session, UserService, ServerService, PaymentService, AccountService] 
}) 
export class MyApp { 
    @ViewChild(Nav) nav: Nav; 
    payment : Payment = new Payment(); 
    rootPage: any = Login; 
    backButtonPressed: boolean = false; 
    pages: Array<{title: string, component: any}>; 

    constructor(public loadingCtrl : LoadingController, public paymentService  : PaymentService, public accountService : AccountService, public menuCtrl: MenuController, public toastCtrl: ToastController, private ionicApp : IonicApp, public platform: Platform, public serverService: ServerService, public session: Session, public push: Push, public userService: UserService, public alertCtrl: AlertController) { 
    this.initializeApp(); 
    if(this.session.isAuth()) 
     this.rootPage = NextPayments; 
    // used for an example of ngFor and navigation 
    this.pages = [ 
     { title: 'Próximos Pagos', component: NextPayments }, 
     { title: 'Mis Unidades', component: UnidadesList }, 
     { title: 'Mis C.B.U.', component: AccountList }, 
     { title: 'Historial de Pagos', component: PaymentHistory }, 
     { title: 'Cambiar Email', component: ChangeEmail }, 
    { title: 'Cambiar Contraseña', component: ChangePassword }, 
    { title: 'Términos y Condiciones', component: Terms }, 
    { title: 'Contáctenos', component: Contactus }, 
    // { title: 'Configuración', component: Settings } 
]; 

this.push.rx.notification() 
    .subscribe((msg) => { 
     let loader = this.loadingCtrl.create({ 
     content: "Obteniendo información..." 
     }); 
     loader.present(); 
     if(msg.payload != undefined && msg.payload != null){ 
     let payload : any = msg.payload; 
     if(payload.id != ""){ 

       this.paymentService.getPayments(payload.id).then((result : Response) => { 
       if(result.success){ 
        if(result.eror!= null && result.eror == "id invalido"){ 
        this.session.clearSession(); 
        this.nav.setRoot(Login); 
        } 
        else{ 
        if(result.data!= null && result.data.expensas.length > 0){ 
         this.payment = result.data.expensas[0]; 
         this.payment.date = this.payment.diahabil; 
         if(this.payment.diahabil > this.payment.date1 && this.payment.date2.toString() != "0000-00-00") 
          this.payment.suggested = this.payment.importe2; 
         else 
          this.payment.suggested = this.payment.importe1; 
         this.accountService.getAccounts().then((result : Response) => { 
          if(result.success){ 
          if(result.data.accounts != null && result.data.accounts.length > 0){ 
           loader.dismiss(); 
           this.payment.account = result.data.accounts[0]; 
           this.nav.setRoot(OneClickPayment, { 
           item : this.payment 
           }); 
          } 
          else{ 
           loader.dismiss(); 
           this.payment.account = result.data.accounts[0]; 
           this.nav.setRoot(PaymentInformation, { 
           item : this.payment 
           }); 
          } 
          } 
         }); 
        } 
        } 
       } 
       }); 
     } 
     } 
     loader.dismiss(); 
     this.nav.setRoot(NextPayments); 
     }); 
    } 

    readPushNotification(){ 
     this.push.register().then((t: PushToken) => { 
     return this.push.saveToken(t); 
     }).then((t: PushToken) => { 
     this.session.setPnToken(t.token); 
     }); 
    } 

    registerBackButtonAction() { 
    this.platform.registerBackButtonAction(() => { 
     let activePortal = this.ionicApp._modalPortal.getActive(); 
     if (activePortal) { 
     activePortal.dismiss().catch(() => {}); 
    activePortal.onDidDismiss(() => {}); 
    return; 
    } 
    if(this.menuCtrl.isOpen()){ 
    this.menuCtrl.close(); 
    } 
    else{ 
    let view = this.nav.getActive(); 
    if(view.component.name == "NextPayments" || view.component.name == "Login") 
     this.showExit(); 
    else 
      return view._nav.canGoBack() ? view._nav.pop() :   this.goBacktoDefaultPage(); 
     } 
    }, 1); 
    } 

    goBacktoDefaultPage(){ 
     if(this.session.isAuth()) 
     this.nav.setRoot(NextPayments); 
    } 
    showExit() { 
    if (this.backButtonPressed) { 
     this.platform.exitApp(); 
    } else { 
     this.toastCtrl.create({ 
     message: 'Presione nuevamente para cerrar la aplicación.', 
     duration: 2000, 
     position: 'top' 
     }).present(); 
     this.backButtonPressed = true; 
     setTimeout(() => this.backButtonPressed = false, 2000); 
    } 
    } 

    initializeApp() { 
     this.platform.ready().then(() => { 
     // Okay, so the platform is ready and our plugins are available. 
     // Here you can do any higher level native things you might need. 
     StatusBar.styleDefault(); 
     Splashscreen.hide(); 
     this.registerBackButtonAction(); 
     this.readPushNotification(); 
    }); 
    if(navigator['connection']['type'] == "none"){ 
     let alert = this.alertCtrl.create({ 
      subTitle: "No posee conexion a internet. La aplicación se cerrara. Por favor reintente cuando posea conectividad.", 
     buttons: [ 
     { 
     text: 'Aceptar', 
     handler:() => { 
      this.platform.exitApp(); 
     } 
     } 
     ] 
    }); 
     alert.present(); 
    } 
    } 

    openPage(page : any) { 
    // Reset the content nav to have just this page 
    // we wouldn't want the back button to show in this scenario 
    this.nav.setRoot(page.component); 
    } 

    logout(){ 
    this.session.clearSession(); 
    this.userService.setToken(null); 
    //UNREGISTER PUSH NOTIFICATION TOKEN 
    this.push.unregister(); 

    this.nav.setRoot(Login); 
    } 
} 

什麼建議嗎?你需要更多信息?這是我在Ionic2中的第一個應用程序,需要15秒才能加載。請幫助找到解決方案,我會投入大量時間在這個應用程序。


編輯:更新的package.json:

"@angular/animations": "^4.0.0", 
"@angular/common": "4.0.0", 
"@angular/compiler": "4.0.0", 
"@angular/compiler-cli": "4.0.0", 
"@angular/core": "4.0.0", 
"@angular/forms": "4.0.0", 
"@angular/http": "4.0.0", 
"@angular/platform-browser": "4.0.0", 
"@angular/platform-browser-dynamic": "4.0.0", 
"@angular/platform-server": "4.0.0", 
"@ionic-native/core": "3.4.2", 
"@ionic-native/push": "^3.4.4", 
"@ionic-native/splash-screen": "3.4.2", 
"@ionic-native/status-bar": "3.4.2", 
"@ionic/cloud-angular": "^0.12.0", 
"@ionic/storage": "2.0.1", 
"@ngtools/webpack": "1.3.0", 
"angularfire2": "^2.0.0-beta.8", 
"firebase": "3.7.8", 
"ionic-angular": "3.0.1", 
"ionicons": "3.0.0", 
"rxjs": "5.1.1", 
"sw-toolbox": "3.4.0", 
"zone.js": "^0.8.4" 
+0

,我也面臨着同樣的issue.Wait的離子團隊發佈更新優化版本。他們正在研究它。 –

+0

您是否有更多信息的鏈接?無論如何,有幾頁有建議(這對我來說不起作用),但也許是離子2.2.1的一個問題?你嘗試過更低的版本嗎? – Faabass

回答

1

嘗試人行橫道或人行橫道精簡版,看看性能如何is.The APK大小將由15-30 MB取決於你人行橫道增加使用,但您可以期望在不同設備上獲得統一的性能。

+0

但是我在不同設備上的性能沒有問題,應用程序加載時性能總是很慢。然後一切都很好。這將有助於加載應用程序的時間? – Faabass

+0

我嘗試了人行橫道,但人行橫道和鉻啓動時間沒有太大的區別。儘管人行橫道的動畫效果更好,但我正在使用的應用程序與您的相同,啓動時間約爲6-9秒(APK-4.7MB)。我使用Ionic 2.3和Ionic native 3.x,儘管在更新到最新版本之前啓動程序是相同的。 – Naveen

0

嘗試更新您的應用程序... 2.0.0-rc.4使用Ionic和Angular的過時版本。

您可以在這裏找到更多的信息:changelog Ionic2

我工作的一個大的應用程序有很多插件和圖像,並與ionic run android --prod,應用程序正在像3-4秒,在真實的設備上啓動(銀河S4 - 相對較舊)。

是一項單調乏味的工作,但遲早你必須這樣做......還添加了很多新功能!

這是我的package.json,給你一個想法:

"dependencies": { 
    "@angular/animations": "4.0.1", 
    "@angular/common": "4.0.1", 
    "@angular/compiler": "4.0.1", 
    "@angular/compiler-cli": "4.0.1", 
    "@angular/core": "4.0.1", 
    "@angular/forms": "4.0.1", 
    "@angular/http": "4.0.1", 
    "@angular/platform-browser": "4.0.1", 
    "@angular/platform-browser-dynamic": "4.0.1", 
    "@ionic-native/barcode-scanner": "3.4.4", 
    "@ionic-native/camera": "3.4.4", 
    "@ionic-native/core": "3.4.4", 
    "@ionic-native/facebook": "3.4.4", 
    "@ionic-native/google-plus": "3.4.4", 
    "@ionic-native/network": "3.4.4", 
    "@ionic-native/social-sharing": "3.4.4", 
    "@ionic-native/splash-screen": "3.4.4", 
    "@ionic-native/sqlite": "3.4.4", 
    "@ionic-native/status-bar": "3.4.4", 
    "ionic-angular": "3.0.0", 
    "ionicons": "3.0.0", 
    "jsbarcode": "3.5.9", 
    "moment": "2.18.1", 
    "rxjs": "5.3.0", 
    "sw-toolbox": "3.6.0", 
    "zone.js": "0.8.5" 
    }, 
    "devDependencies": { 
    "@ionic/app-scripts": "1.3.0", 
    "typescript": "2.2.2" 
    } 

最好的解決方案也許將開始一個新項目,並添加你的插件/ src目錄代碼。 With Ionic 2 v3.0.1(2017-04-06)應用程序啓動在生產模式下非常快速。

我希望能對你有所幫助。 :)

+0

謝謝!我使用最新版本創建了一個新應用程序並遷移了所有邏輯,但獲得了相同的結果。請檢查我的app.components.ts,看看有沒有什麼奇怪的...請注意initializeApp()方法。應用程序啓動後立即註冊推送通知可以嗎?另外,可以將所有的聲明和entryComponents放在app.module.ts中嗎? – Faabass

+0

我注意到應用程序一開始就打電話給https://api.ionic.io/insights ..是嗎?我測試了一個空的項目,它在3/4秒內加載,但我的並不是那麼複雜或很大,所以它應該沒有什麼相同之處。任何線索? – Faabass

+0

也許嘗試刪除一些功能1,並防止它被初始化,並檢查是否開始時間減少..所以,也許你可以找出什麼具體功能增加啓動時間,然後我們可以嘗試找到解決方案! ** initializeApp()**似乎確定,什麼是'api.ionic.io/insights'? – mosca90

0

使用延遲加載的頁面而不是聲明app.module.ts中的所有頁面。 在app.component.ts中將根頁面聲明爲字符串,而不是任何rootPage: String = HomePage。 此外,雖然聲明導航頁面只是聲明爲this.navigation.push("secondPage").我們不需要導入該頁面用於導航目的。

0

考慮使用你選擇的JS minifier涅槃你的代碼,我更喜歡uglify並通過gulp任務運行使用它

通過npm先安裝gulp-cli全球範圍內,然後再安裝然後在index.html文件中圍繞所有js引用的註釋,這些註釋將被useref用於合併一個文件中的所有js文件並替換它們的引用由一個參考

<!--build:js build/all.js --> 

<script src="build/vendor.js"></script> 
<script src="build/main.js"></script> 

<!-- endbuild --> 

然後在開發文件夾中創建的文件夾,發展一個gulpfile.js

var gulp = require("gulp"); 
var uglify = require("gulp-uglify"); 
var useref = require("gulp-useref"); 
var gulpIf = require("gulp-if"); 


gulp.task('build', function() { 

    gulp.src('www/index.html') 
     .pipe(useref()) 
     .pipe(gulpIf('*.js', uglify())) 
     .pipe(gulp.dest('www')); 

}); 

然後打開cmd並運行gulp build 的js文件將被合併和微細化以及在index.html中的引用將相應地更新

你也可以使用使用del插件刪除不需要的文件

編輯

傳遞--prod標誌構建命令利用名列前茅的時間編制的事先uglifies的JS文件,以建立包