如何定位 app is not installed 错误

这两天尝试着去了解下 Android 软件安全。我在 AVD 上一个 apk 无法被安装, 应该是与 apk 本身没有关系,因为这个 apk 就是我从豌豆荚上下载下来的,点击下一步后,直接提示 app is not installed ,在 Google 上搜索了一翻,为什么会出现这个问题,SO 上有提到一些原因,但是都不是我这里的情况。我重新搜索“app is not installed where is log”,这个问题 下面的一个回答提示了我使用

adb logcat

于是,使用上面命令,log 里面很清晰明显的暴露了问题所在。

感谢,logcat ,这是一只好猫。

Yii2 AssetBundle js 和 css 没有自动更新的问题

最近几天看了很多基于 Yii2 的一些开源项目。很多功能都在可以找到开源的以 Yii2 extension 的形式存在,集成到自己的项目中相当简单。这么说吧,基于这些组件,你可以写很少或者几乎不用写一行代码就可以搭建起来一套很好用的程序。常见的富文本编辑器、权限管理、导航栏、菜单等等这些都有很多不错的扩展可以直接集成。之前我一直以为PHP程序源码不含图片的话超过10M就很恐怖了,但最近几天接触的这些基于 Yii2 的项目,composer install 之后整个源码目录大小超过100M很常见。当然,这还没有 node 恐怖,npm install 之后,随随便便 200M+ 更常见。

以上是题外话。前段时间我就发现了如题的问题。那时没注意。今天仔细读了下相关源码,弄明白了这个问题的根源。

假设有如下 HelloAsset

<?php
namespace app\assets;

use yii\web\AssetBundle;

class HelloAsset extends AssetBundle
{

    public $sourcePath = '@app/hello_assets';

    public $js = [
        'hello.js',
        'js/hello2.js',
    ];

}

在视图文件里面注入这个 Bundle

<?php

\app\assets\HelloAsset::register($this);

?>

<h1>Hello, world!</h1>

问题表现为:当我们修改 hello.js 后,刷新页面,加载的是我们修改过后的最新的 js。但是当我们修改 js/hello2.js 的内容后,刷新页面,发现最新的 js 并没有出现在浏览器中。

一路追踪代码到 AssetManager.php 里面有个 hash 方法,这个方法会生成 bundle 对应的 assets 目录,代码如下:

    /**
     * Generate a CRC32 hash for the directory path. Collisions are higher
     * than MD5 but generates a much smaller hash string.
     * @param string $path string to be hashed.
     * @return string hashed string.
     */
    protected function hash($path)
    {
        if (is_callable($this->hashCallback)) {
            return call_user_func($this->hashCallback, $path);
        }
        $path = (is_file($path) ? dirname($path) : $path) . filemtime($path);
        return sprintf('%x', crc32($path . Yii::getVersion()));
    }

上面方法的参数 $path 就是 AssetBundle::$sourcePath 应用 Yii::getAlias 得到的路径。可以看到这个方法的逻辑很清晰,如果没有配置 hashCallback,默认生成 hash 与 $path 的最近一次修改时间相关。怀疑问题出现在这里。

linux 下用 stat 命令可以获取文件的最近访问更改以及改动时间:

$ stat hello_assets 
  文件:"hello_assets"
  大小:4096      	块:8          IO 块:4096   目录
设备:fc00h/64512d	Inode:1321721     硬链接:3
权限:(0775/drwxrwxr-x)  Uid:(  900/ vagrant)   Gid:(  900/ vagrant)
最近访问:2016-03-16 17:46:07.011451000 +0800
最近更改:2016-03-16 17:46:06.995443000 +0800
最近改动:2016-03-16 17:46:06.995443000 +0800
创建时间:-

当我更改 hello.js 的内容后再次运行 stat 命令:

$ stat hello_assets 
  文件:"hello_assets"
  大小:4096      	块:8          IO 块:4096   目录
设备:fc00h/64512d	Inode:1321721     硬链接:3
权限:(0775/drwxrwxr-x)  Uid:(  900/ vagrant)   Gid:(  900/ vagrant)
最近访问:2016-03-16 17:56:31.279429000 +0800
最近更改:2016-03-16 17:56:31.271425000 +0800
最近改动:2016-03-16 17:56:31.271425000 +0800
创建时间:-

