Skip to content
Ider

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

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

Framework

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 →

2015-06-30
30 June
On June 30, 2015
In Data Structures(数据结构), Design Patterns(设计模式), Knowledge Base(心得笔库), Mobile Development(移动开发)

Android自定义View中MeasureSpec浅析

Android的API为开发者提供了很多好用的widget和control,但在开发中依然会需要自定义一些额外的控件来满足特定的需求。当自定义的视图比较复杂时,就会跟onMeasure(int, int)、onLayout(boolean, int, int, int, int)、onDraw(android.graphics.Canvas)这些方法打交道。要在新创建自定义视图类里正确重载实现这些方法,就需要对这些方法的作用,参数意义有所了解。

本文就来对onMeasure(int, int)方法涉及到的MeasureSpec来做个简单介绍,讲讲它的值的含义,每个状态量都是在什么情况下形成,又该如何使用这些值。
Read More →

2015-04-16
16 April
On April 16, 2015
In Design Patterns(设计模式), Front Interface(界面构想), Knowledge Base(心得笔库), Mobile Development(移动开发)

RecyclerView体验简介

前些日子,组里为了在目前的Android程序里实现基于ListView子项的动画效果,希望将最新的RecyclerView引入到程序中,于是我便花了一些时间研究了一下RecyclerView的基本情况。本文算是对这些日子里了解的内容做一些汇总。

在网上关于RecyclerView的基本使用方式已经有了比较详细介绍,而且其设计结构也类似于ListView,所以本文将不重点介绍如何使用,在文末的引用中都可以相关内容。这里主要是介绍RecyclerView的基本功能、设计理念,以及系统提供API的情况。

什么是RecyclerView

RecyclerView是在Android L(也就是后来的Lollipop)中新加入的一种ViewGroup。但因为它使以support-v7库的形式加入到Android系统中,所以不仅仅是Lollipop版本以后的Android系统可以使用,只要系开发项目中引入这个库就在任意API级别中使用。不过查阅其文档,可以感受到RecyclerView是如此的强烈的“还在完善中”的感觉,因为对它的介绍只有短短的一句话:

A flexible view for providing a limited window into a large data set.

单看这句话,其实已经被经常使用的ListView和GridView基本也是满足这句描述的。而且从单呈现效果来看RecyclerView和ListView、GridView并没有大多的差别。

不过它的“flexible”并不单单指它可以在ListView和GridView之间随意互相切换,更在于它可以创造出更多的复杂的可滚动的视图,比如水平方向的ListView,或者是Web上很流行的瀑布流式布局(Masonry)。只是大部分布局系统都没有提供,必须由开发者自己实现。

所以RecyclerView的“flexible”:什么都可以做,但什么都要自己做。
Read More →

2014-09-30
30 September
On September 30, 2014
In Data Structures(数据结构), Design Patterns(设计模式), Knowledge Base(心得笔库), Mobile Development(移动开发)

了解Android中的Preference结构的设计与实现

之前的文章讲到Preference和Setting在本质上是相似的,并相信介绍了SharedPreferences的基本使用方式和其代码设计。但SharedPreferences只能算是设备背后的不可见的存储那一部分,而可见的界面其实对于用户来说更加重要,而这一部分就是本文要讲的那些在android.preference包下的各种Preference。

不过本文不会涉及这些Preference的使用方式,比如如何定义XML文件、如何使用PreferenceActivity和PreferenceFragment加载设置,这些都可以在Android Developer的官方指南里了解到详情。本文主要通过分析源代码来分享Preference的设计和实现方式,让开发者们在今后更加顺手地使用和扩展Preference类,或者在设计其他类似的界面和功能时可以提供参考帮助。

Preference概览

Android的设置界面本质上就是ListView:PreferenceActivity是继承了ListActivity;而3.0以后推荐使用的PreferenceFragment虽然没有继承ListFragment,但也定义了ListView字段。

