1. 程式人生 > >Android進階之用起來很卡,解決方法

Android進階之用起來很卡,解決方法

最近開始安裝使用android studio,痛苦的發現安裝或者後期升級後as執行時非常的卡。於是,深入的研究解決方法並不斷的嘗試,最後總結出如下的兩點。

一、工具軟體配置
1、解決網路連線問題

這裡寫圖片描述

(1)問題描述
檢查你的 Android SDK,卡上很長時間,需要更新則需要進行安裝。

(2)方法步驟
①跳過這一步,可在Android Studio安裝目錄下的 bin 目錄下,找到 idea.properties 檔案,在檔案最後追加disable.android.first.run=true 。
②或者:使用牆外代理。

2、解決記憶體吃緊問題
(1)問題描述
Android Studio 安裝目錄的-xmx 引數是 Java 虛擬機器啟動時的引數,用於限制最大堆記憶體。Android Studio 啟動時設定了這個引數,並且預設值很小。 一旦你的工程變大,IDE 執行時間稍長,記憶體就開始吃緊,頻繁觸發 GC,自然會卡。

(2)方法步驟
每次升級/安裝 Android Studio 之後都修改android-studio/bin/studio.vmoptions studio64.vmoptions 兩個檔案的以下屬性:
-Xms2048m //JVM啟動的起始堆記憶體
-Xmx2048m //AndroidStudio能使用的最大heap記憶體
-XX:MaxPermSize=2048m //最大的Permanent generation大小。存放的事類本身(不是物件),以及方法,一些固定的字串等等。
-XX:ReservedCodeCacheSize=2048m //設定JIT java compiler在compile的時候的最大程式碼快取

3、解決構建速度慢問題
(1)問題描述
隨著專案的增大,依賴庫的增多,構建速度越來越慢,現在最慢要6分鐘才能build一個release的安裝包

(2)方法
開啟gradle單獨的守護程序,增大gradle執行的java虛擬機器的大小,讓gradle在編譯的時候使用獨立程序,讓gradle可以平行的執行。

(2)步驟
①在下面的目錄下面建立gradle.properties檔案:C:\Users\.gradle (Windows)

②在檔案中增加:org.gradle.daemon=true

③優化以上使用者目錄下的gradle.properties檔案,配置如下:

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users: # Settings specified in this file will override any Gradle settings # configured through the IDE. # For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html # The Gradle daemon aims to improve the startup and execution time of Gradle. # When set to true the Gradle daemon is to run the build. # TODO: disable daemon on CI, since builds should be clean and reliable on servers org.gradle.daemon=true # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. # Default value: -Xmx10248m -XX:MaxPermSize=256m org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects org.gradle.parallel=true # Enables new incubating mode that makes Gradle selective when configuring projects. # Only relevant projects are configured which results in faster builds for large multi-projects. # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand org.gradle.configureondemand=true

二、升級硬體
雖然對工具進行配置,但是需要更佳的使用者體驗,還是需要升級硬體。推薦升級配置為:I5+8G和128GSSD,甚至更高。

總結,經過如上的配置和升級,AS的執行速度明顯提升,開啟軟體只需4-6s,Gradle只需4-6s。