3. Goal
Android와 같은 S/W 플랫폼이 성장하고 널리 확산되는데 있어 SDK(Software
Development Kit)의 역할은 매우 큽니다. 아무리 좋은 기능과 성능을 제공하는
플랫폼이라 할지라도 그 위에서 동작하는 앱을 만드는 게 어렵다면 그 플랫폼은
개발자들로부터 외면 받게 됩니다. 이는 해당 플랫폼에서 이용 가능한 앱의 개수가 줄고
품질이 저하되는 결과를 만들게 되어 그 플랫폼은 사용자들에게도 외면 받게 됩니다. 이로
인해 개발자들이 다시금 그 플랫폼을 외면하게 만드는 악순환이 형성됩니다. 이러한
악순환을 끊기 위한 첫 걸음은 바로 개발 생산성을 높여주는 SDK를 잘 만들어 제공하는
것입니다. 따라서 Android 기술 분야에서 경쟁력을 확보하고 주도권을 갖기 위해서는
Android 플랫폼을 제대로 이해하는 것 뿐만 아니라 Android SDK를 제대로 분석하고
개선할 수 있어야 합니다.
이번 세션에서는 "Android SDK 기술의 이해"라는 큰 주제의 첫 꼭지로써 Android
UI(User Interface)를 디버깅하고 프로파일링하는데 사용하는 Hierarchy Viewer를
분석하고자 합니다. 먼저, Hierarchy Viewer를 분석하는데 필요한 몇몇 배경 지식을
설명한 후 Hierarchy Viewer의 내부 구조와 동작 방식을 살펴보겠습니다.
From the Conference Program Overview
The 9th Kandroid Conference
4. SDK (Software Development Kit)
A software development kit (SDK or "devkit") is typically a set of software
development tools that allows for the creation of applications for a certain
software package, software framework, hardware platform, computer system,
video game console, operating system, or similar platform.
It may be something as simple as an application programming interface (API)
in the form of some files to interface to a particular programming language or
include sophisticated hardware to communicate with a certain embedded
system. Common tools include debugging aids and other utilities often presented
in an integrated development environment (IDE). SDKs also frequently include
sample code and supporting technical notes or other supporting documentation
to help clarify points from the primary reference material.
From Wikipedia
Small No. of
Low Quality
Apps
Bad
SDK
Ignored by
Developers
VS.
Large No. of
Hig Quality
Apps
Ignored by
Users
Good
SDK
Attract
Developers
Attract
Users
The 9th Kandroid Conference
5. Android SDK & Hierarchy Viewer
SDK Tools contains tools for debugging and testing
your application and other utility tools.
SDK Platform-tools contains platform-dependent tools for
developing and debugging your application.
Documentation contains a local copy of the latest multi-version
documentation for the Android framework API.
Each SDK platform component includes a fully compliant
• Android library,
• system image (API Level <= 13),
• sample code (API Level <= 6), and
• emulator skins.
Hierarchy Viewer
• Related to graphics
• Complex enough to get insights on
how to develop a tool for Android
Samples contains the sample code and apps available
for each Android development platform.
http://developer.android.com/guide/developing/tools/index.html
The 9th Kandroid Conference
6. Hierarchy Viewer
List of Windows
Properties of a View
Hierarchy of Views in an Window
Layout of Views
The 9th Kandroid Conference
7. Hierarchy Viewer Architecture
4939
Another Target or Emulator
…
Hierarchy
Viewer
4940
4939
4939
Window
Window
Window
…
system server
View
View
View
…
user app.
View
Port Forwarding
View
adb forward tcp:4939 tcp:4939
View
…
user app.
View
ADB
Server
ADB
Daemon
View
View
…
user app.
USB|TCP
Host
Target|Emulator
The 9th Kandroid Conference
8. Roadmap
Questions
Answers
How to get information?
List of Windows
Window & View System 5
Hierarchy of Views in Window
Java Reflection
1
Properties of View
Java Annotations
2
Binder IPC
4
How to deliver information?
Between processes in target
Between host and target
Android Debug Bridge 3
The 9th Kandroid Conference
Hierarchy
Viewer
Internals
6
9. Reflection & Annotation in General
In computer science, reflection is
the process by which a computer
program can observe and
modify its own structure and
behavior at runtime.
Type, interface, class, methods, attributes,
variables, functions, control structures, etc.
Metadata
From Wikipedia
Meta Object
Meta Level
Set/Get
Metadata
(Value)
1
Add New
Metadata
(Type)
2
Application
Base Level
Base Object
Annotation
Reflection
1
Introspection
2
Intercession
In programming, annotations are used mainly for
the purpose of expanding code documentation
and comments. … as a special form of syntactic
metadata in the source code.
From Wikipedia
The 9th Kandroid Conference
13. Android Debug Bridge
adbd
Target
(USB mode)
cmd-line adb (in platform-tools)
adbd
ADB Server
DDMS (in tools)
Act as proxy between
clients & daemons
(multiplexing loop)
Target
(TCP mode)
adbd
Emulator
Hierarchy Viewer (in tools)
USB Connection
TCP Connection
ADB Clients
The 9th Kandroid Conference
ADB Daemons
14. ADB Architecture
ADB client
Service
Service
Service
5037
ADB client
USB|TCP
Service
Service
Service
ADB Server
adbd
…
fork
Service
adb port
console port
5555
Service
5554
…
5555
adbd
Guest (10.0.2.15)
Emulator
Host
Target
The 9th Kandroid Conference
15. Connection to Emulator
ADB Server
Emulator
Network Address in Emulator
Network Address
Description
10.0.2.1
Router/gateway address
10.0.2.2
Special alias to your host loopback interface
(i.e., 127.0.0.1 on your development machine)
10.0.2.3
First DNS server
10.0.2.4 / 10.0.2.5 / 10.0.2.6
Optional second, third and fourth DNS server (if any)
10.0.2.15
The emulated device's own network/ethernet interface
127.0.0.1
The emulated device's own loopback interface
The 9th Kandroid Conference
17. Providing “Simple” Host Service
<Request> := <Length><Payload>
• <Length> := Payload length, as 4-byte
hexadecimal string in ASCII
• <Payload> := Request data
ADB client
connect 3
10 “OKAY0004001d”
7 “000Chost:version”
fdevent
loop
5037
listen 2
8 read
4
alistener
[smart]
5
create
6
create
install_listener 1
main()
A listener is an entity which
• binds and listens to a local port,
• creates an asocket upon receiving a
connection on that port, and
• connect the new local connection to a
specific service.
asocket
[local]
accept
<Response> :=
• “OKAY”, for success
• “FAIL”<Length><Reason>, for failure
• “OKAY”<Length><Version>, for version query
• …
9
enqueue
asocket
[smart]
struct alistener {
alistener *next;
alistener *prev;
ADB Server
Host
fdevent fde;
int fd;
Phase 1: ADB Server Startup
const char *local_name;
const char *connect_to;
atransport *transport;
adisconnect disconnect;
Phase 2: Connection Establishment
Phase 3: Service Request/Response
The 9th Kandroid Conference
};
18. Providing “Not-so-simple” Host Service
An asocket represents one half of a connection
between local and remote entity.
• A local asocket is bound to a file descriptor.
• A remote asocket is bound to an atransport.
ADB client
1
<request>
15 <response>
11
struct asocket {
asocket *next;
asocket *prev;
<response>
unsigned id;
int closing;
asocket *peer;
14 write
7
2 read
“OKAY”
asocket
[local]
8
3
12
13
read
enqueue
enqueue
asocket
[smart]
9 close
asocket
[local]
6 create
write
create
5
fdevent fde;
int fd;
10
service
thread
apacket *pkt_first;
apacket *pkt_last;
4 create
int (*enqueue)(asocket *s, apacket *pkt);
void (*ready)(asocket *s);
void (*close)(asocket *s);
ADB Server
Host
void *extra;
atransport *transport;
};
Phase 3: Service Request
Phase 4: Service Response
* Phase 1 & 2 Omitted
The 9th Kandroid Conference
21. Changing Transport Mode: USB TCP
1
6
2
…
3
$setprop service.adb.tcp.port 5555
$stop adbd
$start adbd
4
2
5
The 9th Kandroid Conference
22. Changing Transport Mode: TCP USB
1
* Protocol fault error if USB not connected
2
$setprop service.adb.tcp.port -1
$stop adbd
$start adbd
* USB need to be re-connected
3
The 9th Kandroid Conference
23. Using Service Manager
Service
Manager
User Application Process
Binder Service
ActivityThread
Looper
main()
Message
Queue
Permission
Unique Name
HelloAndroid
Receiver
H
Handle Message()
Service
Provider
ViewRoot
Impl
Activity
Manager
Service
Activity
Handle Message()
1
activity:0x1
View
7
RUNTIME
Dalvik Virtual
Machine
5
LIBRARIES
9
0x0:1:activity
4
RUNTIME
Core Libraries
Binder
Context
Manager
6
L IBRARIES
8
Binder
Service
Core Libraries
Shared Memory
Driver
0x0000a000
activity:0x1
Custom
Impl.
10
LINUX KERNEL
System
Server
0x0:1:activity
Dalvik Virtual
Machine
3
LIBRARIES
0x0:3:activity:0x1
2
0x0:3:activity:0x0000a000
Binder (IPC) Driver
The 9th Kandroid Conference
From the 8th Kandroid Conference
24. Invoking Service
Binder Service
ActivityThread
Looper
main()
Message
Queue
Permission
Unique Name
HelloAndroid
Receiver
H
Handle Message()
Service
Provider
ViewRoot
Impl
Handle Message()
ActivityManager
Service
IActivityManager
3 : startActivity
…
[parcel]
Activity
activity:0x1
View
1
Core Libraries
L IBRARIES
Binder
Thread
Pool
Binder
Service
Binder
Context
Manager
Dalvik Virtual
Machine
Shared Memory
Driver
0x0000a000
activity:0x1
Custom
Impl.
RUNTIME
LINUX KERNEL
System
Server
Service
Manager
User Application Process
BT #1
3
2
Core Libraries
Dalvik Virtual
Machine
…
LIBRARIES
LIBRARIES
0x1:3:[parcel]
RUNTIME
4
0x0000a000:3:[parcel]
Binder (IPC) Driver
The 9th Kandroid Conference
From the 8th Kandroid Conference
34. Querying Version Information
Hierarchy
Viewer
4939
1 connect
2
accept
Thread
5
6 read
run()
4 submit
T
Host
① “SERVER”
② “PROTOCOL”
7
3
T
create
run()
…
Thread Pool
ViewServer
WindowManagerService
① “4”
② “4”
system server
Target
Phase 1: Connection Establishment
Phase 2: Command/Result
• ADB (Server & Daemon) Omitted
$View
Server
Worker
The 9th Kandroid Conference
35. Querying Windows
<command>
1
Hierarchy
Viewer
2
read
3
invoke
T
T
…
Thread Pool
run()
$View
Server
Worker
4
Host
<result>
ViewServer
① viewServerListWindows()
② viewServerGetFocusedWindow()
③ addWindowChangeListener()
WindowManagerService
system server
Target
<command>
<result>
① “LIST”
② “GET_FOCUS”
③ “AUTOLIST”
① “413931a8 StatusBar” and
“411945c1 com.android.browser/com.android.browser.BrowserActivity“ and
…
“DONE.”
② “411945c1 com.android.browser/com.android.browser.BrowserActivity”
③ “LIST UPDATE” and/or
“FOCUS UPDATE”
Phase 2: Command/Result
• ADB (Server & Daemon) Omitted
• Phase 1 Omitted
The 9th Kandroid Conference
36. Querying/Controlling Views
1
<command>
2
Hierarchy
Viewer
read
3
T
T
viewServerWindowCommand()
…
Thread Pool
run()
$View
Server
Worker
ViewServer
Host
WindowManagerService
4
8 <result>
system server
findWindow()
5 IWindow.executeCommand()
dispatchCommand()
ViewDebug
<command>
①
②
③
④
⑤
⑥
⑦
6
7 invoke
“DUMP 411945c1”
“CAPTURE_LAYERS 411945c1”
“CAPTURE 411945c1 android.widget.Button@405185a8”
“OUTPUT_DISPLAYLIST android.widget.Button@405185a8”
“INVALIDATE android.widget.Button@405185a8”
“REQUEST_LAYOUT android.widget.Button@405185a8”
“PROFILE android.widget.Button@405185a8”
ViewRootImpl
View
View
View
…
Phase 2: Command/Result
• ADB (Server & Daemon) Omitted
• Phase 1 Omitted
$W
user application
Target
The 9th Kandroid Conference
41. Summary
Questions
Answers
How to get information?
List of Windows
Window & View System 5
Hierarchy of Views in Window
Java Reflection
1
Properties of View
Java Annotations
2
Binder IPC
4
How to deliver information?
Between processes in target
Between host and target
Android Debug Bridge 3
The 9th Kandroid Conference
Hierarchy
Viewer
Internals
6