[TIMOB-9314] Native Module Projects (Android/iOS): At runtime, support execution of code originally from both CommonJS file and from native source files.
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2012-06-15T15:25:14.000+0000 |
Affected Version/s | Release 2.1.0 |
Fix Version/s | Release 2.1.0, Sprint 2012-12 Core |
Components | TiAPI |
Labels | core |
Reporter | Bill Dawson |
Assignee | Neeraj Gupta |
Created | 2012-05-30T15:12:38.000+0000 |
Updated | 2017-03-24T21:02:01.000+0000 |
Description
This is the super task (specific iOS and Android sub-tasks exist).
Description
Currently, module projects for both Android and iOS have the option to put Javascript (rather than native) code in assets/[moduleid].js. If that file exists, then at build time the source is compiled/protected and, when distributed, the module is simply whatever is exported in that Javascript -- any code that happened to be on the native (*.m, *.java) files -- though compiled -- is ignored. For example, when create either an iOS or Android module project, you get the .m and .java files for the module and a method namedexample
. If you also add a commonjs module file in the form of assets/[moduleid].js, then that example
method inside the native source file is _not_ reachable at runtime when the module is used.
We wish to make the optional CommonJS module file "additive" in the sense that it extends whatever is available via the native source, rather than replaces it.
Test Case
* Create a module project for the platform you're testing (iOS or Android). * Create a file assets/moduleid.js (replace moduleid with the actual module id) and enter this code:
exports.test = function() {return "hello from inside the module's commonjs file";};
* Build/package the module (./build.py for iOS, ant for Android.)
* Create a Titanium Mobile app.
* Install the module zip to the app (or up in the Titanium root). No instructions here -- it's assumed you know how to install a module.
* Update tiapp.xml to include the module. (assuming you know how).
* Put this in app.js:
var m = require("moduleid"); // replace moduleid with actual module id
Ti.API.info(m.test());
Ti.API.info(m.example());
* In the console/logcat, you should see info statements for "hello from inside the module's commonjs file" and "hello world".
Closing ticket as fixed.