Skip to content
Ider

沉淀我所学习,累积我所见闻,分享我所体验

Primary Navigation Menu
Menu
  • Home
  • About Ider
    • Who Ider?
    • Why Ider?
    • How Ider?
    • Where Ider?
    • What Ider?

Tutorial

2019-08-29
29 August
On August 29, 2019
In Data Structures(数据结构), Knowledge Base(心得笔库), Mobile Development(移动开发), Software Engineering(软件工程)

Gradle android-library 的各种坑

软件开发随着项目越来越大,我们会开始拆封出多个子项目(sub-project)或者库(library)来更好地独立维护。Android开发也不例外,我们也会需要创建各种库。如果使用Android Studio和Gradle,他们为我们提供了便捷的方式来创建libray。但是他们也带来了不少的维护问题,本文就来讲讲android-library里的坑来帮助大家避开。

android-library v.s. java-library

对于普通Android手机项目,我们有创建两种不同的Library可以选用: android-library 和 java-library

它们的gradle文件内容分别如下

[codesyntax lang=”groovy” lines=”normal” highlight_lines=”1″]

apply plugin: 'com.android.library'

android {
    // ...
}

dependencies {
    // ...
}

[/codesyntax]

[codesyntax lang=”groovy” lines=”normal” highlight_lines=”1″]

apply plugin: 'java-library'

sourceCompatibility = "7"
targetCompatibility = "7"

dependencies {
    // ...
}

[/codesyntax]

两种库在定义上的根本差别其实就是不同gradle plugin的使用。另一个小的差别就是android-library需要有一个 AndroidManifest.xml 文件,但它可以简单到只有一行内容(其中package的值通常是文件夹的路径)

[codesyntax lang=”xml” lines=”normal”]

<manifest package="com.ider.android.library" />

[/codesyntax]

使用上他们有一些差异,这些差异也是我们决定使用哪种Library的标准:

  • android-library 可以使用 Android API(比如Activity, Service等等),java-library 则不能使用
  • android-library 可以编译android资源文件(resources files),java-library 则无视这样内容
  • android-library 编译出 arr 文件,java-library编译出 jar 文件(本质上他们都是 zip 文件)
  • android-library 的配置可以包含各种编译类型(build variants)来控制,java-library没有那个复杂
  • android-library 可以依赖于另一个android-library 或者 java-library, java-library只能依赖于java-library而不能依赖于android-library

总体而言 android-library 基本是 java-library 的一个超集, 但 android-library 比 java-libray 使用起来要更加复杂一些。从上边的对比来看,对于Android的项目,因为我们不确定什么时候会需要调用 Android 的API,或者定义一下 Android 的资源内容。再者最后一条的约束也决定了创建的 java-library 要保证在整个依赖树(dependency tree)上要在接近叶子节点的位置。所以不如优先选择创建android-library,以免掉入之后改 build.gradle 的坑中。
Read More →

2016-10-04
04 October
On October 4, 2016
In Knowledge Base(心得笔库), Special Tricks(奇技妙招)

从Terminal截取Android设备的屏幕

做Android开发的时候,经常会需要截取设备的屏幕甚至录制设备屏幕,来让同事知道提交的代码实现的功能,或者让他们知道产品中出现来什么问题。截取Android设备的屏幕有很多方法,比如在Nexus系列同时按下“电源”和“音量调低”按钮来截取,也可以用第三方程序来完成。通过另一部手机来拍照录像也是可行的方案。但是这些方法还需要想办法将文件传输到电脑上,又要多一步麻烦的操作。

Mac电脑上也有一些应用程序可以截屏Android设备,但是我更喜欢用终端指令,令人开心的是Android自带的 adb 已经提供了截取屏幕和录制屏幕的指定:

[codesyntax lang=”bash” lines=”normal”]

$ adb shell screencap /sdcard/screen.png
$ adb shell screenrecord --verbose /sdcard/demo.mp4

