Android Dynamic Module
Lập trình module động trong Android
Phí Ngọc Chi – CTO Eway JSC
chipn@eway.vn | facebook.com/phingocchi
Cập nhật Ứng Dụng Android
APK

APK

Module v 2.0

Module v 1.0

…

…

…

…

,,,

…

Server

Client
Cập nhật Module
APK
APK
Module v 2.0

Module v 1.0
…
…
…

Server

Client
Ưu điểm cập nhật theo Module
Mềm dẻo và linh động
 Khả năng mở rộng tốt
 Đơn giản hóa việc cập nhật với người dùng
 Dung lượng cập nhật nhỏ
 Tốc độ cập nhật nhanh tức thời

Dynamic Module



Module được phát triển như một project độc
lập và đóng gói thành file module.apk
Ứng dụng chính tải file module.apk từ URL



DexClassLoader: thư viện sử dụng để nạp class
động từ file module.apk



Java Reflection: khởi tạo object từ class động
và tương tác với object để kích hoạt logic thực
thi
Module.apk – v2.0

Module
…

Download Module

Load Module

DexClassLoader

APK

Module.apk – v1.0

Java Reflection

Quy trình cập nhật Module

Load Module Class

New Instance
Invoke Method

Active Module
DemoModule Code
public class DemoModule {
public void show() {

Toast.makeText(getApplicationContext(), "Hi!
version 1.0", Toast.LENGTH_SHORT).show();
}
}

DemoModule.apk - v1.0

public class DemoModule{
public void show() {
AlertDialog dialog = new
AlertDialog.Builder(this).create();
dialog.setMessage("Hi! version 2.0");
dialog.show();
}
}

DemoModule.apk – v2.0
Download DemoModule
public void downloadDemoModule(Context context) {
String dexPath = new File(context.getDir("dex",
Context.MODE_PRIVATE), "DemoModule.apk").getAbsolutePath();

//TODO: download DemoModule.apk from URL to dexPath File
}
Load DemoModule
public DexClassLoader getDemoModuleClassLoader(Context context) {
String dexPath = new File(context.getDir("dex",
Context.MODE_PRIVATE), "DemoModule.apk").getAbsolutePath();
String optimizedDirectory = context.getDir("temp", 0).toString();
String libraryPath = null;
ClassLoader parent ClassLoader = context.getClassLoader();
DexClassLoader dexClassLoader = new DexClassLoader(dexPath,
optimizedDirectory, libraryPath, parentClassLoader);
return dexClassLoader;
}
Active DemoModule
DexClassLoader classLoader = this.getDemoModuleClassLoader(this);
try {
Class<?> demoModuleClass =
classLoader.loadClass("com.simple.DemoModule");
Object demoModule = demoModuleClass.newInstance();
demoModuleClass.getMethod("show").invoke(demoModule);
} catch (Exception ex) {
ex.printStackTrace();
}
DemoModule Screenshot
Thanks for your attention!
Phí Ngọc Chi – CTO Eway JSC
chipn@eway.vn | facebook.com/phingocchi

Android dynamic module

  • 1.
    Android Dynamic Module Lậptrình module động trong Android Phí Ngọc Chi – CTO Eway JSC chipn@eway.vn | facebook.com/phingocchi
  • 2.
    Cập nhật ỨngDụng Android APK APK Module v 2.0 Module v 1.0 … … … … ,,, … Server Client
  • 3.
    Cập nhật Module APK APK Modulev 2.0 Module v 1.0 … … … Server Client
  • 4.
    Ưu điểm cậpnhật theo Module Mềm dẻo và linh động  Khả năng mở rộng tốt  Đơn giản hóa việc cập nhật với người dùng  Dung lượng cập nhật nhỏ  Tốc độ cập nhật nhanh tức thời 
  • 5.
    Dynamic Module   Module đượcphát triển như một project độc lập và đóng gói thành file module.apk Ứng dụng chính tải file module.apk từ URL  DexClassLoader: thư viện sử dụng để nạp class động từ file module.apk  Java Reflection: khởi tạo object từ class động và tương tác với object để kích hoạt logic thực thi
  • 6.
    Module.apk – v2.0 Module … DownloadModule Load Module DexClassLoader APK Module.apk – v1.0 Java Reflection Quy trình cập nhật Module Load Module Class New Instance Invoke Method Active Module
  • 7.
    DemoModule Code public classDemoModule { public void show() { Toast.makeText(getApplicationContext(), "Hi! version 1.0", Toast.LENGTH_SHORT).show(); } } DemoModule.apk - v1.0 public class DemoModule{ public void show() { AlertDialog dialog = new AlertDialog.Builder(this).create(); dialog.setMessage("Hi! version 2.0"); dialog.show(); } } DemoModule.apk – v2.0
  • 8.
    Download DemoModule public voiddownloadDemoModule(Context context) { String dexPath = new File(context.getDir("dex", Context.MODE_PRIVATE), "DemoModule.apk").getAbsolutePath(); //TODO: download DemoModule.apk from URL to dexPath File }
  • 9.
    Load DemoModule public DexClassLoadergetDemoModuleClassLoader(Context context) { String dexPath = new File(context.getDir("dex", Context.MODE_PRIVATE), "DemoModule.apk").getAbsolutePath(); String optimizedDirectory = context.getDir("temp", 0).toString(); String libraryPath = null; ClassLoader parent ClassLoader = context.getClassLoader(); DexClassLoader dexClassLoader = new DexClassLoader(dexPath, optimizedDirectory, libraryPath, parentClassLoader); return dexClassLoader; }
  • 10.
    Active DemoModule DexClassLoader classLoader= this.getDemoModuleClassLoader(this); try { Class<?> demoModuleClass = classLoader.loadClass("com.simple.DemoModule"); Object demoModule = demoModuleClass.newInstance(); demoModuleClass.getMethod("show").invoke(demoModule); } catch (Exception ex) { ex.printStackTrace(); }
  • 11.
  • 12.
    Thanks for yourattention! Phí Ngọc Chi – CTO Eway JSC chipn@eway.vn | facebook.com/phingocchi