<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kotlin &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/kotlin/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Tue, 01 Oct 2024 15:30:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Android Declare and Library Enhancements</title>
		<link>https://blog.xojo.com/2024/10/01/android-declare-and-library-enhancements/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Tue, 01 Oct 2024 15:30:45 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[2024r3]]></category>
		<category><![CDATA[Declares]]></category>
		<category><![CDATA[Kotlin]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=13442</guid>

					<description><![CDATA[There have been several enhancements to Declare and Kotlin Library support in Xojo 2024 Release 3 which, along with previous improvements, should allow you to&#8230;]]></description>
										<content:encoded><![CDATA[
<p>There have been several enhancements to Declare and Kotlin Library support in Xojo 2024 Release 3 which, along with previous improvements, should allow you to interface your Android projects with more from the native ecosystem.</p>



<p>This post describes the things you can do with Declares and Libraries with Android projects.</p>



<h2 class="wp-block-heading">Declare Integer Types</h2>



<p>The size-specific Integer types are now precise when used with Declares. The mappings are as follows:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td><strong>Xojo Type</strong></td><td><strong>Kotlin Type</strong></td></tr><tr><td>Int8, Byte</td><td>Byte</td></tr><tr><td>UInt8</td><td>UByte</td></tr><tr><td>Int16</td><td>Short</td></tr><tr><td>UInt16</td><td>UShort</td></tr><tr><td>Int32</td><td>Int</td></tr><tr><td>UInt32</td><td>UInt</td></tr><tr><td>Integer, Int64</td><td>Long</td></tr><tr><td>UInteger, UInt64</td><td>ULong</td></tr></tbody></table></figure>



<p>When mapping to Android API functions or Library functions, be sure to use the correct Xojo type that matches what is expected on Android. By far you will use Int32 and Integer.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>If you previously created Declares, you will likely need to change usage of Integer to Int32 as the mapping for that have changed compared to prior versions.</p>
</blockquote>



<h2 class="wp-block-heading">Nullable Types</h2>



<p>Xojo object variables (also properties, parameters and return values) can be Nil. With Kotlin, the default is that these things cannot be null (the equivalent of Nil). To ensure that you don’t get compile errors with your parameters, declare them as nullable in Kotlin by appending the “?” to the end of the type name.</p>



<h2 class="wp-block-heading">Application Context</h2>



<p>Many Android APIs require the application context. This is not available in a Library so it has to be supplied by the app itself. There is a property for this:</p>



<pre class="wp-block-code"><code>MobileApplication.AndroidContextHandle As Ptr</code></pre>



<p>You can pass this value to a function on the Library that then either saves it or uses it directly.</p>



<h2 class="wp-block-heading">Using Ptr</h2>



<p>Xojo uses the Ptr type to interface with Declares, however Kotlin does not have a Ptr type. Instead the Any type is used with Kotlin. This means that if you are passing something that is a Ptr to Kotlin (such as the app context above), your library function declaration should use “Any?” as the type of the parameter.</p>



<p>In your function, cast the parameter to the actual type you want. The same rule applies to return values. If the return value in the Declare is a Ptr, then in Kotlin the function declaration should be “Any?”.</p>



<p>You can also use Ptr with API calls to save object references as described below.</p>



<h2 class="wp-block-heading">Library Permissions</h2>



<p>Depending on the API you are using, you may need to add permissions to the app manifest.xml for the Library. These permissions also need to be applied to the main app. This can be done using the Advanced tab of the Android Build Settings. There you’ll find the Permissions property in the Inspector where you can add one Permission per line.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Previous versions let you separate permissions by spaces or commas. Going forward, each permission must be on its own line. Empty lines are ignored.</p>
</blockquote>



<p>There you can add the permission constants that are required by your Library. In addition, you can follow the constant with additional attributes and they will also be applied to the app’s manifest file.</p>



<p>For example, to include the android:maxSdkVersion attribute with the BLUETOOTH permission:</p>



<pre class="wp-block-code"><code>android.permission.BLUETOOTH android:maxSdkVersion="30"</code></pre>



<h2 class="wp-block-heading">Library Dependencies</h2>



<p>Similarly to permissions, your Library may make use of additional dependencies, which could even be other Libraries. These are added to the build.gradle file for the Library, but also need to be included in the main app.</p>



<p>You can add these dependencies using the Dependencies property on the Advanced Android Build Settings.</p>



<h2 class="wp-block-heading">Creating Objects</h2>



<p>Previous versions of Xojo only let you call Companion (essentially shared) methods on Library classes. Now you can also work with class instances and call instance methods. This gives you even better control and ability to interface with Android APIs.</p>



<p>To create an object, your Library class should have a factory method on the Companion that returns a new instance. It is clearest to use create() as this method name.</p>



<p>In Xojo you create a Declare to this create() method and call it, saving the result in a Ptr.</p>



<pre class="wp-block-code"><code>Declare Function create Lib "com.example.utility.counter" () As Ptr<br>Var counter As Ptr = create</code></pre>



<p>Instead of using a shared factory method, you can also call the Class constructor using this syntax:</p>



<pre class="wp-block-code"><code>Declare Function counter Lib "com.example.utility.counter" () As Ptr</code></pre>



<p>Because the function name has the same name as the class itself, this will call the constructor of the class.</p>



<p>When you want to call a method on the instance, you pass the instance as a Ptr. First, Declare to the method adding “.instance” to the library location to indicate you are calling a class instance.</p>



<pre class="wp-block-code"><code>Declare Function increment Lib "com.example.utility.counter.instance" (ref As Ptr) As Integer</code></pre>



<p>Then you can call the method and save its value.</p>



<pre class="wp-block-code"><code>Var count As Integer = increment(counter)</code></pre>



<p>The reference as the Ptr must be the first parameter passed in the Declare. Follow it with other parameters as usual.</p>



<p>You can also call instance methods directly in the Android API in this manner.</p>



<h2 class="wp-block-heading">Method Callbacks</h2>



<p>There are Android APIs that use callbacks to methods that you provide. In Xojo these methods are Delegates and are methods that you supply using <a href="https://documentation.xojo.com/api/language/addressof.html#addressof" target="_blank" rel="noreferrer noopener">AddressOf</a>. It is very important that the signature of this method exactly matches what the callback expects which means you’ll be limited to just the Boolean and Ptr types.</p>



<p>If you want to use an API that uses its own callbacks, you’ll be better served by creating a Library and having the API do the callback to your own Library methods, which you can then call back to your Xojo methods.</p>



<p>You can pass the reference to the Xojo method using AddressOf like this:</p>



<pre class="wp-block-code"><code>Var cb As Ptr = AddressOf TestCallback<br>Declare Sub xojocallback Lib "com.example.utility.callback" (cb As Ptr)<br>xojocallback(cb)</code></pre>



<p>The Xojo method can only use parameter types of Boolean and Ptr. It looks like this:</p>



<pre class="wp-block-code"><code>Public Sub TestCallback(b As Boolean, i As Ptr, s As Ptr)</code></pre>



<p>In the Kotlin library you have to cast the incoming parameter to a function reference. This is a two-step process where you verify the function has the correct number of parameters and then you cast it to the specific parameter types. Sample code:</p>



<pre class="wp-block-code"><code>if (cb is Function3&lt;*, *, *, *&gt;) {
    try {
         (cb as Function3&lt;Boolean, Long, String, Unit&gt;).invoke(true, 42, "Hello")
         println("Sent callback to Xojo")
    } catch (e: ClassCastException) {
         println("Casting failed: ${e.message}")
    }
}</code></pre>



<p>The &#8220;Function3&#8221; indicates a function with 3 parameters (the last &#8220;*&#8221; indicates the return type), but you can change that as needed using Function1, Function2, etc. You can pass anything back through a Ptr type, but the Xojo code has to convert them using Ptr methods so you should not change the types of the values you send back to be different than what your Xojo code expects.</p>



<h2 class="wp-block-heading">Object Declares</h2>



<p>Object Declares are not new, but for completeness are described here. Essentially an Object Declare uses special syntax to Declare to a UI control.</p>



<p>The typical way to use an Object Declare is by adding an Extends method to a Module. For example, an extension method to allow MobileButton to change its colors could look like this:</p>



<pre class="wp-block-code"><code>SetBackColor(Extends ctrl As MobileButton, c As Color)</code></pre>



<p>The Object Declare looks like this:</p>



<pre class="wp-block-code xojo"><code>Declare Sub setBackgroundColor Lib "Object:ctrl:MobileButton" (myColor As Int32)
setBackgroundColor(c.ToInteger)</code></pre>



<p>Breaking this down, we’re calling the setBackgroundColor function. The Lib denotes this new type of Object declare: an Object, separated by a colon, the Xojo name of the object (in this case, our “ctrl” parameter), followed by another colon, and the Xojo type of the object.</p>



<h2 class="wp-block-heading">Kotlin Declares</h2>



<p>You can use the Android-specific  &#8220;Kotlin&#8221; Declare designation to indicates that what is specified in the Alias section of the Declare should be used as is. This is particularly helpful when you want to cast Ptr values to a specific type for use with OS APIs.</p>



<p>This code gets a ColorStateList object for a Xojo Color value:</p>



<pre class="wp-block-code"><code>Var c As Color = Color.Blue

Declare Function valueOf Lib "android.content.res.ColorStateList:Kotlin" Alias _
"android.content.res.ColorStateList.valueOf(android.graphics.Color.argb(alpha.toInt(), r.toInt(), g.toInt(), b.toInt()))" _
(alpha As Int32, r As Int32, g As Int32, b As Int32) As Ptr

Var csl As Ptr = valueOf(255 - c.Alpha, c.Red, c.Green, c.Blue)</code></pre>



<p>This code can then be used to call <a href="https://developer.android.com/reference/com/google/android/material/button/MaterialButton#setStrokeColor(android.content.res.ColorStateList)" target="_blank" rel="noreferrer noopener">setStrokeColor</a> on a MobileButton:</p>



<pre class="wp-block-code"><code>Declare Sub import Lib "android.content.res.ColorStateList:Import" // Imports the ColorStateList class

Declare Sub setStrokeColor Lib "Object:ctrl:MobileDateTimePicker:Kotlin" Alias "setStrokeColor(strokecolor as ColorStateList)" (strokeColor As Ptr)
setStrokeColor(csl)</code></pre>



<p>As you can see, in the Alias is the Kotlin method call that casts the incoming Ptr value to a ColorStateList.</p>



<h3 class="wp-block-heading">Import</h3>



<p>Note the &#8220;import&#8221; declare in the code snippet above. This is also specific to Android. This Declare indicates that a specific class should be imported so that it can be used for casting purposes. If you left off that import, then the cast to ColorStateList would cause a compile error because ColorStateList would not be known.</p>



<p>You only need to import a specific class once. After you&#8217;ve done so it can be referenced by any other Declare, even ones in different methods.</p>



<p>The use of an import Declare is optional and is meant to simply the Kotlin method call code in the Alias. Instead of using import you can specify the full class path in the cast like this:</p>



<pre class="wp-block-code xojo"><code>Declare Sub setStrokeColor Lib "Object:ctrl:MobileDateTimePicker:Kotlin" Alias "setStrokeColor(strokecolor as android.content.res.ColorStateList)" (strokeColor As Ptr)
setStrokeColor(csl)</code></pre>



<h2 class="wp-block-heading">Handles</h2>



<p>Many classes in the framework now have Handle properties (or methods) which you can use with various Declare techniques described above. These include: FolderItem, Font, GraphicsPath, Locale, TimeZone, Graphics, Picture. In addition, the mobile controls have Handle properties that can serve as an alternative for an Object Declare.</p>



<h2 class="wp-block-heading">Sample Project</h2>



<p>You can download the sample project that demonstrates a library and some of the concepts described above. You can use this as a template or starting point for creating your own libraries or Declares.</p>



<p><a href="https://files.xojo.com/BlogExamples/UtilityLibrary2.zip" target="_blank" rel="noreferrer noopener">Sample Utility Library Project</a></p>



<p>You might also want to take a look at the <a href="https://github.com/XojoGermany/AndroidDesignExtensions" target="_blank" rel="noreferrer noopener">Android Design Extensions open source project</a>, which has hundreds of Declares that can enhance your Android apps.</p>



<h2 class="wp-block-heading">References</h2>



<ul class="wp-block-list">
<li><a href="https://blog.xojo.com/2023/08/15/creating-an-android-library-with-kotlin-for-use-with-xojo/" target="_blank" rel="noreferrer noopener">Android Libraries blog post</a></li>



<li><a href="https://blog.xojo.com/2023/08/09/android-declares/" target="_blank" rel="noreferrer noopener">Android Declares blog post</a></li>
</ul>



<p><em>Paul learned to program in BASIC at age 13 and has programmed in more languages than he remembers, with Xojo being an obvious favorite. When not working on Xojo, you can find him talking about retrocomputing at <a href="https://goto10.substack.com" target="_blank" rel="noreferrer noopener">Goto 10</a> and </em>on Mastodon @lefebvre@hachyderm.io.</p>



<ul class="wp-block-social-links has-normal-icon-size is-content-justification-center is-layout-flex wp-container-core-social-links-is-layout-16018d1d wp-block-social-links-is-layout-flex"><li class="wp-social-link wp-social-link-facebook  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.facebook.com/goxojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12 2C6.5 2 2 6.5 2 12c0 5 3.7 9.1 8.4 9.9v-7H7.9V12h2.5V9.8c0-2.5 1.5-3.9 3.8-3.9 1.1 0 2.2.2 2.2.2v2.5h-1.3c-1.2 0-1.6.8-1.6 1.6V12h2.8l-.4 2.9h-2.3v7C18.3 21.1 22 17 22 12c0-5.5-4.5-10-10-10z"></path></svg><span class="wp-block-social-link-label screen-reader-text">Facebook</span></a></li>

<li class="wp-social-link wp-social-link-x  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://x.com/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M13.982 10.622 20.54 3h-1.554l-5.693 6.618L8.745 3H3.5l6.876 10.007L3.5 21h1.554l6.012-6.989L15.868 21h5.245l-7.131-10.378Zm-2.128 2.474-.697-.997-5.543-7.93H8l4.474 6.4.697.996 5.815 8.318h-2.387l-4.745-6.787Z" /></svg><span class="wp-block-social-link-label screen-reader-text">X</span></a></li>

<li class="wp-social-link wp-social-link-linkedin  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.linkedin.com/company/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M19.7,3H4.3C3.582,3,3,3.582,3,4.3v15.4C3,20.418,3.582,21,4.3,21h15.4c0.718,0,1.3-0.582,1.3-1.3V4.3 C21,3.582,20.418,3,19.7,3z M8.339,18.338H5.667v-8.59h2.672V18.338z M7.004,8.574c-0.857,0-1.549-0.694-1.549-1.548 c0-0.855,0.691-1.548,1.549-1.548c0.854,0,1.547,0.694,1.547,1.548C8.551,7.881,7.858,8.574,7.004,8.574z M18.339,18.338h-2.669 v-4.177c0-0.996-0.017-2.278-1.387-2.278c-1.389,0-1.601,1.086-1.601,2.206v4.249h-2.667v-8.59h2.559v1.174h0.037 c0.356-0.675,1.227-1.387,2.526-1.387c2.703,0,3.203,1.779,3.203,4.092V18.338z"></path></svg><span class="wp-block-social-link-label screen-reader-text">LinkedIn</span></a></li>

<li class="wp-social-link wp-social-link-github  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://github.com/topics/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12,2C6.477,2,2,6.477,2,12c0,4.419,2.865,8.166,6.839,9.489c0.5,0.09,0.682-0.218,0.682-0.484 c0-0.236-0.009-0.866-0.014-1.699c-2.782,0.602-3.369-1.34-3.369-1.34c-0.455-1.157-1.11-1.465-1.11-1.465 c-0.909-0.62,0.069-0.608,0.069-0.608c1.004,0.071,1.532,1.03,1.532,1.03c0.891,1.529,2.341,1.089,2.91,0.833 c0.091-0.647,0.349-1.086,0.635-1.337c-2.22-0.251-4.555-1.111-4.555-4.943c0-1.091,0.39-1.984,1.03-2.682 C6.546,8.54,6.202,7.524,6.746,6.148c0,0,0.84-0.269,2.75,1.025C10.295,6.95,11.15,6.84,12,6.836 c0.85,0.004,1.705,0.114,2.504,0.336c1.909-1.294,2.748-1.025,2.748-1.025c0.546,1.376,0.202,2.394,0.1,2.646 c0.64,0.699,1.026,1.591,1.026,2.682c0,3.841-2.337,4.687-4.565,4.935c0.359,0.307,0.679,0.917,0.679,1.852 c0,1.335-0.012,2.415-0.012,2.741c0,0.269,0.18,0.579,0.688,0.481C19.138,20.161,22,16.416,22,12C22,6.477,17.523,2,12,2z"></path></svg><span class="wp-block-social-link-label screen-reader-text">GitHub</span></a></li>

<li class="wp-social-link wp-social-link-youtube  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.youtube.com/c/XojoInc" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M21.8,8.001c0,0-0.195-1.378-0.795-1.985c-0.76-0.797-1.613-0.801-2.004-0.847c-2.799-0.202-6.997-0.202-6.997-0.202 h-0.009c0,0-4.198,0-6.997,0.202C4.608,5.216,3.756,5.22,2.995,6.016C2.395,6.623,2.2,8.001,2.2,8.001S2,9.62,2,11.238v1.517 c0,1.618,0.2,3.237,0.2,3.237s0.195,1.378,0.795,1.985c0.761,0.797,1.76,0.771,2.205,0.855c1.6,0.153,6.8,0.201,6.8,0.201 s4.203-0.006,7.001-0.209c0.391-0.047,1.243-0.051,2.004-0.847c0.6-0.607,0.795-1.985,0.795-1.985s0.2-1.618,0.2-3.237v-1.517 C22,9.62,21.8,8.001,21.8,8.001z M9.935,14.594l-0.001-5.62l5.404,2.82L9.935,14.594z"></path></svg><span class="wp-block-social-link-label screen-reader-text">YouTube</span></a></li></ul>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Android Improvements in Xojo 2023r3</title>
		<link>https://blog.xojo.com/2023/10/10/android-improvements-in-xojo-2023r3/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Tue, 10 Oct 2023 13:30:44 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[2023r3]]></category>
		<category><![CDATA[Declares]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Kotlin]]></category>
		<category><![CDATA[Multi-Platform Development]]></category>
		<category><![CDATA[Rapid Application Development]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=12123</guid>

					<description><![CDATA[Android remains in Beta, but there have been many fixes and improvements in Xojo 2023r3 and I'd like to highlight a few including: more support for dark mode, additions to MobileTextField, a Destination property, a HasBackButton property, MobilePopupMenu, enhancements to MobileHTMLViewer, support for running JavaScript, the new Kotlin Declare and more detailed in this post.]]></description>
										<content:encoded><![CDATA[
<p>Android remains in Beta, but there have been many fixes and improvements in Xojo 2023r3 and I&#8217;d like to highlight a few including: more support for dark mode, additions to MobileTextField, a Destination property, a HasBackButton property, MobilePopupMenu, enhancements to MobileHTMLViewer, support for running JavaScript, the new Kotlin Declare and more.</p>



<p>Although Xojo for Android does not yet have full dark mode support, things continue to head in that direction. Previously your app would adjust its UI for light/dark if the Support Dark Mode property was turned on in the Android Build Settings. Starting in Xojo 2023r3, Color.TextColor and Color.FillColor also adjust their values when run in light mode or dark mode, making it easier for your custom drawing to also adapt.</p>



<p>MobileTextField gets a couple new additions. The ReturnPressed event is now called when, as the name suggests, return is pressed. The AllowSpellChecking property is now also available for MobileTextField and MobileTextArea.</p>



<p>In the Shared Build Settings Inspector there is now a Destination property as there is with other platforms.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="377" height="434" src="https://blog.xojo.com/wp-content/uploads/2023/10/image.png" alt="" class="wp-image-12124" srcset="https://blog.xojo.com/wp-content/uploads/2023/10/image.png 377w, https://blog.xojo.com/wp-content/uploads/2023/10/image-261x300.png 261w" sizes="(max-width: 377px) 100vw, 377px" /></figure>



<p>Many Android devices have physical back buttons and all let you turn on global software controls that have a back button, but sometimes you want to have the back button clearly shown on the navigation toolbar. With Xojo 2023r3, the new HasBackButton property of MobileScreen can show a back button on the navigation toolbar.</p>



<p>To make it easier to write code that is applicable to both iOS and Android, but not other platforms, there is a new <a href="https://documentation.xojo.com/api/compiler_directives/targetmobile.html">TargetMobile</a> constant available for conditional compilation.</p>



<p><a href="https://documentation.xojo.com/api/user_interface/mobile/mobilepopupmenu.html">MobilePopupMenu</a> is an all-new control for Android that makes it possible to simplify some of your user interfaces. It works similarly to a PopupMenu on Desktop or Web: when the user taps this control a list of items appears and they can choose one.</p>



<p>On the Advanced tab of the Android Build Settings there is a new Manifest section with a Permissions property. Here you can specify the names of <a href="https://developer.android.com/reference/android/Manifest.permission">Android permission constants</a> that are needed by Declares or libraries. They are added to the AndroidManifest.xml file when the app is run/built.</p>



<p>MobileHTMLViewer got several enhancements. It now has various processing events, including: DocumentBegin, DocumentComplete, CancelLoad, Error, NewWindow, DocumentProgressChanged and TitleChanged.</p>



<p>And there is now support for running JavaScript using the ExecuteJavaScript method along with the JavaScriptResult and JavaScriptRequest events. ExecuteJavaScript is asynchronous and results appear in the JavaScriptResult event. To call the JavaScriptRequest event, use xojo.execute() in the JavaScript code. A method name and up to 5 parameters can be passed back to Xojo. <a href="https://documentation.xojo.com/api/user_interface/mobile/mobilehtmlviewer.html#mobilehtmlviewer-javascriptrequest">Learn more</a> in the Xojo Programming Documentation.</p>



<p>The last thing I want to highlight is the new Kotlin <a href="https://documentation.xojo.com/api/language/declare.html">Declare</a>. For advanced users, this adds the ability to create Android declares that can pass untouched calls via Alias (with the exception of required type/object handling) to the Kotlin compilation step. This is done by specifying Lib &#8220;Kotlin&#8221; or adding &#8220;:Kotlin&#8221; to the end of Lib. Here are a couple examples of what this can look like:</p>



<pre class="wp-block-code"><code>Declare Function AndroidVersion Lib "Kotlin" Alias "android.os.Build.VERSION.RELEASE" As CString</code></pre>



<pre class="wp-block-code"><code>Declare Sub findAllAsync Lib "Object:ctrl:MobileHTMLViewer:Kotlin" Alias "findAllAsync(myfind.toString())" (myFind As CString)</code></pre>



<p>Of course there were many bug fixes as well. Be sure to check out the <a href="https://documentation.xojo.com/versions/2023r3/resources/release_notes/2023r3.html">full release notes</a> for all the details.</p>



<p><em>Paul learned to program in BASIC at age 13 and has programmed in more languages than he remembers, with Xojo being an obvious favorite. When not working on Xojo, you can find him talking about retrocomputing at <a href="https://goto10.substack.com" target="_blank" rel="noreferrer noopener">Goto 10</a> and </em>on Mastodon @lefebvre@hachyderm.io.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Creating an Android Library with Kotlin for use with Xojo</title>
		<link>https://blog.xojo.com/2023/08/15/creating-an-android-library-with-kotlin-for-use-with-xojo/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Tue, 15 Aug 2023 20:11:14 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Declares]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Kotlin]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=11913</guid>

					<description><![CDATA[In this first beta release of Android, there is some simple support for creating Android libraries and calling their methods from Xojo code. The Android libraries have to be written in Kotlin and compiled as AAR library files. Xojo code can call into the library using Declares.]]></description>
										<content:encoded><![CDATA[
<p>In this first beta release of Android, there is some simple support for creating Android libraries and calling their methods from Xojo code. The Android libraries have to be written in Kotlin and compiled as AAR library files. Xojo code can call into the library using Declares.</p>



<p>To create an Android library you will need to use Android Studio, which you should already have installed as its compiler toolchain and SDK are required by Xojo.</p>



<p>Create a New Android project (File-&gt;New-&gt;New Project), choosing “No Activity” from the Phone and Tablet section.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1024x800.png" alt="" class="wp-image-11914" style="width:623px;height:487px" width="623" height="487" srcset="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1024x800.png 1024w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-300x234.png 300w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-768x600.png 768w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1536x1201.png 1536w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic.png 2024w" sizes="(max-width: 623px) 100vw, 623px" /></figure>



<p>You should be sure to choose Kotlin as the language and the Minimum SDK should be 26 to match what Xojo uses. For this example, I’ve named the project “LibraryTest”.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1-1024x800.png" alt="" class="wp-image-11916" style="width:634px;height:495px" width="634" height="495" srcset="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1-1024x800.png 1024w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1-300x234.png 300w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1-768x600.png 768w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1-1536x1201.png 1536w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-1.png 2024w" sizes="(max-width: 634px) 100vw, 634px" /></figure>



<p>In this project, add a module (File-&gt;New-&gt;New Module), choosing the “Android Library” template. Here you can name your library, choose the language, etc. Again, stick with Kotlin.</p>



<p>I’m naming the library “utility” so that it can contain a random collection of things.</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-3-1024x776.png" alt="" class="wp-image-11917" style="width:656px;height:497px" width="656" height="497" srcset="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-3-1024x776.png 1024w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-3-300x227.png 300w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-3-768x582.png 768w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-3-1536x1163.png 1536w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-3.png 1682w" sizes="auto, (max-width: 656px) 100vw, 656px" /></figure>



<p>In the Android Studio navigator, expand the utility library and then the java folder. Yes, it is weird that Kotlin code is in a Java folder, but a lot about Android Studio is weird to me.</p>



<p>Right-click on “com.example.utility” and select New-&gt;Kotlin Class/File. In the selector, choose “Class” and type the name of the class as “regex” and press return. In this class will be a method to do some simple regex pattern matching.</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" src="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-4.png" alt="" class="wp-image-11918" style="width:276px;height:211px" width="276" height="211" srcset="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-4.png 678w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-4-300x229.png 300w" sizes="auto, (max-width: 276px) 100vw, 276px" /></figure>



<p>This creates an empty class:</p>



<pre id="xojo" class="wp-block-code"><code><code>package<strong> </strong>com.example.utility
class regex {

}</code></code></pre>



<p>Currently Xojo can only talk to methods in the companion class. These methods are similar to Shared methods in Xojo and work without creating a specific class instance.</p>



<p>In the class, add the companion object so that it looks like this:</p>



<pre id="Xojo" class="wp-block-code"><code><code>package com.example.utility

class regex {

    companion object {

    }

}</code></code></pre>



<p>Inside the companion object, add a method that will do simple regex pattern matching. Your code now looks like this:</p>



<pre id="Xojo" class="wp-block-code"><code><code>package com.example.utility

class regex {

    companion object {

        fun regexmatch(regex: String, text: String): Boolean {

            // Use the regex pattern
            val pattern = Regex(regex)

            // Returns true if it matches, false if it does not.
            return pattern.matches(text)
        }

    }

}</code></code></pre>



<p>The last step is to change the Kotlin version to match the version Xojo uses. In the Android Studio navigator, open Gradle Scripts and double-click “build.gradle (Project: LibraryTest)” to open it.</p>



<p>Change the last line with the kotlin.android version from whatever is there to “1.6.20”:</p>



<pre id="xojo" class="wp-block-code"><code><code>// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
    id 'com.android.application' version '8.0.2' apply false
    id 'com.android.library' version '8.0.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.20' apply false
}</code></code></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>Note</strong>: You may be prompted to do a Gradle sync after making this change. Go ahead and do the sync if prompted.</p>
</blockquote>



<p>The last thing to do in Android Studio is to build the library. From the Build menu, choose “Select Build Variant”. In the Build Variants panel that appears, click on the Active Build Variant value for the utility row and change it from debug to release.</p>



<p>Select Build-&gt;Rebuild Project to build the library file that Xojo will use.</p>



<p>In your file system, go to your project’s location and fine the aar file, which is located here: LibraryTest/utility/build/outputs/aar/utility-release.aar Make a note of this as we will be using it with Xojo in a moment.</p>



<p>Speaking of Xojo, create a new Xojo Android project and name it LibraryTest. Add a button and a table like so:</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-5-530x1024.png" alt="" class="wp-image-11919" style="width:295px;height:569px" width="295" height="569" srcset="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-5-530x1024.png 530w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-5-155x300.png 155w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-5-768x1484.png 768w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-5-795x1536.png 795w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-5.png 858w" sizes="auto, (max-width: 295px) 100vw, 295px" /></figure>



<p>In the button’s pressed event, <a href="https://blog.xojo.com/2023/08/09/android-declares/">write the Declare statements</a> to call the regexmatch method in the library, passing it a regex pattern and a string to test. Here is the code, which tests three strings and outputs the result to the table:</p>



<pre id="Xojo" class="wp-block-code"><code><code>Declare Function regexmatch Lib "com.example.utility.regex" (pattern As CString, value As CString) As Boolean

// Look for g, followed by any number of "ee" pairs and ending with ks
Var pattern As String = "g(&#91;ee]+)ks?"
Var result As Boolean
Var values() As String = Array("geeks", "geeeeeeeeeeks", "geeksforgeeks")

For Each v As String In values
  result = regexmatch(pattern, v)
  Table1.AddRow(v, result.ToString)
Next

// From: https://www.geeksforgeeks.org/kotlin-regular-expression/</code></code></pre>



<p>The important thing to note in this code is the use of CString in place of String, a requirement when working with a Declare even if it doesn’t directly apply to Android.</p>



<p>Also note that you need to provide the full path to the class. You have to use “com.example.utility.regex”, not just “regex”.</p>



<p>The next step is to add the Library to the Xojo project. Select Android in the Build Settings and add a Copy Files Build Step (Insert-&gt;Build Step-&gt;Copy Files).</p>



<p>Expand Android and click on CopyFiles1. Rename it to LibCopy and drag it so that it is before the Build entry. Change the Destination to “Framework Folder”.</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" src="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-7.png" alt="" class="wp-image-11920" style="width:271px;height:157px" width="271" height="157" srcset="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-7.png 442w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-7-300x174.png 300w" sizes="auto, (max-width: 271px) 100vw, 271px" /></figure>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" src="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-8.png" alt="" class="wp-image-11921" style="width:319px;height:211px" width="319" height="211" srcset="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-8.png 586w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-8-300x199.png 300w" sizes="auto, (max-width: 319px) 100vw, 319px" /></figure>



<p>Add the library that we built with Android Studio to this LibCopy step. Click “+” in the command bar and locate utility-release.aar.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>Note</strong>: If you still have Android Studio open at this point, you should close it, otherwise it will lock debugger access and prevent the Xojo app from starting.</p>
</blockquote>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>Pro Tip</strong>: You can keep Android Studio open and run from Xojo by choosing this preference setting: Build, Execution, Deployment -&gt; Debugger -&gt; Use existing manually managed server. You’ll want to set it back to “Automatically start and manage the server” if you use Android Studio for anything else. Constantly flipping that setting is common when working with Android libraries.</p>
</blockquote>



<p>Run the project. If you did everything correctly, it should launch on your device or emulator. Click the button to see the output which should look like this:</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-6-930x1024.png" alt="" class="wp-image-11923" style="width:430px;height:473px" width="430" height="473" srcset="https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-6-930x1024.png 930w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-6-272x300.png 272w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-6-768x846.png 768w, https://blog.xojo.com/wp-content/uploads/2023/08/Pasted-Graphic-6.png 964w" sizes="auto, (max-width: 430px) 100vw, 430px" /></figure>



<p>With libraries you can add your own extensions to Xojo’s Android support. You are limited to simple types at the moment, but that still opens up a lot of capabilities. This capability will continue to be expanded through the Android beta.</p>



<p>Feel free to share any Kotlin libraries you create in the Android topic of the forum.</p>



<p><a href="https://files.xojo.com/BlogExamples/UtilityLibrary.zip">Download this Example RegEx Library</a></p>



<p>Here is <a href="https://files.xojo.com/BlogExamples/LibTest.zip">another simple Library</a> you can download and examine.</p>



<p><em>Paul learned to program in BASIC at age 13 and has programmed in more languages than he remembers, with Xojo being an obvious favorite. When not working on Xojo, you can find him talking about retrocomputing at <a href="https://goto10.substack.com" target="_blank" rel="noreferrer noopener">Goto 10</a> and </em>on Mastodon @lefebvre@hachyderm.io.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