[/codesyntax]

不过生成的文件依然保存在设备中,还需要用adb pull指定来转到电脑上

为了让整个过程更加的便捷,我将这些指令包裹在bash方法中,然后一个指令来完成截取和传输的工作。

截取Android设备当前屏幕并保存到执行指令的电脑目录上:

[codesyntax lang=”bash” lines=”normal”]

# capture screen of android device
andrdroidScreenCapture() {
    curTime=`date +%Y-%m-%d-%H-%M-%S`
    tmpeName="$curTime.png"
    [[ -n $1 ]] && fileName=$1 || fileName=$tmpeName
    devicePath="/sdcard/$tmpeName"
    adb shell screencap -p $devicePath
    adb pull $devicePath $fileName
    adb shell rm $devicePath
}

[/codesyntax]

录取Android设备屏幕活动,结束后将视频文件保存到执行指令的电脑目录上:

[codesyntax lang=”bash” lines=”normal”]

export ADB_SHELL_SCREENRECORD_ARGS='--verbose --bit-rate 2000000'
# record screen of android device
androidScreenRecord() {
    echo -e "\033[1m(press Ctrl-C to stop recording)\033[0m"
    curTime=`date +%Y-%m-%d-%H-%M-%S`
    tmpeName="$curTime.mp4"
    [[ -n $1 ]] && fileName=$1 || fileName=$tmpeName
    devicePath="/sdcard/$tmpeName"
    adb shell screenrecord $ADB_SHELL_SCREENRECORD_ARGS $devicePath
    sleep 1 # wait for video encoding finish
    adb pull $devicePath $fileName
    # Don't delete copy in device.
    # adb shell rm $devicePath
    open $fileName
}

[/codesyntax]

较短的指令别名,并通过提供的文件的扩展名来决定是截屏还是录制:

[codesyntax lang=”bash” lines=”normal”]

function asc() {
    if [[ -z $1 ]]; then
        echo "Please provide a filename."
        echo "Provideing .png extension for capturing the device screen, and providing .mp4 for recording the device screen."
        return
    fi

    if [[ $1 == *.png ]]; then
        andrdroidScreenCapture $1
    elif [[ $1 == *.mp4 ]]; then
        androidScreenRecord $1
    else
        echo "Filename with unknow extension, only .png and .mp4 are supported"
    fi
}

[/codesyntax]

完整脚本代码可以在Gist上找到,把它们加入到Mac电脑的~/.bash_profile中,连接上开启了“开发者模式”的Android设备就可以方便截图了。

http://blog.iderzheng.com/wp-content/uploads/2016/10/android-screen-capture-SD.mp4

 

References:
  1. Capture or record android screen, pull file to Mac when it’s completed
  2. ADB Shell Commands | Android Studio
  3. Android Debug Bridge | Android Studio
2016-07-31
31 July
On July 31, 2016
In Article Collection(聚宝收藏), Knowledge Base(心得笔库), Language Tips(语言初试)

编程术语鉴别-函数篇

常说“隔行如隔山”,每个行业都只有该业内人士才懂的术语。软件行业里也有很多别人听不懂的话,比如有个笑话就说:所有讲不清楚的过程都叫做“算法(Algorithm)”。但其实即使身处在软件行业里,也任然有好多模棱两可的的概念,常常被混淆使用。就让本文来梳理一下这些概念的细微区别。

function vs. method

函数(function)和方法(method)在编程中的概念就很相近,经常被互换着称呼,但它们的区别主要是在定义的位置上。

function是一段通过名字调用的代码,它可以接受数据进行操作,并可能会有返回值。
method是一段通过关联在对象上的名字调用的代码。

从这段解释上看函数和方法基本一致,只是方法是属于对象的一部分,所以在面向对象语言中才有概念。如果是C语句,就只有函数的说法。

