1

Is it possible to include JavaScript file and call methods inside it on Pure Java/Kotlin Android Application?

I am working on Android Application, using Java and Kotlin. But I am wondering how I can actually include a JavaScript file/Code into my application as to handle some of my logic in JavaScript.

Background: I have implemented some functionality on my Web Application which off course uses JavaScript and JQuery, now on Java and Kotlin World I am not seeing such functionality being possible.

I did deep research for weeks but its not so Now I want a way where I can include some of JavaScript file to handle some logic on my Java/Kotlin App not having to use PhoneGap and Cordova.

Community
  • 1
  • 1

1 Answers1

3

It is not a good idea to add some Javascript to your Java application. You shouldn't mix programming language like that. If you really want Javascript to run inside your Java app, you would need to emulate a web view and run this Javascript inside. But that's exactly what cordova is doing so at this point just use this instead.

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • :) Well so in actual sense I can have my Pure Java/Kotlin App, then implement web view so that in such web view I can implement my JavaScript code? – nasiphi vinqishe Nov 07 '19 at 13:30
  • it is possible, but not recommended. Now that's your call. – Nicolas Nov 07 '19 at 13:32
  • Thank you @Nic a lot, this is little implementation I want so I think it won't cause much damage but I guess again when we looking at Security, the CORS it will cause some issues because I want this web view to be able to send some REST API call to server, I am not sure if I am right. – nasiphi vinqishe Nov 07 '19 at 13:34
  • 1
    Oh, you don't really need to implements Javascript to do REST Call, if that's all you want. See [this answer](https://stackoverflow.com/questions/46177133/http-request-in-kotlin) to get more information on how to create an http client on kotlin. – Nicolas Nov 07 '19 at 13:37
  • 1
    No @Nic I need to Implement this in JavaScript, then send Ajax call to server, then return back from Web View to Android Activity. So I am wondering if sending Ajax Call from the web view webView.loadUrl("file:///myPath/myFile.html"); won't pose CORS warnings when I send such Ajax call – nasiphi vinqishe Nov 07 '19 at 13:41
  • I'm not sure how CORS will work in such an environnement. I encourage you to try it out and maybe update your question to add more information about this. It will greatly helps other is the same situation as you. – Nicolas Nov 07 '19 at 13:43
  • Thanks @Nic, let me do that. – nasiphi vinqishe Nov 07 '19 at 13:44