Android module development in Titanium: port to version 1.8

Source: Internet
Author: User

Official Address: Android Module Porting Guide for 1.8.0.1

Summary

With the release of Titanium Mobile 1.8.0.1, Android support is changed to support multiple Javascript Engines (V8 and Rhino ). this change also affects many APIs and third-party modules. despite the changes in the platform, most internal modules need to be modified, that is, simple method tag, type rename, and import location.

Manifest changes

Third-party modules developed in Titanium Mobile 1.8.0.1 (or higher) need to set a new attribute in module manifest: apiversion; Value: 2.

Reference

Apiversion: 2

If you create a new third-party module, Titanium Mobile 1.8.0.1 will automatically generate this attribute for your project. However, if it is an existing project, you need to manually add this attribute, make sure that you recompile your module by referring to the Build changes below ):

Reference

$ Ant clean & ant

Build changes

To use the new V8 engine, all third-party modules require Android NDK to compile V8. You can download the latest NDK from Google ::

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

Before compiling the Module, you need to set the environment variable ANDROID_NDK:

Reference

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

$ Ant

Of course, you can also set the android. ndk attribute in build. properties:

Reference

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

You have to update your build. properties file. If Eclipse is used, update the. classpath file) to reference Titanium Mobile 1.8.0.1 and API level 8 or higher ::

Reference

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

Basic changes

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 Code

Becomes:

Js Code

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

Js Code

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 shocould suffice.

Major changes

Known compatibility points that need to be changed:

1. Remove KrollInvocation as a method argment.

Example:

Js Code

Becomes:

Js Code

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

Example:

Js Code

Becomes:

Js Code

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

Example:

Js Code

Becomes:

Js Code

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

Example:

Js Code

Becomes:

S Code

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 Code

Becomes:

Js Code

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. in general, system services, etc ., can and shocould use the root activity if it exists. tiApplication. getRootOrCurrentActivity () will serve this purpose in the vast majority of situations.

Example:

Js Code

Becomes:

Js Code

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 Code

9. resolveUrl has been moved to the proxy object.

Example:

Js Code

Becomes:

Js Code

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 Code

Becomes:

Js Code

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 Code

Becomes:

Js Code <

12. getContext () on a TiProxy no longer exists because its purpose was to return a TiContext instance. this call shoshould 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 Code

Becomes:

Js Code

13. Remove context from TiFileFactory. createTitaniumFile.

Example:

Js Code

Becomes:

Js Code

14. getChangeDir is now called on the TiApplication instance.

Example:

Js Code

Becomes:

Js Code

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 Code

Becomes:

Js Code

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 Code

Becomes:

Js Code

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 Code

Becomes:

Js Code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.