方法是附属于对象的,相对于函数可以接受传入参数,对象本身也会作为隐性参数可以在方法中被调用。在返回值方面,方法不仅可以选择返回数据,还可以将数据赋予其所属的对象。

(在我看来,计算机函数的概念应该与数学的“函数”概念是相通的,只是数学上的函数必须要输入和输出。如果考虑到计算机函数的参数的类型,以及异常(Exception)的情况,则感觉与数学的“映射”更接近了。)

References:

  • Difference between a method and a function – Stack Overflow
  • Subroutine – Wikipedia, the free encyclopedia
  • programming practices – Method vs Function vs Procedure – Programmers Stack Exchange

parameter vs. argument

parameter和argument都被翻译成“参数”,它们也经常被混淆。其实很好更精准的翻译应该是“形参(formal parameter)”和“实参(actual argument)”。

parameters是定义函数时定义的那些变量,它们是函数类型识别(type signature)的一部分;
arguments是调用函数时传入到函数中的那些变量,它们是函数的输入值。

这称为也是相对的,比如说下边的例子:
[codesyntax lang=”java” lines=”normal”]

void foo(int x, int y) {
    bar(x, y);
}

[/codesyntax]
x和y对于foo函数来说是形参(parameter),而对于bar来说是实参(argument)。因此也可以说argument是parameter的实例。

References:

  • Parameter (computer programming) – Wikipedia, the free encyclopedia
  • function – “Parameter” vs “Argument” – Stack Overflow
  • language agnostic – Difference between parameter and argument – Stack Overflow

declaration vs. definition

申明(declaration)和定义(definition)这两个概念应该缘起于C/C++,因为写C/C++程序会分头文件(header files)和实现文件(implementation files)。方法和变量的申明部分一般放在头文件,定义部分则在实现文件里。

所以很容易理解

declaration是描述变量、方法、类的类型标识,它没有具体的实现;编译器看到申明部分不会分配内存,而是从其它地方找到其实现的定义。
definition是真正实例化变量,实现方法和类。编译器看到定义部分时就知道如何分配内存,在运行时分配相应大小内存去存放内容。

比如下边这些代码都属于申明:

[codesyntax lang=”cpp” lines=”normal”]

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // no extern allowed for type declarations

[/codesyntax]

而下边的代码是对上述“申明”的具体“定义”:

[codesyntax lang=”cpp” lines=”normal”]

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};

[/codesyntax]

不严格的说“定义”也包含了“申明”的部分,应该单看定义中变量,方法和类的名字部分,那些可以算是“申明”。

 

在Java中并没有“申明”的概念,所有内容都是“申明”即“定义”。如果实在要说细分“申明”,不严格的讲可以把接口(interface)和抽象方法(abstract method)看做“申明”。但实际中我们还是会说“定义接口”,“定义抽象函数”。

References:

  • c++ – What is the difference between a definition and a declaration? – Stack Overflow
  • What’s the difference between declaring and defining in C and C++ – Cprogramming.com
  • What is the difference between declaration and definition in Java? – Stack Overflow
2016-03-31
31 March
On March 31, 2016
In Article Collection(聚宝收藏), IT Products(数码产品), Special Tricks(奇技妙招)

让Mac开口说话

《实用Mac的Dictionary应用程序》已经帮助我们翻译英文单词,提高了阅读的能力,但是“听”和“说”还是大问题。发音要是不标准,念出来给别人听可能还是得不到理解。Mac也想到了这个问题,所以自带了发音程序来,当然机器念出来的英语还是会有不流畅的感觉,有些发音也可能不到位。

在许多原生Mac应用程序中,都很好的集成了发音选项,只要选取想要电脑阅读的段落,然后选择“Start Speaking”就可以听到朗读开始了。
speech-on-page

在System Preference里也可以为Speech设置快捷键,这样无论在哪里选中文字都可以让电脑来阅读:
speech-shortcut
Read More →

2016-01-20
20 January
On January 20, 2016
In Article Collection(聚宝收藏), IT Products(数码产品)

