1. 程式人生 > >安卓減小apk的體積,整體優化程式碼

安卓減小apk的體積,整體優化程式碼

百度到的方法都太普通,還是通過as自帶的功能來實現吧,有兩種方法。

1、首先生成apk檔案,點選Build->Build Apk(s),然後再點選Build->Analyze Apk...會生成如下介面。可以直觀的看出到底是那一部分佔的記憶體大。

2、點選Analyze->Inspect Code...會生成如下介面。下面一條一條的來說。。。

a、Android 

This inspection highlights unallowed XML tags in Android resource files and AndroidManifest.xml 

此檢查突出顯示了Android資原始檔和AndroidManifest.xml中不允許的XML標記。

b、1、Accessibility in Custom Views  可訪問性的自定義檢視

If a View that overrides onTouchEvent or uses an OnTouchListener does not also implement performClick and call it when clicks are detected, the View may not handle accessibility actions properly. Logic handling the click actions should ideally be placed in View#performClick as some accessibility services invoke performClick when a click action should occur.
如果覆蓋onTouchEvent或使用OnTouchListener的檢視在檢測到單擊時也沒有實現performClick並呼叫它,那麼檢視可能不能正確地處理可訪問性操作。處理單擊操作的邏輯應該放在view# performClick中,因為當發生單擊操作時,一些可訪問性服務呼叫performClick。

b、2、Image without contentDescription 沒有內容描述的影象

Non-textual widgets like ImageViews and ImageButtons should use the contentDescription attribute to specify a textual description of the widget such that screen readers and other accessibility tools can adequately describe the user interface.  Note that elements in application screens that are purely decorative and do not provide any content or enable a user action should not have accessibility content descriptions. In this case, just suppress the lint warning with a tools:ignore="ContentDescription" attribute.  Note that for text fields, you should not set both the hint and the contentDescription attributes since the hint will never be shown. Just set the hint.

像ImageViews和ImageButtons這樣的非文字小部件應該使用contentDescription屬性來指定小部件的文字描述,以便螢幕閱讀器和其他可訪問性工具能夠充分描述使用者介面。注意,應用程式螢幕中的元素是純裝飾的,不提供任何內容或使使用者操作不應該具有可訪問性內容描述。在這種情況下,只需使用工具來抑制lint警告:忽略="ContentDescription"屬性。注意,對於文字欄位,不應該同時設定提示和contentDescription屬性,因為提示永遠不會顯示。只是設定提示。

b、3、Keyboard inaccessible widget 鍵盤訪問小部件

A widget that is declared to be clickable but not declared to be focusable is not accessible via the keyboard. Please add the focusable attribute as well.

被宣告為可點選但未宣告為可調焦的小部件不能通過鍵盤訪問。請新增可調焦屬性。

b、4、Missing accessibility label 丟失的可訪問性標籤

Editable text fields should provide an android:hint or, provided your minSdkVersion is at least 17, they may be referenced by a view with a android:labelFor attribute.

可編輯的文字欄位應該提供一個android:提示或者,如果您的minSdkVersion至少是17,那麼可以使用android:labelFor屬性來引用它們。

c、1、Extraneous text in resource files 資原始檔中的無關文字

Layout resource files should only contain elements and attributes. Any XML text content found in the file is likely accidental (and potentially dangerous if the text resembles XML and the developer believes the text to be functional)

佈局資原始檔應該只包含元素和屬性。檔案中發現的任何XML文字內容都可能是偶然的(如果文字類似於XML,並且開發人員認為文字是功能性的,則可能是危險的)

c、2、Hardcoded reference to /sdcard 硬編碼/ sdcard引用

Your code should not reference the /sdcard path directly; instead use Environment.getExternalStorageDirectory().getPath().  Similarly, do not reference the /data/data/ path directly; it can vary in multi-user scenarios. Instead, use Context.getFilesDir().getPath().

您的程式碼不應直接引用/sdcard路徑;而不是使用Environment.getExternalStorageDirectory().getPath()。同樣,不要直接引用/data/data/ path;它可以在多使用者場景中變化。相反,使用Context.getFilesDir().getPath()。

c、3、Layout Inflation without a Parent 沒有父母的佈局膨脹

 When inflating a layout, avoid passing in null as the parent view, since otherwise any layout parameters on the root of the inflated layout will be ignored. 

在膨脹佈局時,避免將null作為父檢視傳遞,否則膨脹佈局的根上的任何佈局引數都將被忽略。

c、4、Mismatched Styleable/Custom View Name 不匹配Styleable /自定義檢視的名稱

The convention for custom views is to use a declare-styleable whose name matches the custom view class name. The IDE relies on this convention such that for example code completion can be offered for attributes in a custom view in layout XML resource files.  (Similarly, layout parameter classes should use the suffix _Layout.)

自定義檢視的約定是使用一個宣告式樣式表,其名稱與自定義檢視類名匹配。IDE依賴於這種約定,例如,可以為佈局XML資原始檔中的自定義檢視中的屬性提供程式碼補全。(類似地,佈局引數類應該使用字尾_Layout。)

c、5、Missing commit() calls  失蹤的commit()呼叫

After creating a FragmentTransaction, you typically need to commit it as well

建立碎片事務之後,通常還需要提交它

c、6、Nested scrolling widgets 巢狀的滾動視窗小部件

A scrolling widget such as a ScrollView should not contain any nested scrolling widgets since this has various usability issues

滾動小部件(比如ScrollView)不應該包含任何巢狀滾動小部件,因為這有各種可用性問題

c、7、Obsolete Gradle Dependency  過時Gradle依賴

This detector looks for usages of libraries where the version you are using is not the current stable release. Using older versions is fine, and there are cases where you deliberately want to stick with an older version. However, you may simply not be aware that a more recent version is available, and that is what this lint check helps find.

這個檢測器查詢您正在使用的版本不是當前穩定版本的庫的用法。使用舊版本是可以的,而且在某些情況下,您故意希望堅持使用舊版本。但是,您可能根本沒有意識到有更新的版本可用,這就是lint check幫助查詢的內容。

下面太多了,就不一個一個寫了,特別要注意Android Lint:Performance中的Unused resources未使用的資源,這裡面能讓你知道有多少資源未使用,能大大較小apk的大小。不過每個都能對程式碼進行優化。還有像未使用的欄位,或者方法等等。還有就是佈局一定要進行優化,佈局不能瞎弄。要考慮好。圖片加文字之家通過drawablestart,drawableend,drawableright等等直接進行設定。

歡迎大家關注我,會一直保持更新,最少一週一篇。