官方地址:Android Module Porting Guide for 1.8.0.1

概要

伴隨著Titanium Mobile 1.8.0.1的發佈, 對於Android的支援改變成支援多個JAVAscript引擎(V8和Rhino). 這個變化也影響了很多的APIs和協力廠商modules. 儘管平臺的變化, 大多數內部module需要修改的地方也就是簡單的方法標記,類型重命名, import 位置。

Manifest變化

開發在Titanium Mobile 1.8.0.1 (或者更高)的協力廠商modules需要在module manifest中設置一個新的屬性:apiversion,值為2.

引用

apiversion: 2

如果你創建一個新的協力廠商module的話,Titanium Mobile 1.8.0.1會為你的工程自動生成這個屬性,但是,如果是已經有的工程的話,你需要手動的添加這個屬性,而且確保你重新編譯你的module(參照下邊的Build變化):

引用

$ ant clean && ant

Build變化

為了能使用新的V8引擎,所有的協力廠商Module都需要Android NDK來編譯V8,可以從Google下載獲取最新的NDK::

HTTP://developer.android.com/sdk/ndk/index.html

編譯Module之前,需要設置環境變數ANDROID_NDK:

引用

$ export ANDROID_NDK=/path/to/android-ndk

$ ant

當然,你也可以在build.properties中設置android.ndk屬性:

引用

android.ndk=/path/to/android-ndk

你還得更新你的build.properties檔(如果使用Eclipse的話也要更新.classpath檔)來引用Titanium Mobile 1.8.0.1 和 API level 8 或者更高::

引用

titanium.sdk=/Library/Application\ Support/Titanium/

titanium.os=osx

titanium.version=1.8.0.1

android.sdk=/usr/android-sdk

titanium.platform=${titanium.sdk}/mobilesdk/${titanium.os}/${titanium.version}/android

android.platform=${android.sdk}/platforms/android-8

google.apis=${android.sdk}/add-ons/addon_google_apis_google_inc_8

基本變化

1. Remove TiContext.

TiContext is being replaced, and any implementation utilizing TiContext will take a performance / stability hit compared to using the desired API's directly.

In most of the places where TiContext is used as an argument, the TiContext argument can be removed entirely or replaced with an Activity reference.

Example:

Js代码

  1. TiDrawableReference.fromUrl(proxy.getTiContext(), tiImage)

becomes:

Js代码

  1. TiDrawableReference.fromUrl(proxy.getActivity(), tiImage)

In the specific case of fromUrl, the following form can also be used:

Js代码

  1. TiDrawableReference.fromUrl(proxy, tiImage)

The specific alternative varies based on which API point is being modified, but generally there is a very simple alternative that can be used.

2. Use KrollFunction instead of KrollCallback

KrollFunction has been added, and KrollCallback has been removed; The dual runtime change required a common interface to be defined to replace KrollCallback.

In most cases, a direct replacement of KrollCallback with KrollFunction should suffice.

主要变化

Known compatibility points that need to be changed:

1. Remove KrollInvocation as a method argment.

Example:

Js代码

  1. @Kroll.method
  2. public void myMethod(final KrollInvocation invocation, KrollDict args)

becomes:

Js代码

  1. @Kroll.method
  2. public void myMethod(KrollDict args)

2. Remove TiContext from your module constructor. super(TiContext) will no longer work due to the previously mentioned TiContext change. In most if not all cases, simply removing the argument will address the problem.

Example:

Js代码

  1. public BoxModule(TiContext context) {
  2. super(context);
  3. }

becomes:

Js代码

  1. public BoxModule() {
  2. super();
  3. }

3. When replacing KrollCallback with KrollFunction, you need to to pass a KrollObject argument to the call and callAsync methods.

Example:

Js代码

  1. KrollCallback success = (KrollCallback)args.get("success");
  2. ...
  3. success.callAsync(event);

becomes:

Js代码

  1. KrollFunction success = (KrollFunction)args.get("success");
  2. ...
  3. success.callAsync(proxy.getKrollObject(), event);

4. Change getView() on a TiViewProxy to getOrCreateView()/

Example:

Js代码

  1. View view = myViewProxy.getView();

becomes:

S代码

  1. View view = myViewProxy.getOrCreateView();

5. Change TiDrawableReference.fromUrl(proxy.getTiContext(), tiImage) to TiDrawableReference.fromUrl(proxy.getActivity(), tiImage) due to the TiContext change. Same applies to all the "from<source>" methods in TiDrawableReference.

Example:

Js代码

  1. TiDrawableReference ref = TiDrawableReference.fromBlob(context, blob);

becomes:

Js代码

  1. TiDrawableReference ref = TiDrawableReference.fromBlob(getActivity(), blob);

6. <KrollInvocation>.getActivity() no longer exists. getActivity can be called on each proxy to get the activity for that proxy or TiApplication.getAppCurrentActivity() and TiApplication.getAppRootActivity() can be used for getting activity instances to work with. In general, system services, etc., can and should use the root activity if it exists. TiApplication.getRootOrCurrentActivity() will serve this purpose in the vast majority of situations.

Example:

Js代码

  1. Activity activity = invocation.getActivity();

becomes:

Js代码

  1. Activity activity = TiApplication.getAppRootOrCurrentActivity();

7. Calling addOnLifeCycleEvent on a module is no longer necessary. Modules are now automatically registered to receive the lifecycle events (onPause, onResume, onStart, onStop, and onDestroy).