一些实用的在线课程的网站

现在的学习已经不仅仅局限于课堂之上,也不光来自于工作之中,借着在线教育网站(Massive open online course, MOOC)的崛起,越来越多的人通过在线学习了补充自身的知识,提高各个方面的能力。之前分享过《一些视频教程网站推荐》,这里再分享一些非常有用的提供在线课程的网站,可以在上下班路上或者平时休息时播放观看。

Udacity

Udacity

Udacity主要是编程开发技术类的在线课程,而且这些课程的由诸如Google、Facebook这些大公司提供,所以质量也很高。这些视频大部分需要付费才能观看,但也有很多免费又值得学习的内容。我个人在上边获得了很多关于Android开发方面的能力提升。

对于付费用户,网站还会在完成某些课程内容后提供“Nanodegree”的荣誉来证明对某项知识已被掌握。

Udemy

Udemy

Udemy上的视频教程包罗万象,对于不满足于一个领域的知识的人,可以在上边了解其他领域的基础知识,比如房产投资,股票交易。

同样的,网站的大部分课程的需要付费,而且价格还不低,但是一些免费课程也非常的受欢迎。另外很多课程每节视频的长度都较短,所以很容易快速完成一节课程的学习。
Read More →

2015-10-21
21 October
On October 21, 2015
In Knowledge Base(心得笔库), Language Tips(语言初试)

Java关键字分类解析

在Java里有50个关键字和3个保留字,它们不仅限定开发者不能使用这些来作为代码中的标识(这些标识指包(package)名、类(class, interface, enum)名、方法(method)名、变量(variable, field, parameter)名),同时它们对代码也有控制作用。本文就对这些关键字做一些归类分组,让大家更好的了解它们的作用。

Group Category Keyword
类型
Types
原始类型
Primitive Type
boolean
byte
char
double
float
int
long
short
void
类相关
Class Type
class
enum
interface
特殊引用
Special Reference
super
this
结构
Structure
类结构
Class Struture
extends
implements
instanceof
new
包相关
Package
import
package
修饰符
Modifier
访问修饰符
Access Modifier
private
protected
public
基础修饰符
Basic Modifier
abstract
final
native
static
strictfp
synchronized
transient
volatile
控制
Control
流程控制
Flow Control
case
default
do
else
for
if
switch
while
流程中断
Flow Interruption
break
continue
return
异常处理
Exception Handling
assert
catch
finally
throw
throws
try
特殊保留
Reserved
文字值
Literal Value
null
true
false
未使用
Not Used
const
goto

Read More →

2014-12-31
31 December
On December 31, 2014
In Article Collection(聚宝收藏), Design Patterns(设计模式), Knowledge Base(心得笔库)

Chrome扩展程序各结构间交互方式

在之前的文章里介绍了很多关于开发Chrome扩展程序时会用到的各部件,对每个部件的功能和用途做了比较详细的描述。不过单单知道怎么使用各个部件还不够,前面有提到每个部件的优缺点:Browser Action和Page Action不能长期保留数据状态;Background Page和Event Page没有用户界面;Content Scripts无法触发DOM原本的事件。所以就需要让这些部件能够联系在一起使用,用其优势不足其它的劣势。

通过阅读网上关于Chrome扩展程序的文档和文章,以及自身的试验和实践,对于Chrome扩展程序中所用到的交互方式主要可以分为三类:调用接口(Interface),消息传送(Message),脚本注入(Injection)。

下图展示了各个部件直接交互的方式和会使用到的API方法:
Chrome-Extension-Interaction

话说回来其实三种方式都是调用Chrome扩展开发中直接提供的API,只是API的设计结构、使用方式、作用对象以及安全性等有些许差别,所以我才臆想成三类不同方式。

而从上图的分布,还可以看出我对于Chrome扩展程序各部件的分类可以归为:“近扩展(Close to Extension)”和“近页面(Close to Page)”。也就是一类主要作用在扩展程序上,另一类主要作用在页面本身。
Read More →

