Jollen's Presentation: Introducing Android low-level

16,223 views

Published on

What's behind the Android apps. 研究Android底層(包含framework)的重要性是什麼? 請看Jollen's presentation.

2 Comments
32 Likes
Statistics
Notes
  • thanks for ur sharing
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • android low-level
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
16,223
On SlideShare
0
From Embeds
0
Number of Embeds
125
Actions
Shares
0
Downloads
1,911
Comments
2
Likes
32
Embeds 0
No embeds

No notes for slide

Jollen's Presentation: Introducing Android low-level

  1. 1. Jollen’s Consulting , , , : www.jollen.org/consulting
  2. 2. Jollen’s Consulting ta@jollen.org [ ] Jollen’s Consulting Team
  3. 3. Presentation Training Coaching
  4. 4. low-level
  5. 5. Android Kernel Features
  6. 6. Android Kernel Features
  7. 7. Android Kernel •Binder •Ashmem (Android shared memory) •PMEM (Processor memory allocator) •logcat (Android Logger) •wakelock (Android Power Management) •Alarm
  8. 8. Key Features 2.6.23 2.6.25 2.6.27 2.6.29 1 Alarm Driver O O O O 2 Android Logger O O O O 3 Low Memory Killer O O O O 4 Power Management O O O O 5 USB Gadget O O O O 6 ASHMEM X O O O 7 PMEM X X O O 8 x86 Support X X O O 9 ./drivers/staging/Android/ X X X O
  9. 9. Android Toolchain •android-toolchain-20081019 •http://android.git.kernel.org/pub/ •gcc 4.2.1 / binutils 2.17 / gdb 6.6 •ARMV5te+ •ABI: EABI, AAPCS •--with-float=soft --with=fpu-vfp •--enable-threads (single)
  10. 10. Application Developers Applications change Application Framework Surface Manager Dalvik VM System Media Framework Shared Libraries Image Linux Kernel
  11. 11. Product Branch Maintainer Applications Application Framework Surface Manager Dalvik VM rebuild Media Framework Shared Libraries Linux Kernel
  12. 12. Hardware Developing Applications Application Framework Surface Manager Dalvik VM change Media Framework Shared Libraries Linux Kernel change
  13. 13. Architecture Porting Applications Application Framework Surface Manager Dalvik VM porting Media Framework Shared Libraries Linux Kernel
  14. 14. • Application Developers
  15. 15. • Application Developers • Product Branch Maintainer
  16. 16. • Application Developers • Product Branch Maintainer • Hardware Developing
  17. 17. • Application Developers • Product Branch Maintainer • Hardware Developing • Architecture Porting
  18. 18. Android Framework
  19. 19. Android Framework
  20. 20. • The best way to write good, efficient code for embedded systems is to understand what the code you write really does. --Android Dev Guide
  21. 21. private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); }
  22. 22. private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); } private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); setContentView(R.layout.main); }
  23. 23. private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); UI; SensorManager; }
  24. 24. private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); UI; SensorManager; } private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SensorManager; UI; }
  25. 25.
  26. 26. public class TunitBill extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Debug.startMethodTracing("TunitB"); CharSequence time = Long.toString(SystemClock.elapsedRealtime()); Debug.stopMethodTracing(); } }
  27. 27. public class TunitBill extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Debug.startMethodTracing("TunitB"); CharSequence time = Long.toString(SystemClock.elapsedRealtime()); Debug.stopMethodTracing(); } } • method-level
  28. 28. method-level low-level
  29. 29. • TraceView method (method tracing)
  30. 30. Avoid Creating Objects Use Native Methods Prefer Virtual Over Interface Prefer Static Over Virtual Avoid Internal Getters/Setters Cache Field Lookups Declare Constants Final Use Enhanced For Loop Syntax With Caution Avoid Enums Use Package Scope with Inner Classes Avoid Float
  31. 31. Avoid Creating Objects Use Native Methods Prefer Virtual Over Interface Prefer Static Over Virtual Avoid Internal Getters/Setters Cache Field Lookups Declare Constants Final Use Enhanced For Loop Syntax With Caution Avoid Enums Use Package Scope with Inner Classes Avoid Float • Designing for Performance -- Android Dev Guide
  32. 32. Android Tracing API android.os.Debug
  33. 33. Debug.startMethodTracing("TunitB"); /* writing codes to profile */ Debug.stopMethodTracing(); Android Tracing API android.os.Debug
  34. 34. public class TunitChris extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.btn); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); } }
  35. 35.
  36. 36. Acitivty View.OnClickListener Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
  37. 37. public class TunitChris extends Activity implements View.OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.btn); button.setOnClickListener(this); } public void onClick(View v) { Toast.makeText( this, "Yes.", Toast.LENGTH_LONG).show(); } }
  38. 38. button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } });
  39. 39. public class TunitChris extends Activity implements View.OnClickListener
  40. 40.
  41. 41. SensorManager
  42. 42. private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); } SensorManager
  43. 43. private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onStop() { super.onStop(); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); }
  44. 44. private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onStop() { super.onStop(); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); } better
  45. 45. Source: Android SDK
  46. 46. Launcher
  47. 47. method-level
  48. 48. Android Driver Model HAL
  49. 49. Android Driver Model HAL
  50. 50. native C programs glibc & shared libs S!"#$%!&'(( Kernel Hardware
  51. 51. Android Framework Application JNI S!"#$%!&'(( bionic & shared libs Kernel Hardware
  52. 52. public class Sensors extends Activity { ... private SensorManager mSensorManager; ... @Override protected void onCreate(Bundle savedInstanceState) { // Be sure to call the super class. super.onCreate(savedInstanceState); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); } ... }
  53. 53. class SensorService extends ISensorService.Stub { ... private static native int _sensors_control_init(); private static native ParcelFileDescriptor _sensors_control_open(); private static native boolean _sensors_control_activate(int sensor, boolean activate); private static native int _sensors_control_set_delay (int ms); } at framework/base/services/java/SensorService.java
  54. 54. class SensorService extends ISensorService.Stub { ... public SensorService(Context context) { if (localLOGV) Log.d(TAG, "SensorService startup"); _sensors_control_init(); mNotificationManager = (NotificationManager) context.getSystemService (Context.NOTIFICATION_SERVICE); mContext = context; } ... } at framework/base/services/java/SensorService.java
  55. 55. static JNINativeMethod gMethods[] = { {"_sensors_control_init", "()I", (void*) android_init }, {"_sensors_control_open", "()Landroid/os/ ParcelFileDescriptor;", (void*) android_open }, {"_sensors_control_activate", "(IZ)Z", (void*) android_activate }, {"_sensors_control_wake", "()I", (void*) android_data_wake }, {"_sensors_control_set_delay","(I)I", (void*) android_set_delay }, };
  56. 56. static jint android_init(JNIEnv *env, jclass clazz) { sensors_module_t* module; if (hw_get_module(SENSORS_HARDWARE_MODULE_ID, (const hw_module_t**) &module) == 0) { if (sensors_control_open(&module->common, &sSensorDevice) == 0) { const struct sensor_t* list; int count = module->get_sensors_list(module, &list); return count; } } return 0; } static inline int sensors_control_open(const struct hw_module_t* module, struct sensors_control_device_t** device) { return module->methods->open(module, SENSORS_HARDWARE_CONTROL, (struct hw_device_t**) device); }
  57. 57. Application Context getSystemService Create Manager App App View !"#$%& IStubService Manager framework/base/services/java alvik VM IStubService
  58. 58. SensorManager
  59. 59. private SensorManager mSensorManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); } SensorManager
  60. 60. public Object getSystemService(String name) { if (WINDOW_SERVICE.equals(name)) { return WindowManagerImpl.getDefault(); } else if (LAYOUT_INFLATER_SERVICE.equals(name)) { synchronized (mSync) { LayoutInflater inflater = mLayoutInflater; if (inflater != null) { return inflater; } mLayoutInflater = inflater = PolicyManager.makeNewLayoutInflater(getOuterContext()); return inflater; } } else if (ACTIVITY_SERVICE.equals(name)) { return getActivityManager(); } else if (ALARM_SERVICE.equals(name)) { return getAlarmManager(); .. } else if ( SENSOR_SERVICE.equals(name)) { return getSensorManager(); } ... } Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
  61. 61. private SensorManager getSensorManager() { synchronized (mSync) { if (mSensorManager == null) { mSensorManager = new SensorManager (mMainThread.getHandler().getLooper()); } } return mSensorManager; }
  62. 62. Application Context getSystemService Create Manager App App View !"#$%& IStubService Manager framework/base/services/java Dalvik VM IStubService Service/Java framework/base/services/java register JNI Table Android server VM Onload App Onload Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
  63. 63. • device driver
  64. 64. !"#$%& Manager framework/base/services/java Dalvik VM IStubService Service/Java framework/base/services/java register JNI Table Android server Service/JNI framework/base/services/jni HAL hardware/libhardware HAL Stub sysfs /sys VM Onload App Onload Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
  65. 65. at framework/base/services/java/SensorService.java
  66. 66. class SensorService extends ISensorService.Stub { ... public SensorService(Context context) { if (localLOGV) Log.d(TAG, "SensorService startup"); _sensors_control_init(); mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mContext = context; } ... } at framework/base/services/java/SensorService.java
  67. 67. Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
  68. 68. VFS Driver D Driver B Driver A Driver E Driver C Copyright (c) 2009 Jollen’s Consulting, www.jollen.org/consulting Embedded Linux
  69. 69. kernel buses devices classes firmware kobject Core Driver Model – Subsystems (ksets…) /sys Driver D Driver B Driver A Driver E Driver C Copyright (c) 2009 Jollen’s Consulting, www.jollen.org/consulting Embedded Linux
  70. 70. Android
  71. 71. Android • Launcher Model • getSystemService • Framework • HAL module sysfs
  72. 72. high-level low-level
  73. 73. high-level low-level
  74. 74. high-level low-level
  75. 75. high-level architect-level low-level
  76. 76. architect
  77. 77. Android Source Tree
  78. 78. Android Source Tree |-- Makefile         Makefile |-- bionic          Bionic 义 这 |-- bootloader       |-- build            build 录 内 脚 |-- dalvik          JAVA虚拟 |-- development      发 |-- external         |-- frameworks       |-- hardware         |-- kernel          Linux2.6 |-- packages         Android |-- prebuilt         Android 脚 |-- recovery         `-- system          Android
  79. 79. <company_name> <board_name> + Android.mk + product_config.mk + system.prop products + AndroidProducts.mk + <first_product_name>.mk + <second_product_name>.mk Android Jollen’s Consulting. www.jollen.org/consulting
  80. 80. 1. Get Google Android $ repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake $ repo sync 2. Manifest file. <manifest> ... <project name="platform/vendor/asus/eee_701" path="vendor/asus/ eee_701"/> ... </manifest> 3. Get EeePC platform. $ repo sync 4. Build Android image. $ TARGET_ARCH=x86 TARGET_PRODUCT=eee_701 DISABLE_DEXPREOPT=true make -j2 installer_img 5. Create USB boot stick. Use make-live script. https://review.source.android.com/Gerrit#change,6475 Android Jollen’s Consulting. www.jollen.org/consulting
  81. 81. Build EeePC 701 Product Tips # Google API issue $(call inherit-product, $(SRC_TARGET_DIR)/product/generic.mk) PRODUCT_NAME := eee_701 PRODUCT_DEVICE := eee_701 PRODUCT_POLICY := android.policy_mid PRODUCT_PROPERTY_OVERRIDES += ro.com.android.dataroaming=true Cupcake将Google APIs add-ons Android Jollen’s Consulting. www.jollen.org/consulting
  82. 82. Build EeePC 701 Product Tips # e2fsprogs issue external/e2fsprogs/Android.mk: --- a/Android.mk +++ b/Android.mk @@ -1,3 +1,3 @@ ifneq ($(TARGET_SIMULATOR),true) -# include $(call all-subdir-makefiles) + include $(call all-subdir-makefiles) endif Android Jollen’s Consulting. www.jollen.org/consulting
  83. 83. boot.img installer.img ramdisk.img system.img userdata.img Android Jollen’s Consulting. www.jollen.org/consulting
  84. 84. Android 随 $ make-live Android Jollen’s Consulting. www.jollen.org/consulting
  85. 85. AMD PCnet32 PCI support VESA VGA graphics support VGA 8x8 font VGA 8x16 font Android Jollen’s Consulting. www.jollen.org/consulting
  86. 86. $ VBoxManage convertromraw -format VDI ./installer.img ./android.vdi Android Jollen’s Consulting. www.jollen.org/consulting
  87. 87. device/system/init device/system/init/init.c /etc/init.rc 动mount file system ( /etc/fstab) Android Jollen’s Consulting. www.jollen.org/consulting
  88. 88. /system/bin/logd /sbin/adbd /system/bin/usbd /system/bin/debuggerd /system/bin/rild /system/bin/app_process /system/bin/runtime /system/bin/dbus-daemon system_server Android Jollen’s Consulting. www.jollen.org/consulting
  89. 89. • Application Developers • Product Branch Maintainer • Hardware Developingframework, app LED : kernel, hal, • Architecture Porting
  90. 90. www.jollen.org/consulting Jollen@jollen.org

×