很明显,最近改动时间已经变了,这点是没问题的。

当我更改 js/hello2.js 的内容后再运行 stat:

$ stat hello_assets            
  文件:"hello_assets"
  大小:4096      	块:8          IO 块:4096   目录
设备:fc00h/64512d	Inode:1321721     硬链接:3
权限:(0775/drwxrwxr-x)  Uid:(  900/ vagrant)   Gid:(  900/ vagrant)
最近访问:2016-03-16 17:56:31.279429000 +0800
最近更改:2016-03-16 17:56:31.271425000 +0800
最近改动:2016-03-16 17:56:31.271425000 +0800
创建时间:-

可以看到这时 stat hello_assets 的最近改动时间并没有变化。

所以得出结论,当我们更改 $sourcePath 下面再下一级目录下的文件时,$sourcePath 目录的 filemtime 时间并不会改变,因此 hash 函数生成的哈希值也就没有变化。进一步,AssetManager 就没有再进行 publish 资源文件。

解决这个问题的一个方案就是更换生成哈希的实现。我做了一个简单的实现,在配置文件里面 components 项配置 assetManager 的 hashCallback 属性

$config = [
    /*...*/
    'components' => [
        /*...*/
        'assetManager' => [
            'hashCallback' => function ($path) {

                if (!function_exists('_myhash_')) {
                    function _myhash_($path) {
                        if (is_dir($path)) {
                            $handle = opendir($path);
                            $hash = '';
                            while (false !== ($entry = readdir($handle))) {
                                if ($entry === '.' || $entry === '..') {
                                    continue;
                                }
                                $entry = $path . '/' . $entry;
                                $hash .= _myhash_($entry);
                            }
                            $result = sprintf('%x', crc32($hash . Yii::getVersion()));
                        } else {
                            $result = sprintf('%x', crc32(filemtime($path) . Yii::getVersion()));
                        }
                        return $result;
                    }
                }

                return _myhash_($path);
            }
        ],
        /*...*/
    ],
    /*...*/
];

 

Is this php’s bug?

I am reading yii2’s source code right now. I found that this method yii\di\Instance::ensure something werid。

I changed the code as following:

public static function ensure($reference, $type = null, $container = null)
{
// I added three lines
     $container = null;
     var_dump(get_class($container));
     die;
// original code
}

I got output as :

string(15) “yii\di\Instance”

Can anyone explain?

#Answer

Just now(2016-04-12 15:20:58),I asked Laruence about this questing in qq group.  He answered me that get_calss(null) will return current scope. Then I read the php documetation , It says:

If object is omitted when inside a class, the name of that class is returned.

So, RTFM si right.

配置 nginx 访问 http 自动跳转 https

昨晚在 wosign 申请了个2年的 ssl 证书,很快就部署好了。

今天配置了下,让访问 http 的 url 自动跳转到 https。

相关配置如下即可:

listen 80;
listen 443 ssl;
ssl_certificate /PATH_TO_CRT;
ssl_certificate_key /PATH_TO_KEY;
server_name upliu.net;
if ($scheme != https) {
    rewrite ^/(.*) https://$server_name/$1 permanent;
}

同时监听 80 和 443 端口,并且给 443 端口加上 ssl 支持。当访问不是通过 https 过来的,则重定向到 https 的路径。

compile php with openssl on mac osx error

从源码手动编译 PHP 时出现如下错误:

Undefined symbols for architecture x86_64:
  "_PKCS5_PBKDF2_HMAC", referenced from:
      _zif_openssl_pbkdf2 in openssl.o
  "_TLSv1_1_client_method", referenced from:
      _php_openssl_setup_crypto in xp_ssl.o
  "_TLSv1_1_server_method", referenced from:
      _php_openssl_setup_crypto in xp_ssl.o
  "_TLSv1_2_client_method", referenced from:
      _php_openssl_setup_crypto in xp_ssl.o
  "_TLSv1_2_server_method", referenced from:
      _php_openssl_setup_crypto in xp_ssl.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libs/libphp5.bundle] Error 1