2014-11-08
08 November
On November 8, 2014
In Article Collection(聚宝收藏), Design Patterns(设计模式), Front Interface(界面构想), Knowledge Base(心得笔库)

Chrome扩展程序开发基础介绍

虽然现在的工作主要是Android平台的开发,但是对于Web的兴趣总是无法割舍。只可惜因为不是日常工作的一部分,许多Web技术其实学得都不精湛,也常常会淡忘了解过的知识。好在有了Chrome这个强大的前端神器,观察页面布局、学习脚本源码、调试代码样式都变得异常自如,也让学习Web变得方便很多。

前阵子为了完成一些功能,学习了一下Chrome扩展程序(Chrome Extensions)开发的基本内容和步骤,觉得为自己的Web技术找到了新的用武之地。不用再纠结和苦恼如果设计发开出功能全面界面吸引人的应用或系统;做出一些插件来扩展喝补强当前页面功能也是很有意义的。比如常看一些网页被旁边的广告栏打扰了,可以写个插件把display设成none;如果在线看一些文档,也可以做个插件用来标记哪些内容比较重要然后呈现时做个高亮显示。

开发Chrome扩展程序也非常容易上手,只要会HTML,CSS和JavaScript就可以开始了。不需要学习新的语言,也不需要了解新的布局方式,也不用知道新的IDE,还不用编译。当然Chrome扩展程序的基本架构和组成结构还是需要额外了解的,这样才能知道每个部分要如何实现,保证扩展程序能流畅运行。

Chrome扩展程序的官方文档让人感觉有点凌乱,可能是扩展程序开发算不上热门内容,也可能是Chrome的版本升级太频繁,所以没有很好地整理文档。本文就来总结这些知识内容与大家分享,也方便自己以后回顾。

入门准备和基础概念