settings

Read More →

2014-08-20
20 August
On August 20, 2014
In Design Patterns(设计模式), Knowledge Base(心得笔库), Mobile Development(移动开发)

了解Android API中的SharedPreferences

Preference翻译为偏好,但实际上它可以算是Setting(设置)的别名。两种叫法给人的差异在于针对的对象不同:设置更倾向于设备的属性,修改设置便是改变设备的功能;偏好则倾向于用户的性格,用户基于其个人的偏好来来形成设备的差异化。但是总体而言,他们是一致的,都是通过用户的需求改变设备的体验。

在Android的开发过程中,会在使用的API中见到很多名字中带有Preference的类和接口,此篇文章就来介绍一下这些“*Prefere*”的功能和用途。

在Android提供API中,带有Preference的其实主要分为两类:一类是android.content包下的SharedPreferences,另一类则是android.preference包下的Preference。它们分别实现不同功能,却又相互联系合作完成对Android程序的控制。

SharedPreferences简介

SharedPreferences是以复数形式存在,因为在Android中它是用来存储键值对(Key-Value Pair)数据的集合。API中包含了多个方法来方面读取相应类型的数据:

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

String getString(String key, String defValue);
Set<String> getStringSet(String key, Set<String> defValues);
int getInt(String key, int defValue);
long getLong(String key, long defValue);
float getFloat(String key, float defValue);
boolean getBoolean(String key, boolean defValue);

[/codesyntax]
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.

2013-11-04
04 November
On November 4, 2013
In Data Structures(数据结构), Knowledge Base(心得笔库), Language Tips(语言初试), Mobile Development(移动开发)

JavaScriptCore框架在iOS7中的对象交互和管理

之前一篇的文章中已经简单入门了iOS7中新加的JavaScriptCore框架的基本用法,十分的简单方便而且高效,不过也仅限于数值型、布尔型、字符串、数组等这些基础类型。本文将扩展到更复杂的类型,介绍一下该强大的框架是如何让Objective-C对象和JavaScript对象进行直接互通的。

为了方便起见,以下所有代码中的JSContext对象都会添加如下的log方法和eventHandler:

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

JSContext *context = [[JSContext alloc] init];
context.exceptionHandler = ^(JSContext *con, JSValue *exception) {
    NSLog(@"%@", exception);
    con.exception = exception;
};

context[@"log"] = ^() {
    NSArray *args = [JSContext currentArguments];
    for (id obj in args) {
        NSLog(@"%@",obj);
    }
};

[/codesyntax]
Read More →

2013-10-05
05 October
On October 5, 2013
In Design Patterns(设计模式), Language Tips(语言初试), Mobile Development(移动开发)

iOS7新JavaScriptCore框架入门介绍

前阵子,Apple正式发布了新的iOS 7系统,最大最直观的改变在于界面变得小清新范了,我也提到《iOS,你真的越来越像Android了》。不过对于移动开发者来说,除了要适应Xcode 5,最应该关注的还是iOS 7在开发接口的上的变化。概览Apple提供的官方文档《What’s New in iOS》,最最让我欣喜的是iOS 7中加入了JavaScriptCore框架。该框架让Objective-C和JavaScript代码直接的交互变得更加的简单方便。

JavaScriptCore

这个框架其实只是基于webkit中以C/C++实现的JavaScriptCore的一个包装,在旧版本iOS开发中,很多开发者也会自行将webkit的库引入项目编译使用。不过虽然iOS7把它当成了标准库,可惜目前,我还没有在Apple Developer中找到像之前文章中收集的那样的官方文档介绍这个框架的具体使用方法。

好在还可以在Xcode中找到头文件,而且里面的注释对每个类和方法的功能写得还算清楚,再结合网上仅有的几篇文章的介绍,我也在此简单入门一下JavaScriptCore。更多细节还待开发过程中发现。
Read More →

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.