安卓减小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等等直接进行设置。

欢迎大家关注我,会一直保持更新,最少一周一篇。

猜你喜欢

转载自blog.csdn.net/haojiagou/article/details/81127372