既然是开发Chrome扩展程序,自然需要Chrome浏览器,然后下载一些示例程序。一般的Chrome扩展程序是.crx文件,但实质上它只是一个压缩文件,随时可以以zip格式来解压查看里面的内容。但是为了开发方便,Chrome也支持直接上传文件夹,但是要先打开”开发模式”。启用方式很简单,只要开打Chrome浏览器,在Extensions窗口下(可通过菜单开打,也可以在地址栏里输入chrome://extensions来进入)勾选Develop mode就可以了:
Developer-Mode

这样就可以直接上传文件夹来安装在开发中的扩展程序了。如果开发完成也通过这里进行打包然后发布到Chrome商店上,不过这部分不在本文中介绍。

对比直接上传的和从商店上安装的扩展程序,可以发现几个明显的不同:
Unpacked

以文件夹格式上传的,会显示Loaded from项来指示扩展程序在文件系统中的位置,另外还可以通过刷新或者点击Reload链接按钮来更新修改过的扩展程序。而安装的则无法这么简单的来更新和升级了。

当然其他的内容都是一样的:名字、版本号这些接下来会介绍如何定义;权限(Permissions)的内容比较多本文也不会涉及,可以在官方文档中了解。还有ID这一项,它是Chrome指定给每个扩展程序的唯一标示,可以用它在Chrome浏览器中通过链接chrome-extension://<extensionID>/<pathToFile>打开扩展程序中的任意文件。比如Evernote的选项页面就是:chrome-extension://pioclpoplcdbaefihamjohnefbikjilc/options.html
Evernote-Options

对于没有打包成.crx文件的扩展程序,Chrome会根据文件夹所在位置来指定生成ID;在第一次打包扩展程序时,Chrome会生成一个Private Key文件,之后就可以用同样的Key来标示同一个程序的版本升级,Chrome也就会赋予它同样的ID。之后也会介绍这个ID还有其他的用处。
Pack-Extension
Read More →

2014-03-31
31 March
On March 31, 2014
In Data Structures(数据结构), English Posts(英文写作), Language Tips(语言初试), Mobile Development(移动开发)

Introduction to Loc (Lua Objective-C)

Recently we were starting to use Lua in our game. Lua is a very popular scripting language in the game industry. It’s written in pure C and is very lightweight. We can simply add Lua source code to our project, and it is very easy to compile and run.

However, in practice we encountered issues with type conversion between Lua and Objective-C. In the beginning, we followed instructions to create C functions, and inside the functions we called Objective-C methods, then registered those functions into Lua State as library. When requirements went more complicated, we have to create a bunch of C functions, which are redundancy in a lot of places. This is really not an ideal to bridge between Lua and Objective-C.

After reading the Lua instructions for two days, and with knowledge about iOS7 JavaScript Framework, I decided to create a similar framework to handle type conversion between these two languages. The good thing is Objective-C is a superclass of C, and Lua is naturally compiled from C, this makes it very easy to construct a bridge and make them communicating with each other. With reflection from Objective-C, it’s also very convenient to call Objective-C methods from Lua.

Loc (Lua Objective-C) is the framework I created to convert type between Lua and Objective-C for our project. It’s as simple as Lua, with only 6 packages. It reduces a lot of time to create functions mapping Lua call, and it works pretty well so far.

With the permission from my company, I shared the slides that I presented to my colleagues about the design and syntax about Loc. I am trying to persuade my managers to make it open source, so more developers could use it to prove the usability and make the framework more mature.

2014-01-17
17 January
On January 17, 2014
In Article Collection(聚宝收藏), English Posts(英文写作), Language Tips(语言初试)

Redis Command Reference

Redis is really awesome key-value storage. Not only because it is very fast on data get/set as all data are stored in memory, but also it is very simple to setup even from source code, it has multiple data structures that can be used in practice, and it is well documented, many useful resources can be found in Redis.io.

The most valuable thing of Redis should be the commands that communicate with Redis server to operate on data. On Redis.io, there is page fully explained the functionality of each commands, it also provides some interactive page to try each command and know them well.

Here I restyle the commands page, so that they could be print out in two papers, and also highlighted them with different color according to their type. You can download the PDF file here as well.

Posts pagination

1 2 Next
Facebook
Twitter
LinkedIn
RSS
ZhiHu

Recent Posts

  • 三年居家工作感受
  • Pixel Watch智能手表和Pixel 5, 6 Pro 及 7 Pro手机
  • 我拥有过的无线耳机
  • 毕业工作一个月,我差点被开除
  • 我拥有过的移动硬盘
  • ProtoBuf 2.0 method count optimization for android development
  • 面过100场行为面试后

Categories

  • Algorithm Analysis(算法分析)
  • Article Collection(聚宝收藏)
  • Data Structures(数据结构)
  • Design Patterns(设计模式)
  • English Posts(英文写作)
  • Front Interface(界面构想)
  • IT Products(数码产品)
  • Knowledge Base(心得笔库)
  • Language Tips(语言初试)
  • Mathematical Theory(数学理论)
  • Mobile Development(移动开发)
  • Programming Life(程序人生)
  • Reading Notes(阅而后知)
  • Software Engineering(软件工程)
  • Special Tricks(奇技妙招)
  • Tangential Speech(漫话杂谈)

Tags

Aero Android API Bash Binary Search Bitwise Operation Book C/C++ Career Chrome Conference CSS Debug Device DOM Extension Framework Game Gradle Hearthstone HTML Initialization Intellij Interview iOS Java JavaScript jQuery Keyword Language Issues Mac Microsoft Mobile Modifier Objective-C PHP Principle Reference Regular Expression Static String Tools Tutorial UI XML

Blogroll

  • Ahmed's Blog
  • Gert Lombard's Blog
  • Gordon Luk
  • Jack & Allison
  • 开发部落

Archives

Designed using Chromatic. Powered by WordPress.