解决办法

MakeFile 里面找到类似下面这一行:

EXTRA_LIBS = -lresolv -lmcrypt -lltdl -liconv-lm -lxml2 -lcurl -lssl -lcrypto

删除所有的 -lssl 和 -lcrypto 然后添加 libssl.dylib 和 libcrypto.dylib 的路径(如果你安装了 brew,那么则是 /usr/local/opt/openssl/lib/),重新运行 make 命令,done。

附上我修改后的 MakeFile EXTRA_LIBS 那一行:

EXTRA_LIBS = -lz -lresolv -lmcrypt -lltdl -lstdc++ -liconv -liconv -lpng -lz -lcurl -lz -lm -lxml2 -lz -licucore -lm -lcurl -lxml2 -lz -licucore -lm -licui18n -licuuc -licudata -licuio -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm /usr/local/opt/openssl/lib/libssl.dylib /usr/local/opt/openssl/lib/libcrypto.dylib

 

蜗牛工作室介绍

蜗牛工作室承接各类web程序开发以及数据抓取分析类需求。

整站开发类需求报价不低于五千,如果您的预算低于五千请另寻他人。

设计定稿后蜗牛工作室开始开发并每隔一段时间(例如每隔一周)同步最新开发成果给客户演示。在此期间客户可以提出细微调整需求(如部分细节颜色,宽高度调节),如果更改涉及大的前端页面改动或者后台功能设计,客户需要为此额外付费。蜗牛工作室对开发完成的程序提供一次免费的安装部署服务。蜗牛工作室对开发完成的程序承诺永久免费 bug 修复。

蜗牛部分客户案例展示如下:

  1. 贝美玩具
  2. 深圳市维度设计有限公司

查看图片大小色度等信息的命令 identify

安装 ImageMagick 包后会有一个命令

identify

使用该命令即可查看图片详情。

例如:

identify example.png

输出类似如下:

example.png PNG 1020×2754 1020×2754+0+0 8-bit sRGB 750KB 0.000u 0:00.000

如果想看更加详细的输出,则加上 -verbose 选项:

identify -verbose example.png

输出比较多:

Image: example.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 1020×2754+0+0
Resolution: 28.35×28.35
Print size: 35.9788×97.1429
Units: PixelsPerCentimeter
Type: TrueColorAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit
Channel statistics:
Pixels: 2809080
Red:
min: 0 (0)
max: 255 (1)
mean: 204.674 (0.802644)
standard deviation: 81.414 (0.319271)
kurtosis: 0.239677
skewness: -1.38498
Green:
min: 0 (0)
max: 255 (1)
mean: 207.794 (0.81488)
standard deviation: 79.2734 (0.310876)
kurtosis: 0.665358
skewness: -1.53063
Blue:
min: 0 (0)
max: 255 (1)
mean: 210.003 (0.823541)
standard deviation: 78.877 (0.309322)
kurtosis: 0.861659
skewness: -1.61903
Alpha:
min: 255 (1)
max: 255 (1)
mean: 255 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 155.618 (0.610266)
standard deviation: 69.163 (0.271228)
kurtosis: 6.56095
skewness: -2.02349
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 1020×2754+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2015-03-16T16:31:31+08:00
date:modify: 2015-03-16T16:31:31+08:00
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 1020, 2754
png:pHYs: x_res=2835, y_res=2835, units=1
png:sRGB: intent=0 (Perceptual Intent)
signature: 6a369e9590b4ae45f4fde49cf23b5c60b1ab3e71a5f90e31e8bd88277f527c6b
Artifacts:
filename: example.png
verbose: true
Tainted: False
Filesize: 750KB
Number pixels: 2.809M
Pixels per second: 25.54MB
User time: 0.100u
Elapsed time: 0:01.109
Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-23 http://www.imagemagick.org

附上不同系统安装 ImageMagick 的命令:

yum install ImageMagick # CentOS
apt-get install ImageMagick # Debian/Ubuntu
brew install ImageMagick # Mac with brew