8. <KrollEventManager>.addOnEventChangeListener() is no longer supported. The new mechanism for this is to override KrollProxy.eventListenerAdded, and move the code from the listenerAdded method into the overridden eventListenerAdded method after the call to super.eventListenerAdded.

Example:

Js代码

  1. @Override
  2. public void eventListenerAdded(String type, int count, KrollProxy proxy)
  3. {
  4. super.eventListenerAdded(type, count, proxy);
  5.  
  6. // this is the logic that used to live inside the listenerAdded() method
  7. if (MY_CUSTOM_EVENT.equals(type)) {
  8. invokeMyCustomMethod();
  9. }
  10. }

9. resolveUrl has been moved to the proxy object.

Example:

Js代码

  1. _proxy.getTiContext().resolveUrl(url);

becomes:

Js代码

  1. _proxy.resolveUrl(url);

10. Change getModuleById to getModuleByName and specify the module name in the module constructor. By default the module cannot be found by calling getModuleByName. You must use the form of super() in the module constructor that allows you to specify the module name.

Example:

Js代码

  1. public myModule(TiContext tiContext) {
  2. super(tiContext);
  3. }
  4. ...
  5. TiApplication appContext = TiApplication.getInstance();
  6. MyModule myModule =(MyModule)appContext.getModuleById("ti.mymodule");

becomes:

Js代码

  1. public myModule() {
  2. super("mymodule");
  3. }
  4. ...
  5. TiApplication appContext = TiApplication.getInstance();
  6. MyModule myModule = (MyModule)appContext.getModuleByName("mymodule");

11. <TiContext>.getAndroidContext() no longer exists. If the ContextWrapper returned originally is being used to access system or app level resources, use TiApplication.getInstance() or TiApplication.getInstance().getApplicationContext() instead. To get the ContextWrapper for the top most Activity, use TiApplication.getAppCurrentActivity() instead.

Example:

Js代码

  1. myFunction(context.getAndroidContext());

becomes:

Js代码 <

  1. myFunction(TiApplication.getInstance().getApplicationContext());

12. getContext() on a TiProxy no longer exists because its purpose was to return a TiContext instance. This call should no longer be needed in module implementation once TiContext is no longer being passed in as an argument (the normal use case for this method).

Example:

Js代码

  1. Context context = proxy.getContext();

becomes:

Js代码

  1. Context context = TiApplication.getInstance().getApplicationContext();

13. Remove context from TiFileFactory.createTitaniumFile.

Example:

Js代码

  1. TiBaseFile file = TiFileFactory.createTitaniumFile(context, fileURL, false);

becomes:

Js代码

  1. TiBaseFile file = TiFileFactory.createTitaniumFile(fileURL, false);

14. getChangeDir is now called on the TiApplication instance.

Example:

Js代码

  1. _proxy.getContext().getCacheDir()

becomes:

Js代码

  1. TiApplication.getInstance().getCacheDir()

15. Overriding the fireEvent method requires a change to the method signature. The argument has changed from a KrollDict class to an Object.

Example:

Js代码

  1. @Override
  2. public boolean fireEvent(String eventName, KrollDict data) {
  3. ...
  4. }

becomes:

Js代码

  1. @Override
  2. public boolean fireEvent(String eventName, Object data) {
  3. ...
  4. }

16. <KrollInvocation>.getTiContext() no longer exists. TiContext is no longer needed. Some examples show this being used to get the TiApplication instance. TiApplication.getInstance() can be used instead.

17. Change usage of KrollDict in method signatures to HashMap. Dictionary values are now passed to methods as HashMap objects. If you need to access any of the KrollDict methods on the HashMap object you can create a KrollDict object from the HashMap object.

Example:

Js代码

  1. public void myMethod(KrollInvocation invocation, KrollDict args)
  2. {
  3. TiBlob blob = (TiBlob) args.get("image");
  4. int myId = args.getInt("id);

becomes:

Js代码

  1. public void myMethod(HashMap args) {
  2. KrollDict argsDict = new KrollDict(args);
  3. TiBlob blob = (TiBlob) args.get("image");
  4. int myID = argsDict.getInt("id");

18. runOnUiThread is no longer supported. You must explicitly manage and call your methods on the UI thread where necessary. Specifically, you can use the TiMessenger class to run code on the UI thread.

Example:

Js代码

  1. @Kroll.method(runOnUiThread = true)
  2. public void refresh() {
  3. if (_view != null)
  4. _view.refresh();
  5. }

becomes:

Js代码

  1. private static final int MSG_REFRESH = 50000;
  2. private final Handler handler = new Handler(TiMessenger.getMainMessenger().getLooper(), new Handler.Callback ()
  3. {
  4. public boolean handleMessage(Message msg)
  5. {
  6. switch (msg.what) {
  7. case MSG_REFRESH: {
  8. AsyncResult result = (AsyncResult) msg.obj;
  9. handleRefresh();
  10. result.setResult(null);
  11. return true;
  12. }
  13. }
  14. return false;
  15. }
  16. });
  17.  
  18. private void handleRefresh()
  19. {
  20. _view.refresh();
  21. }
  22.  
  23. @Kroll.method
  24. public void refresh() {
  25. if (_view != null) {
  26. if (!TiApplication.isUIThread()) {
  27. TiMessenger.sendBlockingMainMessage(handler.obtainMessage(MSG_REFRESH));
  28. } else {
  29. handleRefresh();
  30. }
  31. }
  32. }
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()