应用程序安装大小如何随手机而变化?

时间:2013-07-12 14:26:24

标签: android animation view dialog size

我最近为经常修改设备的root用户制作了此application

现在我注意到应用程序的安装大小非常奇怪。

当我使用从XML文件初始化视图的常规活动时,大小为715 kb

 @Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.main_layout);
    executeEvents = new ExecuteEvents(this);
    display = (TextView) findViewById(R.id.display);
    powermenu = (Button) findViewById(R.id.powermenu);
    turnoffscreen = (Button) findViewById(R.id.turnscreenoff);
    mapButton = (ImageButton) findViewById(R.id.mapButton);
    SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
    if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
        display.setText(getResources().getString(R.string.configured));
    mapButton.setOnClickListener(this);
    powermenu.setOnClickListener(this);
    turnoffscreen.setOnClickListener(this);
}

切换到创建的对话框后,设置包含XML文件中小部件的视图。应用程序大小现为200kb

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    //setContentView(R.layout.main_layout);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.main_layout, null);
    executeEvents = new ExecuteEvents(this);
    display = (TextView) view.findViewById(R.id.display);
    powermenu = (Button) view.findViewById(R.id.powermenu);
    turnoffscreen = (Button) view.findViewById(R.id.turnscreenoff);
    mapButton = (ImageButton) view.findViewById(R.id.mapButton);
    SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
    if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
        display.setText(getResources().getString(R.string.configured));
    mapButton.setOnClickListener(this);
    powermenu.setOnClickListener(this);
    turnoffscreen.setOnClickListener(this);

    Dialog dialog = new Dialog(this);
    dialog.setContentView(view);
    dialog.setTitle(getResources().getString(R.string.app_name));
    dialog.show();
}

当我使用它时,它减少到80kb

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.main_layout);
    executeEvents = new ExecuteEvents(this);
    display = (TextView) dialog.findViewById(R.id.display);
    powermenu = (Button) dialog.findViewById(R.id.powermenu);
    turnoffscreen = (Button) dialog.findViewById(R.id.turnscreenoff);
    mapButton = (ImageButton) dialog.findViewById(R.id.mapButton);
    SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
    if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
        display.setText(getString(R.string.configured));
    mapButton.setOnClickListener(this);
    powermenu.setOnClickListener(this);
    turnoffscreen.setOnClickListener(this);
    dialog.setTitle(getString(R.string.app_name));
    dialog.show();
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            dialog.dismiss();
            MainActivity.this.finish();
        }
    });
}

当我尝试使用最后一种代码(80kb一种)添加动画时,我还注意到应用程序大小发生了另一个奇怪的变化。应用程序大小变得很大1 MB。这是我初始化动画的方法,并在单击按钮时尝试调用它:

private static final ScaleAnimation animation = new ScaleAnimation(1, .95f, 1, .95f, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);//declared as global variable

在onCreate方法中:

animation.setDuration(1000);
animation.setFillAfter(false);

之后我在onClickListener

中调用了它
mapButton.startAnimation(animation);

为什么在我没有添加任何新资源但只更改了代码样式时应用程序大小发生了如此大的变化?我在初始化对话框中存在的小部件的方式中存在如此巨大的差异?当我按照添加动画的方式添加动画时,为什么会有巨大差异呢?

跟进问题:

 @Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.main_layout);
    executeEvents = new ExecuteEvents(this);
    display = (TextView) this.findViewById(R.id.display);
    powermenu = (Button) this.findViewById(R.id.powermenu);
    turnoffscreen = (Button) this.findViewById(R.id.turnscreenoff);
    mapButton = (ImageButton) this.findViewById(R.id.mapButton);
    SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
    if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
        display.setText(getResources().getString(R.string.configured));
    mapButton.setOnClickListener(this);
    powermenu.setOnClickListener(this);
    turnoffscreen.setOnClickListener(this);
}

思索

这可能是由于导入的包或类造成的吗?至少对问题的animation位有意义。

添加视图初始化的上下文会减少内存使用吗?

1 个答案:

答案 0 :(得分:9)

是的,您的猜测接近正确,因此您可能知道您的所有Java代码首先通过说javac编译为Java字节码,然后dx将它们转换为Dalvik字节码,您的dalvik字节码文件(又名dex)打包在你的apk中(顺便说一句apk大小并没有变化,因为zip压缩帮助)通常为classes.dex,稍后当你在你的设备中安装apk时,Android可能会为特定设备优化它(又名odex)。

因此要验证您的最终缓存dex是否实际更改并且负责此大小更改,只需添加动画,您可以通过解压缩apk或在设备内找到它,我总能找到第二种选择更令人兴奋:

所以这是你的dex没有动画(你的80kb):

http://i.imgur.com/2JkpWS3.png

您的应用信息:

http://i.imgur.com/HseGJih.png

所以这是你的dex动画片(你的1 MB):

http://i.imgur.com/pyrthYd.png

您的应用信息:

http://i.imgur.com/FSVPWAC.png

通过仔细查看dalvik字节码常量池,您可以发现它们真的与众不同:

http://i.imgur.com/02mqaif.png

由于某些原因,我没有得到如此激进的尺寸差异,但它是明显的,我使用gradle + Android Studio构建你的应用程序,所以我认为他们帮助和优化了一些东西。

最后,您可以使用ProGuard但是嘿!有些k现在并不是那么多,我不担心会给你的应用添加一些“胖”,只要它变得更好,当然总有改进的余地但还记得你不是在java4k或东西那样:))