Title: [Proposal] Use custom classes required at compile time (e.g. Activity, Application, etc.) · Issue #283 · NativeScript/android · GitHub
Open Graph Title: [Proposal] Use custom classes required at compile time (e.g. Activity, Application, etc.) · Issue #283 · NativeScript/android
X Title: [Proposal] Use custom classes required at compile time (e.g. Activity, Application, etc.) · Issue #283 · NativeScript/android
Description: Overview In Android development it is a standard practice to use inheritance to create custom activities, applications and other components. Currently NativeScript runtime for Android ({N}) has limited support for this scenario. This pro...
Open Graph Description: Overview In Android development it is a standard practice to use inheritance to create custom activities, applications and other components. Currently NativeScript runtime for Android ({N}) has lim...
X Description: Overview In Android development it is a standard practice to use inheritance to create custom activities, applications and other components. Currently NativeScript runtime for Android ({N}) has lim...
Opengraph URL: https://github.com/NativeScript/android/issues/283
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Proposal] Use custom classes required at compile time (e.g. Activity, Application, etc.)","articleBody":"## Overview\n\nIn Android development it is a standard practice to use inheritance to create custom activities, applications and other components. Currently NativeScript runtime for Android ({N}) has limited support for this scenario. This proposal aims to provide a solution for creating custom activities, applications and other components.\n\n> Note: for the sake of this proposal I use the common scenario to extend an activity class.\n## Current State\n\n{N} provides built-in `NativeScriptApplication` and `NativeScriptActivity` classes. While these classes helped the adoption of {N} they also provide limitations. Currently it is not possible to inherit from a third party activity or other types which are used in `AndroidManifest.xml` file.\n## Technical Details\n\nHere is how a Java class is extended in {N}\n\n``` JavaScript\n// app/myactivity.js\nvar MyActivity = android.app.Activity.extend({\n onCreate: function(bundle) {\n // implementation\n }\n});\n```\n\nHowever `MyActivity` class is generated at runtime by {N} bridge. This mean you cannot use `MyActivity` in `AndroidManifest.xml` the way we currently use `NativeScriptActivity` for example.\n\n``` XML\n\u003c!-- AndroidManifest.xml -->\n\u003cactivity\n android:name=\"com.tns.NativeScriptActivity\"\n android:label=\"TestApp\">\n \u003cintent-filter>\n \u003caction android:name=\"android.intent.action.MAIN\" />\n \u003ccategory android:name=\"android.intent.category.LAUNCHER\" />\n \u003c/intent-filter>\n\u003c/activity>\n```\n## Proposal\n\nThe current `extend` implementation can accept two parameters as well. We can rewrite the previous example as follows\n\n``` JavaScript\n// app/myactivity.js\nvar MyActivity = android.app.Activity.extend(\"MyActivity\", {\n onCreate: function(bundle) {\n // implementation\n }\n});\n```\n\nWe can leverage the latter syntax of `extend` function and pass the fully qualified type name of the new class as follows\n\n``` JavaScript\n// app/myactivity.js\nvar MyActivity = android.app.Activity.extend(\"com.example.MyActivity\", {\n onCreate: function(bundle) {\n // implementation\n }\n});\n```\n\nDuring build time we can parse the JavaScript and generate Java file `com/example/MyActivity.java`. This proposal will not make the existing code ambiguous because currently there is a validation rule that says the class name cannot contain `.` symbol. The generated `MyActivity.java` file will look as follows\n\n``` Java\n// com/example/MyActivity.java\npackage com.example;\n\n@com.tns.JavaScriptImplementation(javaScriptFile = \"app/myactivity.js\")\npublic class MyActivity extends android.app.Activity {\n public MyActivity() {\n com.tns.Platform.initInstance(this);\n }\n public void onCreate(android.os.Bundle param_0) {\n Object[] args = new Object[1];\n args[0] = param_0;\n com.tns.Platform.callJSMethod(this, \"onCreate\", void.class, args);\n }\n}\n```\n\nThis is how the currently **runtime** generated types look like. We can provide a tool that generates equivalent types at **build time**. The only difference is the use of `com.tns.JavaScriptImplementation` annotation. The need a way to find the JavaScript implementation for `MyActivity` when an instance is created. Using annotation is just one possible approach. I find it clear and, more importantly, I find it aligned with the following proposition how to extend Java class in TypeScript.\n\n``` TypeScript\n// myactivity.ts\n@JavaProxy(\"com.tns.MyActivity\")\nclass MyActivity extends android.app.Activity {\n onCreate(bundle: android.os.Bundle) {\n // implementation\n }\n}\n```\n\nUsing ES6/TypeScript decorators is a simple way to indicate that we have to generate Java class at build time. The main benefit of using decorators is that the change is less obtrusive than other alternatives.\n## Impact\n\nThis proposal will require [{N} modules](https://github.com/NativeScript/NativeScript) to change the way an application is initialized and activities are created. I expect that this would be small-to-medium change. The immediate benefits are better encapsulation and control. Also, it may require {N} modules to come with prebuilt `Activity` and `Application` classes. Another approach would be adding these classes to the project template.\n## Summary\n\nThis proposal has some other benefits as well. Removing `NativeScriptActivity` and `NativeScriptApplication` from {N} will allows us to support [lower Android API levels](https://github.com/NativeScript/android-runtime/issues/198). Currently we have to compile against API level 17 and there is no technical justification for that. Also it will allow embedding {N} in other applications much easier. One primary scenario is [NativeScript companion app](https://play.google.com/store/apps/details?id=com.telerik.NativeScript&hl=en).\n\nFinally, you can find a prototype [here](https://github.com/NativeScript/android-runtime/tree/slavchev/new-project-template2)\n","author":{"url":"https://github.com/slavchev","@type":"Person","name":"slavchev"},"datePublished":"2015-11-27T16:31:58Z","url":"https://github.com/NativeScript/android/issues/283"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:64013b14-2d6d-cdf2-b79c-35506b5a9a59 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8284:2D96F1:1B638C9:2426C65:6B0150AB |
| html-safe-nonce | 3962e548b91dc89c6382a83304ec6c398766e4a8f41fdebf851830669c32eeed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Mjg0OjJEOTZGMToxQjYzOEM5OjI0MjZDNjU6NkIwMTUwQUIiLCJ2aXNpdG9yX2lkIjoiMjAyMTEwNjk2ODcwOTY0ODU1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5f2c336afd9ae6a223d4cd8d5e30b95fb8bfdc8f7efa7b694ef7c917105b018f |
| hovercard-subject-tag | issue:119221398 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/NativeScript/android/283/issue_layout |
| twitter:image | https://opengraph.githubassets.com/553a950b51ed216ef725470df4161210a69ae2fd421fef60301d0fde9b3e8230/NativeScript/android/issues/283 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/553a950b51ed216ef725470df4161210a69ae2fd421fef60301d0fde9b3e8230/NativeScript/android/issues/283 |
| og:image:alt | Overview In Android development it is a standard practice to use inheritance to create custom activities, applications and other components. Currently NativeScript runtime for Android ({N}) has lim... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | slavchev |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/NativeScript/android git https://github.com/NativeScript/android.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 29137950 |
| octolytics-dimension-repository_nwo | NativeScript/android |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 29137950 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/android |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width