{"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=\"span-reading-time 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=\"span-reading-time 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_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[212,4,180,207],"tags":[153,292,181],"class_list":["post-1713","post","type-post","status-publish","format-standard","hentry","category-android-2","category-delphi","category-oxygene","category-xe5","tag-android","tag-delphi","tag-oxygene-2"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-rD","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1713","targetHints":{"allow":["GET"]}}],"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}]}}