{"id":1713,"date":"2013-09-20T16:08:37","date_gmt":"2013-09-20T04:08:37","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=1713"},"modified":"2013-09-20T16:42:30","modified_gmt":"2013-09-20T04:42:30","slug":"how-to-call-java-code-from-an-oxygene-android-application","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/1713\/","title":{"rendered":"How to Call Java Code from an Oxygene Android Application"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">2<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p><a href=\"http:\/\/members.adug.org.au\/2013\/09\/20\/how-to-call-java-code-from-an-xe5-android-application\/\">Lachlan just posted<\/a> a link to a <a href=\"https:\/\/plus.google.com\/117591599539574616466\/posts\/Tc7viMithGi\">post on Google+<\/a> (also available as a <a href=\"https:\/\/docs.google.com\/file\/d\/0By80N3FuOKnkTXBrWGdkM0xYYW8\/edit?hl=en-GB&#038;forcehl=1\">PDF<\/a>) demonstrating how to call Java from Delphi XE5.<\/p>\n<p><!--more--><\/p>\n<p>I was shocked at both the <em>amount<\/em> and the <em>nature<\/em> of the code involved.  It is long, convoluted and ugly stuff (<em><strong>nb.<\/strong> that isn&#8217;t a criticism of Paul&#8217;s code; such techniques rarely if ever result in elegant solutions<\/em>) indeed sometimes requiring you to resort to using tools to reverse engineer the Java code.  Certainly not for the faint hearted.<\/p>\n<p>In <strong>Oxygene<\/strong> things are much simpler because when compiling with <strong>Oxygene for Java<\/strong>,  <strong>Oxygene<\/strong> code <strong><em>is<\/em><\/strong> <strong>Java<\/strong> code, it&#8217;s just not written in the Java <em>language<\/em>.<\/p>\n<h3>The Comparison<\/h3>\n<p>Paul Foster&#8217;s post ends with an example of calling a <strong>getIntValue<\/strong> and <strong>setIntValue<\/strong> method on some Java class that he wrote to test the technique.  For the purposes of providing a more convenient, side-by-side comparison I hope he doesn&#8217;t mind if I reproduce that last snippet here:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nvar\r\n  jLoadedClassID,jLoadedObjectID:JNIObject;\r\n  jGetMethod,jSetMethod:JNIMethodID;\r\n  JavaEnv:PJNIEnv;\r\n  jiReturnedValue:JNIInt;\r\n  jvValuetoSet:JNIValue;\r\nbegin\r\n  {...don\u2019t forget the other bits first!}\r\n\r\n  jiReturnedValue:=JavaEnv^.CallIntMethodA(JavaEnv, jLoadedObjectID,jGetMethod);\r\n\r\n  jvValuetoSet.i:=1234567890;\r\n  JavaEnv^.CallVoidMethodA(JavaEnv, jLoadedObjectID, jSetMethod,@jvValuetoSet);\r\nend;\r\n<\/pre>\n<p>As you can see, this isn&#8217;t complete &#8211; there&#8217;s pre-amble needed to make all this work and I encourage you to read his article in full to appreciate just how much and what is involved.<\/p>\n<p>By contrast, here&#8217;s the <strong>Oxygene<\/strong> code that achieves the same:<\/p>\n<pre class=\"brush: oxygene; title: ; notranslate\" title=\"\">\r\n  var\r\n    loadedObject: MyJavaClass;\r\n    returnedValue: Integer;\r\n  begin\r\n    \/\/ get the reference to loadedObject from somewhere ...\r\n\r\n    returnedValue := loadedObject.getIntValue;\r\n\r\n    loadedObject.setIntValue(1234567890);\r\n  end;\r\n<\/pre>\n<p>Or, if you prefer to take advantage of the fact that Oxygene recognises that <strong>get<\/strong>\/<strong>set<\/strong> methods can be treated as properties, and prefer the more Pascal-like syntax this permits:<\/p>\n<pre class=\"brush: oxygene; title: ; notranslate\" title=\"\">\r\n    returnedValue := loadedObject.IntValue;\r\n\r\n    loadedObject.IntValue := 1234567890;\r\n<\/pre>\n<h3>Important Firsts<\/h3>\n<p><strong>Oxygene for Java<\/strong> is just one part of <strong>Oxygene<\/strong> product.  When you purchase <strong>Oxygene<\/strong> you get:<\/p>\n<ul>\n<li>Oxygene for .NET (supports Windows RT)<\/li>\n<li>Oxygene for Java (and therefore, <em>but not only<\/em>, Android)<\/li>\n<li>Oxygene for Cocoa (OS X and iOS)<\/li>\n<\/ul>\n<p>In each case, the language is a <strong>first-class<\/strong> citizen of each platform, allowing you to learn new, portable skills on those platforms, taking <strong>full<\/strong> advantage and make full use of each platform&#8217;s capabilities using the platform SDK&#8217;s directly.<\/p>\n<p><strong>Oxygene for Cocoa<\/strong> has supported <strong>iOS 7<\/strong> since <strong>Apple<\/strong> started shipping the beta SDK to developers.<\/p>\n<p><strong>Oxygene for Android<\/strong> will support <strong>Kit-Kat<\/strong> as soon as <strong>Google<\/strong> starts shipping the SDK.<\/p>\n<p>If you enjoy Pascal and enjoy <a href=\"http:\/\/blog.marcocantu.com\/blog\/first_delphi_android.html\">firsts<\/a>, it&#8217;s hard to beat being one of the first developers able to target new platforms using your favoured language.<\/p>\n<h3>Not Cheap, But Certainly Affordable<\/h3>\n<p>A subscription to Oxygene, including <strong>all<\/strong> of the above, costs <strong>just $699<\/strong> (<em>$599 cross-grade offer for existing Delphi users<\/em>) with annual renewals of just <strong>$499<\/strong> thereafter.<\/p>\n<p>The <strong>Oxygene<\/strong> command line compiler is free.<\/p>\n<p>As is a <a href=\"http:\/\/www.remobjects.com\/oxygene\/downloads.aspx\">30-day trial<\/a>, if you are interested to take a look.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">2<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span> Lachlan just posted a link to a post on Google+ (also available as a PDF) demonstrating how to call Java from Delphi XE5.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[212,4,180,207],"tags":[153,292,181],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-rD","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1817,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1817\/","url_meta":{"origin":1713,"position":0},"title":"Getting the Battery Level on Android With Delphi","date":"01 Oct 2013","format":false,"excerpt":"Over the past few days I posted a two part series showing how to obtain the current battery level as part of the implementation of an Android AppWidget using Oxygene. As far as I can tell AppWidgets simply aren't possible using Delphi but reading the battery is quite straightforward Android\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1624,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1624\/","url_meta":{"origin":1713,"position":1},"title":"Exploring Listeners With Oxygene","date":"16 Sep 2013","format":false,"excerpt":"Part 2 in a short series demonstrating the development of a simple camera app for Android using Oxygene. In the previous instalment we looked at the basic framework of our app. For this instalment I was going to show how to implement the camera preview or viewfinder for this instalment,\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1525,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1525\/","url_meta":{"origin":1713,"position":2},"title":"RAD Studio in Auckland \/ Android in a VM \/ Touchy Feely","date":"30 Aug 2013","format":false,"excerpt":"Well, not really RAD Studio as such, more like Mobile Studio (whether as part of RAD Studio Ent+ or the Mobile Add-On) since everything shown was oriented around the Android support and emphasising the fact that an application written for iOS can simply be recompiled and will run on Android.\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"Androidx86","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Androidx86-1024x608.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1199,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1199\/","url_meta":{"origin":1713,"position":3},"title":"Oxygene &#8220;Nougat&#8221; is Mac and iOS !","date":"06 Sep 2012","format":false,"excerpt":"Marc Hoffman has confirmed that \"Nougat\" is to Mac\/iOS as \"Cooper\" was to Java. \u00a0Some have speculated that this will be based on Mono, but Oxygene has had Mono covered for some time already, so I strongly doubt that this is the case. Far more likely is that just as\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1882,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1882\/","url_meta":{"origin":1713,"position":4},"title":"Not Your Grand-Daddy&#8217;s Pascal (or Java)","date":"15 Oct 2013","format":false,"excerpt":"I've mentioned some of the cool stuff in the Oxygene language in various posts and thought it would be a good idea to list them again, along with some others that I've not previously mentioned. Oxygene Everywhere First some of the core language features that are available on all supported\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1845,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1845\/","url_meta":{"origin":1713,"position":5},"title":"Another Great Oxygene Release","date":"03 Oct 2013","format":false,"excerpt":"RemObjects have officially released the September update to Oxygene with the usual round of bug fixes and some dramatic improvements in the tool chain. Cocoa - Principally iOS 7 My focus with Oxygene most recently has been on the Android side. My previous experience with using the Cocoa (iOS) support\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1713"}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=1713"}],"version-history":[{"count":10,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1713\/revisions"}],"predecessor-version":[{"id":1723,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1713\/revisions\/1723"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}