<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Karl</title>
    <description>The latest articles on DEV Community by Karl (@kewp).</description>
    <link>https://dev.to/kewp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F95250%2F9980e922-429c-41ce-b895-36f7c2b80cf9.png</url>
      <title>DEV Community: Karl</title>
      <link>https://dev.to/kewp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kewp"/>
    <language>en</language>
    <item>
      <title>Build and Test a React Native App with TypeScript and OAuth 2.0</title>
      <dc:creator>Karl</dc:creator>
      <pubDate>Thu, 29 Nov 2018 05:00:00 +0000</pubDate>
      <link>https://dev.to/oktadev/build-and-test-a-react-native-app-with-typescript-and-oauth-2-0-21on</link>
      <guid>https://dev.to/oktadev/build-and-test-a-react-native-app-with-typescript-and-oauth-2-0-21on</guid>
      <description>&lt;p&gt;React Native is one of the most popular ways of creating mobile apps. Building on the success of React, it ties together native components for both Android and iOS using a shared JavaScript code-base. However, JavaScript has come under fire recently for not being type safe which can lead to a loss of developer trust. Enter TypeScript, which allows type annotations to be added to existing JavaScript code.&lt;/p&gt;

&lt;p&gt;One requirement of many mobile apps is authentication (aka authn - confirming user identity) and authorization (authz - confirming access rights). Implementing this through the OAuth 2.0 standard allows integration with all the major third-party authn / authz services. It’s also imperative to create automated tests to check that core functionality has not been broken.&lt;/p&gt;

&lt;p&gt;In this tutorial you’ll create an empty React Native project, add OAuth capabilities with &lt;a href="https://github.com/FormidableLabs/react-native-app-auth" rel="noopener noreferrer"&gt;React Native App Auth&lt;/a&gt;, port the project over to TypeScript, and finally add testing using the &lt;a href="https://reactjs.org/docs/test-renderer.html" rel="noopener noreferrer"&gt;React Test Renderer&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a React Native Project
&lt;/h2&gt;

&lt;p&gt;The latest version of the &lt;a href="https://facebook.github.io/react-native/docs/getting-started.html" rel="noopener noreferrer"&gt;official getting started guide&lt;/a&gt; recommends using &lt;a href="https://expo.io/" rel="noopener noreferrer"&gt;Expo&lt;/a&gt; to create initial projects, but I found Expo to be quirky to setup. The old way is using the command-line tool (CLI). Check out the &lt;strong&gt;Building Projects with Native Code&lt;/strong&gt; tab in the aforementioned guide to get details of setting this up.&lt;/p&gt;

&lt;p&gt;Once you have &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;installed Node&lt;/a&gt; (at least 8.3 - and I recommend using the LTS version) you should be able to install the React Native CLI with &lt;code&gt;npm&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; react-native-cli@2.0.1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once done you should have a command &lt;code&gt;react-native&lt;/code&gt; available to create a new project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;react-native init ReactNativeTypeScript

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install the latest version of React Native (&lt;a href="https://github.com/facebook/react-native/releases" rel="noopener noreferrer"&gt;currently 0.57.4&lt;/a&gt;) into the &lt;code&gt;ReactNativeTypeScript&lt;/code&gt; folder. Change into that folder and run &lt;code&gt;react-native run-android&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;ReactNativeTypeScript
react-native run-android

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don’t have a phone plugged in or an Android Virtual Device (AVD) running, you’ll see an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;com.android.builder.testing.api.DeviceException: No connected devices!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fix this, open Android Studio, and create a new AVD. You can do this by navigating to &lt;strong&gt;Tools&lt;/strong&gt; &amp;gt; &lt;strong&gt;Android&lt;/strong&gt; &amp;gt; &lt;strong&gt;AVD Manager&lt;/strong&gt;. Create a new Virtual Device and click the play button.&lt;/p&gt;

&lt;p&gt;This will build and install the skeleton app on either a connected Android device or an emulator if it is connected. Similarly for &lt;code&gt;react-native run-ios&lt;/code&gt;. If you get stuck here, check out the getting started guide I mentioned above (specifically the &lt;strong&gt;Building Projects with Native Code&lt;/strong&gt; tab).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets-jekyll%2Fblog%2Freact-native-testing%2Fwelcome-to-react-native-8f28b08a07e085a007eab5cf7b96333be1858b2feb2e2ce0ce63c434fd35f233.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets-jekyll%2Fblog%2Freact-native-testing%2Fwelcome-to-react-native-8f28b08a07e085a007eab5cf7b96333be1858b2feb2e2ce0ce63c434fd35f233.png" alt="Welcome to React Native!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a Native Application in Okta
&lt;/h2&gt;

&lt;p&gt;To make adding authentication and authorization simple, we’ll use Okta in this tutorial. You can sign-up for a &lt;a href="https://developer.okta.com/signup/" rel="noopener noreferrer"&gt;free Okta developer account here&lt;/a&gt;. When that’s done, log in and navigate to &lt;strong&gt;Applications&lt;/strong&gt; &amp;gt; &lt;strong&gt;Add Application&lt;/strong&gt;. Select &lt;strong&gt;Native&lt;/strong&gt; and click &lt;strong&gt;Next&lt;/strong&gt;. Choose a name, select &lt;strong&gt;Refresh Token&lt;/strong&gt; and click &lt;strong&gt;Done&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Note your &lt;strong&gt;Login redirect URI&lt;/strong&gt; and the &lt;strong&gt;Client ID&lt;/strong&gt; since you’ll be adding them to your app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add AppAuth to Your Project
&lt;/h2&gt;

&lt;p&gt;For authentication we’re going to use the &lt;a href="https://github.com/FormidableLabs/react-native-app-auth" rel="noopener noreferrer"&gt;react-native-app-auth&lt;/a&gt; library, using their example project as a basis. First clone the repository to a local directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/FormidableLabs/react-native-app-auth

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then inside of your project folder (&lt;code&gt;ReactNativeTypeScript&lt;/code&gt;) install the required extra libraries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;react-native-app-auth@4.0.0
react-native &lt;span class="nb"&gt;link &lt;/span&gt;react-native-app-auth

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last command links native code into your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;styled-components@4.1.1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This library is used in the example code. Now modify your &lt;code&gt;App.js&lt;/code&gt; to what is inside of the example code at &lt;code&gt;Example/AndroidExample/App.js&lt;/code&gt;, and copy the &lt;code&gt;assets&lt;/code&gt; and &lt;code&gt;component&lt;/code&gt; folders from the example into your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cp&lt;/span&gt; ../react-native-app-auth/Example/AndroidExample/App.js &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nb"&gt;cp&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; ../react-native-app-auth/Example/AndroidExample/assets &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nb"&gt;cp&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; ../react-native-app-auth/Example/AndroidExample/components &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add the following to the &lt;code&gt;defaultConfig&lt;/code&gt; section of the &lt;code&gt;android&lt;/code&gt; config in &lt;code&gt;android/app/build.gradle&lt;/code&gt;, using the base of your &lt;strong&gt;Redirect URL&lt;/strong&gt; , e.g. &lt;code&gt;com.oktapreview.dev-628819&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;manifestPlaceholders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
      &lt;span class="nl"&gt;appAuthRedirectScheme:&lt;/span&gt; &lt;span class="s1"&gt;'{redirectBase}'&lt;/span&gt;
&lt;span class="o"&gt;]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, modify the &lt;code&gt;config&lt;/code&gt; section of your &lt;code&gt;App.js&lt;/code&gt; to include your Okta app information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;issuer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://{yourOktaDomain}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;oauth2&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{clientId}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;redirectUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{redirectUrl}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;additionalParameters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;scopes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;openid&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;offline_access&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see &lt;code&gt;{yourOktaDomain}&lt;/code&gt; as the issuer above, you can find this value on the Okta dashboard. If you don’t see this variable, it’s because this site is smart enough to fill it in for you. The values for &lt;strong&gt;clientId&lt;/strong&gt; and &lt;strong&gt;redirectUrl&lt;/strong&gt; you can get from your Okta app.&lt;/p&gt;

&lt;p&gt;After running &lt;code&gt;react-native run-android&lt;/code&gt; you should get the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets-jekyll%2Fblog%2Freact-native-testing%2Fhello-stranger-e82d2b6090b052598f0028d56b83c967a526b50333a58632c8ed3240d1edd5df.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets-jekyll%2Fblog%2Freact-native-testing%2Fhello-stranger-e82d2b6090b052598f0028d56b83c967a526b50333a58632c8ed3240d1edd5df.png" alt="Hello, stranger"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: I got several errors about &lt;code&gt;unable to delete&lt;/code&gt; on the console which I fixed by cleaning the android project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;android
gradlew clean

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you click on &lt;code&gt;Authorize&lt;/code&gt; you should see a page detailing about the tokens being used for authentication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets-jekyll%2Fblog%2Freact-native-testing%2Fauthorize-page-4cc3046cd98a49a21b28035f33482bb82cb1a4f4b76ba87a18fab29340f39bb6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets-jekyll%2Fblog%2Freact-native-testing%2Fauthorize-page-4cc3046cd98a49a21b28035f33482bb82cb1a4f4b76ba87a18fab29340f39bb6.png" alt="Screen with access token and refresh token"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TIP:&lt;/strong&gt; You can reload your app’s code in the Android Emulator using &lt;strong&gt;CTRL + M&lt;/strong&gt; ( &lt;strong&gt;Command + M&lt;/strong&gt; on Mac)..&lt;/p&gt;

&lt;h2&gt;
  
  
  Port Your React Native App to TypeScript
&lt;/h2&gt;

&lt;p&gt;One common way of using TypeScript in React Native is by using a tool like &lt;a href="https://github.com/ds300/react-native-typescript-transformer" rel="noopener noreferrer"&gt;react-native-typescript-transformer&lt;/a&gt;But in September of this year, Facebook released React Native 0.57 which includes Babel 7 that has TypeScript support, so you can use it out of the box. All you have to do is rename your &lt;code&gt;App.js&lt;/code&gt; to &lt;code&gt;App.tsx&lt;/code&gt; and React Native will pick up the changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mv &lt;/span&gt;App.js App.tsx

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You also need to modify &lt;code&gt;App.tsx&lt;/code&gt; and change the &lt;code&gt;State&lt;/code&gt; variable to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;State&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;hasLoggedInOnce&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;accessTokenExpirationDate&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;refreshToken&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The question marks were moved back to before the colon. This is how TypeScript identifies optional parameters (as opposed to Flow, which is what the &lt;code&gt;react-native init&lt;/code&gt; command uses to set up a skeleton project).&lt;/p&gt;

&lt;p&gt;Running the project again should start and operate as before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;react-native run-android

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you get an error like &lt;code&gt;Module not registered in graph&lt;/code&gt;, close down the packager window before re-running &lt;code&gt;run-android&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test your React Native Application
&lt;/h2&gt;

&lt;p&gt;The standard way of testing React Native is &lt;a href="https://jestjs.io/docs/en/tutorial-react-native" rel="noopener noreferrer"&gt;using Jest&lt;/a&gt; and the &lt;a href="https://reactjs.org/docs/test-renderer.html" rel="noopener noreferrer"&gt;React Test Renderer&lt;/a&gt;, both of which come included in projects created with &lt;code&gt;react-native init&lt;/code&gt;. However, you need to set things up to work with TypeScript.&lt;/p&gt;

&lt;p&gt;First, move the contents of &lt;code&gt;.babelrc&lt;/code&gt; into &lt;code&gt;babel.config.js&lt;/code&gt; and then delete &lt;code&gt;.babelrc&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;presets&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;module:metro-react-native-babel-preset&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, put the following into &lt;code&gt;jest.config.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;defaults&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;tsjPreset&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ts-jest/presets&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;tsjPreset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;preset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;tsjPreset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s1"&gt;.js$&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;rootDir&amp;gt;/node_modules/react-native/jest/preprocessor.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ts-jest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;babelConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// This is the only part which you can keep&lt;/span&gt;
  &lt;span class="c1"&gt;// from the above linked tutorial's config:&lt;/span&gt;
  &lt;span class="na"&gt;cacheDirectory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.jest/cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a folder called &lt;code&gt;__tests__&lt;/code&gt; and inside create a file called &lt;code&gt;Component-tests.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Page&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../components/Page&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Form&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../components/Form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Heading&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../components/Heading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;renderer&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-test-renderer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Page renders correctly&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;renderer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Page&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toJSON&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toMatchSnapshot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Form renders correctly&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;renderer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Form&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toJSON&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toMatchSnapshot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Heading renders correctly&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;renderer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Heading&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toJSON&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toMatchSnapshot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you are pulling in various components and checking that they render correctly. Now install &lt;code&gt;ts-jest&lt;/code&gt; and &lt;code&gt;typescript&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; ts-jest typescript

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a folder called &lt;code&gt;src&lt;/code&gt; and move &lt;code&gt;App.tsx&lt;/code&gt; into it, modifying the components import so that it finds it correctly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ButtonContainer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Form&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Heading&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../components&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, change &lt;code&gt;index.js&lt;/code&gt; in the root folder to import &lt;code&gt;./src/App&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** @format */&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;AppRegistry&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;appName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;AppRegistry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;registerComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;appName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now running &lt;code&gt;npm test&lt;/code&gt; should check that the &lt;code&gt;Page&lt;/code&gt;, &lt;code&gt;Form&lt;/code&gt; and &lt;code&gt;Heading&lt;/code&gt; components render correctly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets-jekyll%2Fblog%2Freact-native-testing%2Fconsole-test-12cc85214481c0267f0f1a752c8207181e4d175c98090450a1b6460e2a0022cb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets-jekyll%2Fblog%2Freact-native-testing%2Fconsole-test-12cc85214481c0267f0f1a752c8207181e4d175c98090450a1b6460e2a0022cb.png" alt="Output from running npm test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In these tests, you are just checking whether certain components render correctly. Ideally, you’d want to check that authentication works using something like the &lt;a href="https://github.com/wix/Detox" rel="noopener noreferrer"&gt;Detox end-to-end testing framework&lt;/a&gt;. However, there is currently &lt;a href="https://github.com/wix/detox/issues/608" rel="noopener noreferrer"&gt;an issue with the latest Android&lt;/a&gt;. Hopefully, this will be fixed soon.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Native + TypeScript = Win!
&lt;/h2&gt;

&lt;p&gt;Congrats! You’ve created a React Native app with TypeScript from scratch, with authentication and authorization (through Okta), and automated tests!&lt;/p&gt;

&lt;p&gt;You can find the source code for this tutorial at &lt;a href="https://github.com/oktadeveloper/okta-react-native-typescript-example" rel="noopener noreferrer"&gt;https://github.com/oktadeveloper/okta-react-native-typescript-example&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you’re interested to know more about TypeScript, React Native or secure user management with Okta, check out the following resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/blog/2018/11/15/node-express-typescript"&gt;Use TypeScript to Build a Node API with Express&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/blog/2017/09/19/build-a-secure-notes-application-with-kotlin-typescript-and-okta"&gt;Build a Secure Notes App with Kotlin, Typescript, and Okta&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/blog/2018/03/16/build-react-native-authentication-oauth-2"&gt;Build a React Native App and Authentication with OAuth 2.0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like what you learned today? Follow us &lt;a href="https://twitter.com/oktadev" rel="noopener noreferrer"&gt;on Twitter&lt;/a&gt;, like us &lt;a href="https://www.facebook.com/oktadevelopers/" rel="noopener noreferrer"&gt;on Facebook&lt;/a&gt;, check us out &lt;a href="https://www.linkedin.com/company/oktadev/" rel="noopener noreferrer"&gt;on LinkedIn&lt;/a&gt;, and &lt;a href="https://www.youtube.com/channel/UC5AMiWqFVFxF1q9Ya1FuZ_Q" rel="noopener noreferrer"&gt;subscribe to our YouTube channel&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>reactnative</category>
      <category>oauth</category>
      <category>android</category>
    </item>
    <item>
      <title>Build a Basic CRUD App in Android with Kotlin</title>
      <dc:creator>Karl</dc:creator>
      <pubDate>Tue, 11 Sep 2018 05:00:00 +0000</pubDate>
      <link>https://dev.to/oktadev/build-a-basic-crud-app-in-android-with-kotlin-3nlj</link>
      <guid>https://dev.to/oktadev/build-a-basic-crud-app-in-android-with-kotlin-3nlj</guid>
      <description>&lt;p&gt;Kotlin was recently given official Android support status by Google, but it remains difficult to understand for many developers. The best way to start is by creating a complete app yourself, which you’ll do in this tutorial. In this tutorial, you’ll use Spring Boot for the API that powers your Android (+ Kotlin) mobile app. Spring Boot is a great way to create a robust REST API with a minimal amount of code.&lt;/p&gt;

&lt;p&gt;I’m going to assume you have some Java experience and have at least played around with creating an Android app. If you don’t have any Android experience you should be able to follow along but you might have to Google a few things here and there.&lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://github.com/oktadeveloper/okta-android-kotlin-crud-example" rel="noopener noreferrer"&gt;complete code&lt;/a&gt; if you rather go straight to the end.&lt;/p&gt;

&lt;p&gt;Before we start, let’s talk a bit about Kotlin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Kotlin vs Java
&lt;/h2&gt;

&lt;p&gt;Kotlin looks strange to newcomers. It resembles other languages you may have seen but some things look off, often because it is so concise!&lt;/p&gt;

&lt;p&gt;Don’t panic - because it is so extensible there are many ways to write the same code, and many shortcuts that aren’t available in other languages. For example, often you’ll see curly brackets used as function parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dialogBuilder.setPositiveButton("Delete", { dialog, whichButton -&amp;gt;
    deleteMovie(movie)
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is actually creating an anonymous function (a &lt;em&gt;lambda&lt;/em&gt;) and passing it in. This function takes in two parameters which are here inferred. Take a look at the equivalent (pre-JRE 8) Java code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dialogBuilder.setPositiveButton("Delete",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            deleteMovie(movie);
        }
    }
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Of course, now Java 8 has &lt;a href="https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html" rel="noopener noreferrer"&gt;lambdas too&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Here is another example of some code we will use in a bit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MovieViewHolder(val view: View) : RecyclerView.ViewHolder(view)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to understand this you have to know several things:&lt;/p&gt;

&lt;p&gt;Declaring a class with parentheses (i.e. &lt;code&gt;(view: View)&lt;/code&gt;) means you are declaring the class’s &lt;em&gt;primary constructor&lt;/em&gt; (and yes - there are &lt;a href="https://kotlinlang.org/docs/reference/classes.html" rel="noopener noreferrer"&gt;secondary constructors&lt;/a&gt; as well).The colon &lt;code&gt;:&lt;/code&gt; is similar to &lt;code&gt;implements&lt;/code&gt; or &lt;code&gt;extends&lt;/code&gt; but really is about &lt;a href="https://kotlinlang.org/docs/reference/interfaces.html" rel="noopener noreferrer"&gt;Interfaces&lt;/a&gt;.Anything declared in the primary constructor is automatically declared as a property (member variable).&lt;/p&gt;

&lt;p&gt;For clarity, this is the equivalent Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static class MovieViewHolder extends RecyclerView.ViewHolder {
    public final View view;
    public MovieViewHolder(View v) {
        super(v);
        view = v;
   }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a last example, look at the following bean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package demo

data class Movie( val id: Int, val name: String )

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the complete file. It declares a class with a constructor, two read-only properties (member variables), and assigns those in the constructor. Then &lt;code&gt;data&lt;/code&gt; creates getters and setters for all our member variables, as well as &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;toString()&lt;/code&gt; &lt;a href="https://kotlinlang.org/docs/reference/data-classes.html" rel="noopener noreferrer"&gt;and others&lt;/a&gt; (&lt;a href="https://gist.github.com/kewp/1efc1a4c406577342c43ccb258bf8739" rel="noopener noreferrer"&gt;see here&lt;/a&gt; if you want to see it in it’s full Java glory).&lt;/p&gt;

&lt;p&gt;Now that you’ve got some background, let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Create the Spring Boot API for Your Android + Kotlin Project
&lt;/h2&gt;

&lt;p&gt;The official Spring Boot tutorials suggest you use the &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;Initializr Website&lt;/a&gt; to create a starting skeleton but I find it easier to build projects from scratch.&lt;/p&gt;

&lt;p&gt;To start, initialize an empty directory with Gradle (make sure you’ve &lt;a href="https://gradle.org/install/" rel="noopener noreferrer"&gt;installed Gradle&lt;/a&gt; and that it’s available on the command line).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\Karl\Kotlin-Spring&amp;gt;gradle init

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
C:\Users\Karl\Kotlin-Spring&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should have two folders and four files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle

2 directories, 6 files

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now change &lt;code&gt;build.gradle&lt;/code&gt; to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;buildscript {
    ext.kotlin_version = '1.2.61' // Required for Kotlin integration
    ext.spring_boot_version = '2.0.2.RELEASE'
    repositories {
        jcenter()
    }
    dependencies {
        // Required for Kotlin integration
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin         
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version")
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
    }
}

// Required for Kotlin integration
apply plugin: 'kotlin'
// See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
apply plugin: "kotlin-spring" 
apply plugin: 'kotlin-jpa'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

jar {
    baseName = 'kotlin-demo'
    version = '0.1.0-SNAPSHOT'
}

repositories {
    jcenter()
}

dependencies {
    // Required for Kotlin integration
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect" // For reflection
    compile 'org.springframework.boot:spring-boot-starter-data-rest'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'com.h2database:h2'
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the Kotlin and Spring Boot plugins are imported, external repositories are declared, and dependency libraries are added.&lt;/p&gt;

&lt;p&gt;If you haven’t used Spring Boot before you should know that it (or rather the Spring Framework) uses dependency injection at runtime. This means the entire application is wired up automatically based on the libraries you import. For example, at the end of our &lt;code&gt;build.gradle&lt;/code&gt; you’ll see the Data REST and Data JPA libraries. Spring Boot will automatically configure your application as a REST server when it sees two these libraries. Furthermore, since you included the &lt;code&gt;H2&lt;/code&gt; database library Spring will use the &lt;a href="http://www.h2database.com/html/main.html" rel="noopener noreferrer"&gt;H2 database engine&lt;/a&gt; to persist any of our REST data coming in and out of queries.&lt;/p&gt;

&lt;p&gt;All you need to have a complete REST application, is to define a class with the &lt;code&gt;@SpringBootApplication&lt;/code&gt; annotation. You don’t even need to specify it’s path - Spring will search for it!&lt;/p&gt;

&lt;p&gt;Put the following into &lt;code&gt;src/main/kotlin/demo/Application.kt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package demo

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class Application

fun main(args: Array&amp;lt;String&amp;gt;) {
    SpringApplication.run(Application::class.java, *args)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you run &lt;code&gt;gradlew bootRun&lt;/code&gt; (&lt;code&gt;./gradlew bootRun&lt;/code&gt; on *nix) everything should build (and download) and you should see somewhere in the enormous log &lt;strong&gt;Started Application&lt;/strong&gt;. Now run &lt;code&gt;curl&lt;/code&gt; in another window to see what is happening.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\Karl&amp;gt;curl localhost:8080
{
  "_links" : {
    "profile" : {
      "href" : "http://localhost:8080/profile"
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amazingly, you’ve created a fully compliant REST server with Kotlin, all by editing just two files!&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Objects with Kotlin
&lt;/h2&gt;

&lt;p&gt;To create objects you just need the entity class and a repository.&lt;/p&gt;

&lt;p&gt;Next to &lt;code&gt;Application.kt&lt;/code&gt; put the following into &lt;code&gt;Model.kt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package demo

import javax.persistence.*

@Entity
data class Movie(@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
                 val Id: Long,
                 val name: String)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you’ve used the &lt;a href="https://kotlinlang.org/docs/reference/idioms.html#creating-dtos-pojospocos" rel="noopener noreferrer"&gt;data idiom&lt;/a&gt; to create getters and setters for all the properties, as well as &lt;a href="https://spring.io/guides/gs/accessing-data-jpa/" rel="noopener noreferrer"&gt;JPA annotations&lt;/a&gt; to specify how to generate the ids for your entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;Id&lt;/code&gt; field must start with a capital &lt;strong&gt;I&lt;/strong&gt;. If it doesn’t, the server won’t return the id field when doing queries. This will give you trouble down the line when hooking up to the client app.&lt;/p&gt;

&lt;p&gt;Now put this into &lt;code&gt;Repository.kt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package demo

import org.springframework.data.repository.CrudRepository

interface ItemRepository : CrudRepository&amp;lt;Movie, Long&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you’ve done it! Incredibly, we can now perform any CRUD operation on this server and it will work, persisting all changes to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\Karl&amp;gt;curl -X POST -H "Content-Type:application/json" -d " {\"name\":\"The 40 Year Old Virgin\"} " localhost:8080/movies
{
  "name" : "The 40 Year Old Virgin",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/movies/1"
    },
    "item" : {
      "href" : "http://localhost:8080/movies/1"
    }
  }
}


C:\Users\Karl&amp;gt;curl localhost:8080/movies/1
{
  "name" : "The 40 Year Old Virgin",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/movies/1"
    },
    "item" : {
      "href" : "http://localhost:8080/movies/1"
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Load Initial Data in your Kotlin App
&lt;/h2&gt;

&lt;p&gt;To finish up, let’s load some data. Again, as with Spring Boot everything can be done simply. Just put the following into &lt;code&gt;src/main/resources/data.sql&lt;/code&gt; and it will be run on boot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO movie (name) VALUES
  ('Skyfall'),
  ('Casino Royale'),
  ('Spectre');

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To confirm it works, restart the server and run &lt;code&gt;curl localhost:8080/movies&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And you are done with the back-end. Time to build out the client.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build Your Android App with Kotlin
&lt;/h2&gt;

&lt;p&gt;This will require a couple of steps: First you’ll create an empty Kotlin app with &lt;a href="https://developer.android.com/studio/" rel="noopener noreferrer"&gt;Android Studio&lt;/a&gt;. You’ll then create a list view (with add, edit and delete buttons) using &lt;a href="https://developer.android.com/guide/topics/ui/layout/recyclerview" rel="noopener noreferrer"&gt;RecyclerView&lt;/a&gt;, populating it with hard-coded data. Finally, you’ll use &lt;a href="https://square.github.io/retrofit/" rel="noopener noreferrer"&gt;Retrofit&lt;/a&gt; to wire the view to the REST back-end you’ve just created.&lt;/p&gt;

&lt;p&gt;Create a project in Android Studio. Make sure you’re using at least version Android Studio 3. Use the default values for each window except make sure you &lt;strong&gt;include Kotlin support&lt;/strong&gt;. Name the project whatever you want - I called mine “Kotlin Crud”. At the end, select an &lt;strong&gt;Empty Activity&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you press Play on the top icon bar you should see Hello World when you run it (you can either plug in your phone or run it &lt;a href="https://developer.android.com/studio/run/managing-avds" rel="noopener noreferrer"&gt;on an emulator&lt;/a&gt;. Check online for how to set this up).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fhello-world-android-72e1560cd6e6b760073155ca8463ed808dc957a5ee03966ebb39a2f0f6283bf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fhello-world-android-72e1560cd6e6b760073155ca8463ed808dc957a5ee03966ebb39a2f0f6283bf6.png" alt="Hello World in Android"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’ve made an Android app before using Java you’ll notice the only difference is the main activity: it’s called &lt;strong&gt;MainActivity.kt&lt;/strong&gt; , not &lt;code&gt;MainActivity.java&lt;/code&gt;, and the code looks a bit different.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package demo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)
   }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are the differences:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The class is not specified as &lt;code&gt;public&lt;/code&gt; (in Kotlin this is the default)&lt;/li&gt;
&lt;li&gt;Types are specified with a colon &lt;code&gt;:&lt;/code&gt; - the class is of type &lt;code&gt;AppCompatActivity&lt;/code&gt; (or rather it &lt;em&gt;implements&lt;/em&gt; &lt;code&gt;AppCompatActivity&lt;/code&gt;, as you would say in Java) and the &lt;code&gt;savedInstanceState&lt;/code&gt; is of type &lt;code&gt;Bundle&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Methods are just called &lt;code&gt;fun&lt;/code&gt; instead of &lt;em&gt;function&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;override&lt;/code&gt; is not an annotation&lt;/li&gt;
&lt;li&gt;The question mark means a parameter is &lt;em&gt;optional&lt;/em&gt; (which is not possible in Java)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last point is one of the most talked about when discussing the importance of Kotlin vs Java: it’s one of the various ways the language ensures &lt;a href="https://kotlinlang.org/docs/reference/null-safety.html" rel="noopener noreferrer"&gt;null safety&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Import Additional Android Libraries
&lt;/h2&gt;

&lt;p&gt;You need to add extra libraries to your application’s &lt;code&gt;build.gradle&lt;/code&gt; file: one for the recycler view (which you’ll use in a second), one for the card view, and another for the floating action button. Place these next to the others in the &lt;code&gt;dependencies&lt;/code&gt; section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Android Studio should ask you to &lt;em&gt;Sync Now&lt;/em&gt;. Click that and see that everything builds without error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Make sure the version is the same as the other support libraries (e.g. appcompat-v7:27.1.1). Also, because you’ll be using built-in icons (which you should avoid doing in the future) you need to put the following into the &lt;code&gt;defaultConfig&lt;/code&gt; section of your &lt;code&gt;build.gradle&lt;/code&gt; as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vectorDrawables.useSupportLibrary = true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add Icons in Kotlin
&lt;/h2&gt;

&lt;p&gt;You’ll need some icons for buttons - one for &lt;em&gt;add&lt;/em&gt; and another for &lt;em&gt;refresh&lt;/em&gt;. Go to the &lt;a href="https://material.io/tools/icons/" rel="noopener noreferrer"&gt;Material Icons&lt;/a&gt; site and select the one you like. I’m choosing the &lt;a href="https://material.io/tools/icons/?icon=add&amp;amp;style=baseline" rel="noopener noreferrer"&gt;add button&lt;/a&gt; half the way down. When you click on it a grey and blue download section should appear on the button left. Click the grey box &lt;strong&gt;Selected Icons&lt;/strong&gt; control to open the download options. Now there should be a drop-down where you can select Android as the type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fdownload-icons-31857d19b0c2ffab00b0b54e8978322506fbfddd2b005731125919f5084b3796.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fdownload-icons-31857d19b0c2ffab00b0b54e8978322506fbfddd2b005731125919f5084b3796.png" alt="Change drop down to Android"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Change the color to white and download the PNG option. Extract the contents of the ZIP file to &lt;code&gt;app/src/main&lt;/code&gt; (you should see the ZIP file has a &lt;code&gt;res&lt;/code&gt; folder in it).&lt;/p&gt;

&lt;p&gt;Now you can use the new icons in your layouts. They’re called things like &lt;code&gt;baseline_add_white_36&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally do the same thing for the &lt;em&gt;loop&lt;/em&gt; icon, also white.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create the View XML
&lt;/h2&gt;

&lt;p&gt;You need an XML view for each list item. Place the following into &lt;code&gt;src/main/res/layout/list_item.xml&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"

    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:padding="3dp"
    card_view:cardElevation="2dp"
    card_view:cardMaxElevation="2dp"&amp;gt;

    &amp;lt;RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"&amp;gt;

        &amp;lt;TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="lastname"
            android:textSize="16dp" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:drawableLeft="@android:drawable/ic_delete"
            android:padding="5dp" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/btnEdit"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_marginRight="2dp"
            android:layout_toLeftOf="@+id/btnDelete"
            android:drawableLeft="@android:drawable/ic_menu_edit"
            android:padding="5dp" /&amp;gt;
    &amp;lt;/RelativeLayout&amp;gt;

&amp;lt;/android.support.v7.widget.CardView&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you’re using a &lt;a href="https://developer.android.com/guide/topics/ui/layout/cardview" rel="noopener noreferrer"&gt;Card View&lt;/a&gt; which is the popular way of creating lists in Android. Almost all of the XML is layout settings to ensure proper alignment. Note the &lt;code&gt;android:id&lt;/code&gt; values which you use to connect these to our Kotlin files. Also, I’ve used some built in Android icons for our edit and delete buttons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; this is not the recommended way of doing it since those icons can change between Android Studio versions - rather download the icons as we did previously !&lt;/p&gt;

&lt;p&gt;Now for the main activity XML. Here is what &lt;code&gt;src/main/res/layout/activity_main.xml&lt;/code&gt; should look like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"&amp;gt;

    &amp;lt;android.support.v7.widget.RecyclerView
        android:id="@+id/rv_item_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" /&amp;gt;

    &amp;lt;android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/baseline_add_white_36"
        android:layout_gravity="bottom|end"
        app:elevation="6dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_margin="20dp"/&amp;gt;

&amp;lt;/RelativeLayout&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s pretty straight forward. You’ve now got a recycle view and a floating action button inside of a relative layout and have assigned &lt;code&gt;baseline_add_white_36&lt;/code&gt; as the source for the button. Note that the id of the recycler view is &lt;code&gt;rv_list_item&lt;/code&gt; (you’ll be using this soon).&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Refresh to the Action Bar
&lt;/h2&gt;

&lt;p&gt;To fill things out lets put a refresh button on the action bar. This requires a new piece of XML in &lt;code&gt;res/menu/buttons.xml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"&amp;gt;

    &amp;lt;item
        android:id="@+id/refresh"
        android:icon="@drawable/baseline_loop_white_48"
        android:title="@string/refresh"
        app:showAsAction="ifRoom"/&amp;gt;

&amp;lt;/menu&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note it has an id called &lt;strong&gt;refresh&lt;/strong&gt;. Also, I’ve used the &lt;em&gt;loop&lt;/em&gt; icon from the Android Icons site (the white variant) - you’ll have to download this as before. Also, I’m using a string from the resources so you’ll have to change &lt;code&gt;res/values/strings.xml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;resources&amp;gt;
    &amp;lt;string name="app_name"&amp;gt;Kotlin Crud&amp;lt;/string&amp;gt;
    &amp;lt;string name="refresh"&amp;gt;Refresh&amp;lt;/string&amp;gt;
&amp;lt;/resources&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Display Lists in Kotlin
&lt;/h2&gt;

&lt;p&gt;Now to display a list of item using our views. The canonical way of doing this is the relatively new &lt;code&gt;RecyclerView&lt;/code&gt; which supplanted the original &lt;code&gt;ListView&lt;/code&gt;. The basic idea of a &lt;code&gt;RecyclerView&lt;/code&gt; is to create only enough views to show on screen - if the screen can fit five items then only five are created. As you scroll through the list these views are re-used (recycled), replacing their contents with the appropriate (new) values.&lt;/p&gt;

&lt;p&gt;How do you get started with this ? The first thing you need is a bean. Let’s call it &lt;code&gt;Movie.kt&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package demo

data class Movie( val id: Int, val name: String )

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; for all the following classes make sure the &lt;code&gt;package&lt;/code&gt; matches that of &lt;code&gt;MainActivity.kt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Was that not easy? Next, you need an &lt;code&gt;Adapter&lt;/code&gt;. This is a class with three methods: one to return how many items in total are being displayed (&lt;code&gt;getItemCount()&lt;/code&gt;), one that creates an Android View control for a particular item (&lt;code&gt;onCreateViewHolder()&lt;/code&gt;), and one that populates an existing view with an instance of your data (&lt;code&gt;onBindViewHolder()&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Put this into &lt;code&gt;MovieAdapter.kt&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MovieAdapter : RecyclerView.Adapter&amp;lt;MovieAdapter.MovieViewHolder&amp;gt;() {

    var movies: ArrayList&amp;lt;Movie&amp;gt; = ArrayList()

    init { refreshMovies() }

    class MovieViewHolder(val view: View) : RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup,
                                    viewType: Int): MovieAdapter.MovieViewHolder {

        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.list_item, parent, false)

        return MovieViewHolder(view)
    }

    override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
        holder.view.name.text = movies[position].name
    }

    override fun getItemCount() = movies.size

    fun refreshMovies() {
        movies.clear()

        movies.add(Movie(0, "Guardians of the Galaxy"))
        movies.add(Movie(1, "Avengers: Infinity War"))
        movies.add(Movie(2,"Thor: Ragnorok"))

        notifyDataSetChanged()
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you paste this into Android Studio it will highlight certain things as red. You need to ALT-ENTER (Option + Enter on Mac) several times to pull in the imports you need. Eventually this is the list of imports you should have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.list_item.view.*

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A lot is going on in &lt;code&gt;MovieAdapter.kt&lt;/code&gt;. Besides just implementing the three methods needed to implement &lt;code&gt;RecylcerView.Adapter&lt;/code&gt;, you created a property called &lt;code&gt;movies&lt;/code&gt;, a list, and initialise it in the &lt;code&gt;init{}&lt;/code&gt; constructor. Also, you declared an inner class called &lt;code&gt;MovieViewHolder&lt;/code&gt;. That is what gets instantiated for each view needed to be displayed (in the example discussed, five views). As you can see, &lt;code&gt;onCreateViewHolder&lt;/code&gt; actually returns an object of this type. The class is quite simple - it takes into its constructor a &lt;code&gt;View&lt;/code&gt; (which is now also a property), and returns a &lt;code&gt;Holder&lt;/code&gt; type object. This object is what you then use when you fill in data using &lt;code&gt;onBindViewHolder&lt;/code&gt; - in our case, setting the text of our display.&lt;/p&gt;

&lt;p&gt;This does seem complicated at first. It good way to look at all this is the following: how does this connect to your main code class (i.e. &lt;code&gt;MainActivity.kt&lt;/code&gt;), and how does it connect to the views you’ve defined in XML ?&lt;/p&gt;

&lt;p&gt;For the first part, this is what main activity should now look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MainActivity : AppCompatActivity() {

    lateinit var adapter:MovieAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        adapter = MovieAdapter()    

        rv_item_list.layoutManager = LinearLayoutManager(this)
        rv_item_list.adapter = adapter
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here you’ve defined the &lt;code&gt;adapter&lt;/code&gt; as a &lt;strong&gt;lateinit&lt;/strong&gt; variable - &lt;code&gt;lateinit&lt;/code&gt; is telling Kotlin that you want to initialize this at some stage after creation, not the default in Kotlin classes - normally you have to initialize things immediately.&lt;/p&gt;

&lt;p&gt;In the constructor you assign an instance of your adapter to this property (note you don’t need to use &lt;code&gt;new&lt;/code&gt; in Kotlin) and assign two things to &lt;code&gt;rv_item_list&lt;/code&gt; - a &lt;a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.LayoutManager" rel="noopener noreferrer"&gt;LayoutManager&lt;/a&gt; (which is used for positioning), and an Adapter (which we’ve just created).&lt;/p&gt;

&lt;p&gt;We should talk about &lt;code&gt;rv_item_list&lt;/code&gt;. This is just the id of a control inside of &lt;code&gt;activity_main.xml&lt;/code&gt;, specifically the recyclerview. Normally you would need to use &lt;code&gt;findViewById&lt;/code&gt; (a pain for Android developers) but with Kotlin you can just specify its name. When Android Studio complains about imports and you ALT-ENTER (or your platform equivalent) it will automatically import &lt;code&gt;kotlinx.android.synthetic.main.activity_main.*&lt;/code&gt;, bringing in all the ids into the namespace.&lt;/p&gt;

&lt;p&gt;Lastly, add the following two functions to &lt;code&gt;MainActivity&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override fun onCreateOptionsMenu(menu: Menu): Boolean {
    val inflater = menuInflater
    inflater.inflate(R.menu.buttons, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
    R.id.refresh -&amp;gt; {
        adapter.refreshMovies()
        Toast.makeText(this.baseContext, "Refreshed", Toast.LENGTH_LONG).show())
        true
    }
    else -&amp;gt; {
        super.onOptionsItemSelected(item)
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That will inflate the menu xml you defined as well as tying the button to your adapter’s refresh function (and give a convenience &lt;a href="https://developer.android.com/guide/topics/ui/notifiers/toasts" rel="noopener noreferrer"&gt;toast&lt;/a&gt; to say it worked).&lt;/p&gt;

&lt;p&gt;That should be it ! Run our code and you should see the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fmovie-list-android-56b754978fce492ea4882557e03fa8bf4402ed2036ea80d056b60e6c0e0b339c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fmovie-list-android-56b754978fce492ea4882557e03fa8bf4402ed2036ea80d056b60e6c0e0b339c.png" alt="Completed Android app with movie list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wiring Your Android + Kotlin App Up
&lt;/h2&gt;

&lt;p&gt;Next you need to replace the hard-coded values with data coming from your API server, as well as wiring the different buttons to their respective API calls. For that you will be using Square’s Retrofit library.&lt;/p&gt;

&lt;p&gt;Start by adding the following to your &lt;code&gt;build.gradle&lt;/code&gt; dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now take a look at what happens when you call your server for a list of movies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\Karl&amp;gt;curl http://localhost:8080/movies
{
  "_embedded" : {
    "movies" : [ {
      "name" : "Skyfall",
      "id" : 1,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/movies/1"
        },
        "movie" : {
          "href" : "http://localhost:8080/movies/1"
        }
      }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I’ve only shown one since it’s quite long (Spring follows something called HATEOAS which adds links to json responses). As you can see, the response is wrapped in an &lt;code&gt;_embedded&lt;/code&gt; object, and your movies come as a list in &lt;code&gt;movies&lt;/code&gt;. You need to represent this in your Kotlin model so Retrofit can know what to expect. Change &lt;code&gt;Movie.kt&lt;/code&gt; to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import com.google.gson.annotations.SerializedName

data class Movie( val id: Int, val name: String )
data class MovieList (
    @SerializedName("movies" )
    val movies: List&amp;lt;Movie&amp;gt;
)
data class MovieEmbedded (
    @SerializedName("_embedded" )
    val list: MovieList
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you need to create a new class to setup Retrofit. Let’s call it &lt;code&gt;MovieApiClient.kt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import io.reactivex.Completable
import io.reactivex.Observable
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.*

interface MovieApiClient {

    @GET("movies") fun getMovies(): Observable&amp;lt;MovieEmbedded&amp;gt;
    @POST("movies") fun addMovie(@Body movie: Movie): Completable
    @DELETE("movies/{id}") fun deleteMovie(@Path("id") id: Int) : Completable
    @PUT("movies/{id}") fun updateMovie(@Path("id")id: Int, @Body movie: Movie) : Completable

    companion object {

        fun create(): MovieApiClient {

            val retrofit = Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl("http://10.0.2.2:8080/")
                    .build()

            return retrofit.create(MovieApiClient::class.java)
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you define all the endpoints using annotations as well as their expected return types (&lt;code&gt;Completable&lt;/code&gt;, part of &lt;a href="https://github.com/ReactiveX/RxJava" rel="noopener noreferrer"&gt;RxJava&lt;/a&gt;, just means nothing is returned). You also declare a &lt;a href="https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects" rel="noopener noreferrer"&gt;companion object&lt;/a&gt; (which is like a static class) which instantiates a Retrofit builder with the details of our API. Note the base url uses the IP &lt;code&gt;10.0.2.2&lt;/code&gt; which allows emulators to connect to localhost.&lt;/p&gt;

&lt;p&gt;Now in &lt;code&gt;MovieAdapter&lt;/code&gt; change the header to include a &lt;code&gt;context&lt;/code&gt; property (so you can attach toasts for our API results) as well as adding a &lt;a href="https://kotlinlang.org/docs/reference/delegated-properties.html#lazy" rel="noopener noreferrer"&gt;lazy&lt;/a&gt; client property initialized with your previous &lt;code&gt;create()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MovieAdapter(val context: Context) : RecyclerView.Adapter&amp;lt;MovieAdapter.MovieViewHolder&amp;gt;() {

    val client by lazy { MovieApiClient.create() }
    var movies: ArrayList&amp;lt;Movie&amp;gt; = ArrayList()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lazy takes in a function (note the curly brackets) and says “when someone first tries to use this property, run this function and assign it”.&lt;/p&gt;

&lt;p&gt;To initialize the context, change the &lt;code&gt;adapter&lt;/code&gt; initialize statement to include the main activity context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adapter = MovieAdapter(this.baseContext)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now change &lt;code&gt;refreshMovies()&lt;/code&gt; in the adapter to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun refreshMovies() {
    client.getMovies()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({ result -&amp;gt;
                movies.clear()
                movies.addAll(result.list.movies)
                notifyDataSetChanged()
        },{ error -&amp;gt;
                Toast.makeText(context, "Refresh error: ${error.message}", Toast.LENGTH_LONG).show()
                Log.e("ERRORS", error.message)
        })
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you’re using the client’s &lt;code&gt;getMovies()&lt;/code&gt; function which is declared at the top of &lt;code&gt;MovieApiClient.kt&lt;/code&gt;. To understand what is going on here is an entire discussion on it’s own. Basically it’s using &lt;a href="http://reactivex.io/" rel="noopener noreferrer"&gt;Reactive Programming&lt;/a&gt; which is a new way to wire things that are asynchronous (like calling out to an external API) together.&lt;/p&gt;

&lt;p&gt;For the rest of the access methods, put the following after &lt;code&gt;refreshMovies()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun updateMovie(movie: Movie) {
    client.updateMovie(movie.id, movie)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({ refreshMovies() }, { throwable -&amp;gt;
                Toast.makeText(context, "Update error: ${throwable.message}", Toast.LENGTH_LONG).show()
        })
}

fun addMovie(movie: Movie) {
    client.addMovie(movie)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({ refreshMovies() }, { throwable -&amp;gt;
                Toast.makeText(context, "Add error: ${throwable.message}", Toast.LENGTH_LONG).show()
        })
}

fun deleteMovie(movie: Movie) {
    client.deleteMovie(movie.id)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({ refreshMovies() }, { throwable -&amp;gt;
                Toast.makeText(context, "Delete error: ${throwable.message}", Toast.LENGTH_LONG).show()
        })
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you use the Retrofit reactive calls as before, but instead just call &lt;code&gt;refreshMovies()&lt;/code&gt; when things return successfully (and make a toast of the error if they do not).&lt;/p&gt;

&lt;h2&gt;
  
  
  Show Dialogs in Kotlin
&lt;/h2&gt;

&lt;p&gt;The last thing you need is to show dialogs for the various input situations: delete, edit, and add. Here you’ll do this manually so you don’t have to create any new XML.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;MainActivity.kt&lt;/code&gt; add the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun showNewDialog() {
    val dialogBuilder = AlertDialog.Builder(this)

    val input = EditText(this@MainActivity)
    val lp = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT)
    input.layoutParams = lp

    dialogBuilder.setView(input)

    dialogBuilder.setTitle("New Movie")
    dialogBuilder.setMessage("Enter Name Below")
    dialogBuilder.setPositiveButton("Save", { dialog, whichButton -&amp;gt;
        adapter.addMovie(Movie(0,input.text.toString()))
    })
    dialogBuilder.setNegativeButton("Cancel", { dialog, whichButton -&amp;gt;
        //pass
    })
    val b = dialogBuilder.create()
    b.show()
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the &lt;a href="https://developer.android.com/guide/topics/ui/dialogs" rel="noopener noreferrer"&gt;Dialog Builder&lt;/a&gt; is used to show a standard pop-up. You’ve also manually added in an &lt;code&gt;EditText&lt;/code&gt; control so the user can enter in a new name. Listeners are made for the Positive and Negative buttons (see the Adding Buttons section of the previous link), and when Positive happens (a dialog confirmation) the adapter’s &lt;code&gt;addMovie&lt;/code&gt; function is called.&lt;/p&gt;

&lt;p&gt;To make sure this dialog pops up when you click the action button, put the following at the bottom of &lt;code&gt;MainActivity.onCreate()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fab.setOnClickListener{ showNewDialog() }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the code for the other dialogs, which we’ll put into&lt;code&gt;MovieAdapter&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun showUpdateDialog(holder: MovieViewHolder, movie: Movie) {
    val dialogBuilder = AlertDialog.Builder(holder.view.context)

    val input = EditText(holder.view.context)
    val lp = LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.MATCH_PARENT,
             LinearLayout.LayoutParams.MATCH_PARENT)
    input.layoutParams = lp
    input.setText(movie.name)

    dialogBuilder.setView(input)

    dialogBuilder.setTitle("Update Movie")
    dialogBuilder.setPositiveButton("Update", { dialog, whichButton -&amp;gt;
        updateMovie(Movie(movie.id,input.text.toString()))
    })
    dialogBuilder.setNegativeButton("Cancel", { dialog, whichButton -&amp;gt;
        dialog.cancel()
    })
    val b = dialogBuilder.create()
    b.show()
}

fun showDeleteDialog(holder: MovieViewHolder, movie: Movie) {
    val dialogBuilder = AlertDialog.Builder(holder.view.context)
    dialogBuilder.setTitle("Delete")
    dialogBuilder.setMessage("Confirm delete?")
    dialogBuilder.setPositiveButton("Delete", { dialog, whichButton -&amp;gt;
        deleteMovie(movie)
    })
    dialogBuilder.setNegativeButton("Cancel", { dialog, whichButton -&amp;gt;
        dialog.cancel()
    })
    val b = dialogBuilder.create()
    b.show()
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To wire them up, add the following to the &lt;code&gt;onBindViewHolder&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;holder.view.btnDelete.setOnClickListener { showDeleteDialog(holder, movies[position]) }
holder.view.btnEdit.setOnClickListener { showUpdateDialog(holder, movies[position]) }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’re almost done. You just need to give your app permission to access the outside server (the internet). Add the following above the &lt;code&gt;&amp;lt;application&amp;gt;&lt;/code&gt; node in your &lt;code&gt;AndroidManifest.xml&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;uses-permission android:name="android.permission.INTERNET" /&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run your app. You should be able to add, edit and delete, all persisted to the back-end and each action with a dialog to confirm or with details. You’ve just made a complete CRUD client-app solution!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fnew-movie-dialog-fe6a53796db4dfed57421a3368d07142c8a69b11f4eb5dbffc1e3eb6e98e46f3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fnew-movie-dialog-fe6a53796db4dfed57421a3368d07142c8a69b11f4eb5dbffc1e3eb6e98e46f3.png" alt="Add a movie via a new dialog"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Authentication for a Secure Mobile App with Android and Kotlin
&lt;/h2&gt;

&lt;p&gt;Most modern apps require some level of security, so it’s worthwhile to know how to add authentication quickly and easily. For this, you’ll use the &lt;a href="https://github.com/okta/okta-sdk-appauth-android" rel="noopener noreferrer"&gt;OktaAppAuth&lt;/a&gt; wrapper library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Okta?
&lt;/h3&gt;

&lt;p&gt;Okta’s goal is to make &lt;a href="https://developer.okta.com/product/user-management/" rel="noopener noreferrer"&gt;identity management&lt;/a&gt; a lot easier, more secure, and more scalable than what you’re used to. Okta is a cloud service that allows developers to create, edit, and securely store user accounts and user account data, and connect them with one or multiple applications. Our API enables you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.okta.com/product/authentication/" rel="noopener noreferrer"&gt;Authenticate&lt;/a&gt; and &lt;a href="https://developer.okta.com/product/authorization/" rel="noopener noreferrer"&gt;authorize&lt;/a&gt; your users&lt;/li&gt;
&lt;li&gt;Store data about your users&lt;/li&gt;
&lt;li&gt;Perform password-based and &lt;a href="https://developer.okta.com/authentication-guide/social-login/" rel="noopener noreferrer"&gt;social login&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Secure your application with &lt;a href="https://developer.okta.com/use_cases/mfa/" rel="noopener noreferrer"&gt;multi-factor authentication&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;And much more! Check out our &lt;a href="https://developer.okta.com/documentation/" rel="noopener noreferrer"&gt;product documentation&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://developer.okta.com/signup/" rel="noopener noreferrer"&gt;Register for a forever-free developer account&lt;/a&gt;, and when you’re done, create a new Native Application and make note of the client id and redirect URI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add an OAuth 2.0 Authorization Server
&lt;/h3&gt;

&lt;p&gt;First you need to turn your server into an OAuth resource (managed by Okta). Add the following to your &lt;code&gt;build.gradle&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;compile 'com.okta.spring:okta-spring-boot-starter:0.5.0'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.security.oauth:spring-security-oauth2:2.2.0.RELEASE'
compile 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.0.1.RELEASE'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add the following import to your &lt;code&gt;Application.kt&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also annotate the class with &lt;code&gt;@EnableResourceServer&lt;/code&gt;. Then create a file called &lt;code&gt;src/main/resource/application.properties&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;okta.oauth2.issuer=https://{yourOktaDomain}/oauth2/default
okta.oauth2.clientId={clientId}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get the client ID value in the Applications tab in the Okta management website when you login.&lt;/p&gt;

&lt;p&gt;Now when you restart Spring Boot and try access your API anonymously, you will get an authorization error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\Karl&amp;gt;curl localhost:8080/movies
{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add the Android AppAuth Plugin
&lt;/h3&gt;

&lt;p&gt;To make our app use Okta you need to use the AppAuth plugin for Android. Start by creating a new activity to house the login process.&lt;/p&gt;

&lt;p&gt;Go to &lt;strong&gt;New&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Activity&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Empty Activity&lt;/strong&gt; and call it &lt;code&gt;LoginActivity&lt;/code&gt;. Inside of the created &lt;code&gt;activity_login.xml&lt;/code&gt; place the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"&amp;gt;

    &amp;lt;LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center"&amp;gt;

        &amp;lt;TextView
            android:id="@+id/auth_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="8pt"
            android:text="Initializing authorization"
            style="@style/Base.TextAppearance.AppCompat.Medium"/&amp;gt;

        &amp;lt;ProgressBar
            android:id="@+id/progress_bar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="true"/&amp;gt;

        &amp;lt;Button
            android:id="@+id/auth_button"
            style="@style/Widget.AppCompat.Button.Colored"
            android:text="Login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" /&amp;gt;

    &amp;lt;/LinearLayout&amp;gt;
&amp;lt;/android.support.constraint.ConstraintLayout&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now inside of &lt;code&gt;AndroidManifest.xml&lt;/code&gt; swap the names of the login activity and main activity so that it’s the login which is launched on startup.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fadd-auth-android-18ddb6499363e3e80bdc94341f9c5663bfba5729899f1db291b25b851168849b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Fadd-auth-android-18ddb6499363e3e80bdc94341f9c5663bfba5729899f1db291b25b851168849b.png" alt="Hello World in Android"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ve added a progress bar which will stay until the authorization connection to Okta has been made. Once it is complete you’ll hide that and then show a login button. To do that you need to change &lt;code&gt;LoginActivity.kt&lt;/code&gt;. But first, add the following to your &lt;code&gt;build.gradle&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;implementation 'com.okta.android:appauth-android:0.1.0'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That will pull in the Okta AppAuth library for Android. You also need to change the support lib version to &lt;strong&gt;25.3.1&lt;/strong&gt; so that it’s compatible with this library, so change any references to the other versions (e.g. &lt;code&gt;27.1.1&lt;/code&gt;) to that version. Also, change the minSdkVersion to &lt;code&gt;16&lt;/code&gt; and the target SDK to &lt;code&gt;25&lt;/code&gt;. Finally, add the following to &lt;code&gt;defaultConfig&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;android.defaultConfig.manifestPlaceholders = [
    // match the protocol of your "Login redirect URI"
    "appAuthRedirectScheme": "com.oktapreview.dev-628819"
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now everything should build fine.&lt;/p&gt;

&lt;p&gt;Next, change the contents of &lt;code&gt;LoginActivity.kt&lt;/code&gt; to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class LoginActivity : AppCompatActivity() {

    private var mOktaAuth: OktaAppAuth? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mOktaAuth = OktaAppAuth.getInstance(this)

        setContentView(R.layout.activity_login)

        mOktaAuth!!.init(
                this,
                object : OktaAppAuth.OktaAuthListener {
                    override fun onSuccess() {
                        auth_button.visibility = View.VISIBLE
                        auth_message.visibility = View.GONE
                        progress_bar.visibility = View.GONE
                    }

                    override fun onTokenFailure(ex: AuthorizationException) {
                        auth_message.text = ex.toString()
                        progress_bar.visibility = View.GONE
                        auth_button.visibility = View.GONE
                    }
                }
        )

        val button = findViewById(R.id.auth_button) as Button
        button.setOnClickListener { v -&amp;gt;
                val completionIntent = Intent(v.context, MainActivity::class.java)
                val cancelIntent = Intent(v.context, LoginActivity::class.java)

                cancelIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP

                mOktaAuth!!.login(
                        v.context,
                        PendingIntent.getActivity(v.context, 0, completionIntent, 0),
                        PendingIntent.getActivity(v.context, 0, cancelIntent, 0)
                )
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will try initialize Okta authentication and hide both the progress bar and the message when done, showing a login button. Right now you’ll see an error if you try to run it. We need to add in the Okta app details to our project&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure Your Okta App
&lt;/h3&gt;

&lt;p&gt;Create &lt;code&gt;app/src/main/res/raw/okta_app_auth_config.json&lt;/code&gt; and place the following inside:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "client_id": "{clientId}",
  "redirect_uri": "{redirectUriValue}",
  "scopes": ["openid", "profile", "offline_access"],
  "issuer_uri": "https://{yourOktaDomain}/oauth2/default"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;clientId&lt;/code&gt; and &lt;code&gt;redirectUriValues&lt;/code&gt; you should get from your application in the Okta management console.&lt;/p&gt;

&lt;p&gt;Now when you restart your app you should see a login button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Flogin-button-android-476e79cd6ef714761d76e5f913351793b73d9e5c8858017edabb8d62a31fe647.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.okta.com%2Fassets%2Fblog%2Fandroid-kotlin-crud%2Flogin-button-android-476e79cd6ef714761d76e5f913351793b73d9e5c8858017edabb8d62a31fe647.png" alt="Login button added to application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pressing it will take you to a pre-build Okta login screen. If you login using credentials on your Okta account you’ll be let through to our main activity but the list will be empty - and you should get a 401 error toast popup (unauthorized access).&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Authorization to Retrofit
&lt;/h3&gt;

&lt;p&gt;You need to get the access token out of the AppAuth library and pass it to Retrofit when API calls are made. Inside of &lt;code&gt;MainActivity.kt&lt;/code&gt; add the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun readAuthState(): AuthState {
    val authPrefs = getSharedPreferences("OktaAppAuthState", Context.MODE_PRIVATE)
    val stateJson = authPrefs.getString("state", "")
    return if (!stateJson!!.isEmpty()) {
        try {
            AuthState.jsonDeserialize(stateJson)
        } catch (exp: org.json.JSONException) {
            Log.e("ERROR",exp.message)
            AuthState()
        }

    } else {
        AuthState()
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It uses &lt;a href="https://developer.android.com/reference/android/content/SharedPreferences" rel="noopener noreferrer"&gt;shared preferences&lt;/a&gt; to pull out the authorization data stored by Okta.&lt;/p&gt;

&lt;p&gt;Now change the header of your adapter so that it accepts an access token as a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MovieAdapter(val context: Context, val token: String?)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then when you instantiate it in &lt;code&gt;MainActivity&lt;/code&gt; pass in the token from the auth state object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adapter = MovieAdapter(this.baseContext, readAuthState().accessToken)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you need to change the get call for movies in &lt;code&gt;MovieApiClient.kt&lt;/code&gt; to include an authorization header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@GET("movies") fun getMovies(@Header("Authorization") token:String): Observable&amp;lt;MovieEmbedded&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back in the adapter, change &lt;code&gt;refreshMovies()&lt;/code&gt; to use this new header - with the new token and a &lt;em&gt;Bearer&lt;/em&gt; prefix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun refreshMovies() {
    client.getMovies("Bearer $token")
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll need to do the same changes to the other methods (add, delete, update) in order for those functions to work with your authenticated back-end.&lt;/p&gt;

&lt;p&gt;That should be it. Redeploy the app and you should get through to a list as before - this time it is authenticated!&lt;/p&gt;

&lt;p&gt;Congratulations! You’ve just made a complete client-server solution, with a robust and compliant REST back-end, and an Android application on the front using the latest technologies, all with central, industry-standard authentication. You can find the example created in this tutorial &lt;a href="https://github.com/oktadeveloper/okta-android-kotlin-crud-example" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn More about Android, Java, and Secure Authentication
&lt;/h2&gt;

&lt;p&gt;We’ve written some other cool Spring Boot and Android tutorials, check them out if you’re interested.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.okta.com/blog/2017/03/21/spring-boot-oauth" rel="noopener noreferrer"&gt;Get Started with Spring Boot, OAuth 2.0, and Okta&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.okta.com/blog/2017/12/06/bootiful-development-with-spring-boot-and-react" rel="noopener noreferrer"&gt;Bootiful Development with Spring Boot and React&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.okta.com/blog/2018/08/10/basic-android-without-an-ide" rel="noopener noreferrer"&gt;Create a Basic Android App without an IDE&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, if you want to learn more about Kotlin a great place to look once you’ve worked with it for a bit is the Kotlin &lt;a href="https://kotlinlang.org/docs/reference/idioms.html" rel="noopener noreferrer"&gt;idioms page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have any questions, please don’t hesitate to leave a comment below, or ask us on our &lt;a href="https://devforum.okta.com/" rel="noopener noreferrer"&gt;Okta Developer Forums&lt;/a&gt;. Follow us on Twitter &lt;a href="https://twitter.com/oktadev" rel="noopener noreferrer"&gt;@oktadev&lt;/a&gt; if you want to see more tutorials like this one!&lt;/p&gt;

</description>
      <category>mobile</category>
      <category>android</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Create a Basic Android App without an IDE</title>
      <dc:creator>Karl</dc:creator>
      <pubDate>Fri, 10 Aug 2018 05:00:00 +0000</pubDate>
      <link>https://dev.to/oktadev/create-a-basic-android-app-without-an-ide-1aeh</link>
      <guid>https://dev.to/oktadev/create-a-basic-android-app-without-an-ide-1aeh</guid>
      <description>&lt;p&gt;Virtually every Android tutorial uses Android Studio to create and develop an app. This isn’t great for learning since you don’t see how things work, namely&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The components that make up an Android Studio project&lt;/li&gt;
&lt;li&gt;How builds are setup and configured&lt;/li&gt;
&lt;li&gt;What parts comprise the source&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Software development is about files and in this tutorial we’re going to go through every file in a basic Android project – first by examining what Android Studio outputs and then by building up an Android project from scratch. We won’t assume any previous Android experience, just a little Java.&lt;/p&gt;

&lt;p&gt;Note: I’ll be doing this on Windows but most instructions should work on other platforms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Break Down Your Android Studio Project
&lt;/h2&gt;

&lt;p&gt;This is what Android Studio creates when you start a completely bare project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5z7OXgXx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/bare-project-576de8b3deab805ecdcba8d5c028fd9dec71b9f4c87b56778af991fcd55cbe26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5z7OXgXx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/bare-project-576de8b3deab805ecdcba8d5c028fd9dec71b9f4c87b56778af991fcd55cbe26.png" alt="Bare project file tree" width="218" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing to notice is that most files involve Gradle, the system for configuring and executing builds. What do we have without any Gradle files ?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ip46KbxQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/bare-project-without-gradle-094673d3348e92661dfdc1a61861fa0a97cb19f3e12b159664bcae18268f0269.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ip46KbxQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/bare-project-without-gradle-094673d3348e92661dfdc1a61861fa0a97cb19f3e12b159664bcae18268f0269.png" alt="Bare project file tree without Gradle" width="216" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Only three folders and three files. Clearly the main complexity in Android projects is the build system.&lt;/p&gt;

&lt;p&gt;Let’s look at what files are not included in source control by looking at &lt;em&gt;.gitignore&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--snK6Malu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/gitignore-7c303ed5e232c95b245e721d1144fbf66df97d81dec320509055a3d5e9162a3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--snK6Malu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/gitignore-7c303ed5e232c95b245e721d1144fbf66df97d81dec320509055a3d5e9162a3e.png" alt="gitignore file contents" width="379" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;MyApplication.iml&lt;/code&gt; isn’t important. If you &lt;a href="https://stackoverflow.com/questions/30737082/what-are-iml-files-in-android-studio"&gt;Google what iml files are for&lt;/a&gt; you will see they are used by Android Studio and can be regenerated from the configurations in &lt;code&gt;.idea/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, &lt;code&gt;local.properties&lt;/code&gt; aren’t important either, as well as &lt;code&gt;build/&lt;/code&gt;. What does that leave us with? Just the&lt;code&gt;app/&lt;/code&gt; folder and some files in &lt;code&gt;.idea/&lt;/code&gt; which is where IntelliJ (which Android Studio is built on) stores configuration files.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;app&lt;/code&gt; folder you’ll find two directories and three files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;libs/&lt;/code&gt;, which is empty&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src/&lt;/code&gt;, which isn’t&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.gitignore&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;build.gradle&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ProGuard&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ProGuard helps shrink your final APK by removing unused libraries. You don’t need this file (it’s actually all commented out). The&lt;code&gt;.gitignore&lt;/code&gt; is use for source control, if you didn’t know that already. So it’s just &lt;code&gt;src/&lt;/code&gt; and &lt;code&gt;build.gradle&lt;/code&gt; that are important.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/&lt;/code&gt; contains your Java source code, the resources you use like layouts and configuration files, and the &lt;code&gt;AndroidManifest&lt;/code&gt; which tells Android what your app is. And &lt;code&gt;build.gradle&lt;/code&gt; tells Gradle how to convert your source into an APK using the Gradle Android plugin.&lt;/p&gt;

&lt;p&gt;To see all of this in action, let’s get to building our code base from the ground up, first installing the SDK, then initializing gradle, onto converting to an Android build, and finally writing the app code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started with the Android SDK
&lt;/h2&gt;

&lt;p&gt;For this project, you’ll need to download the Android SDK. This is just a ZIP file. Go to the &lt;a href="https://developer.android.com/studio/"&gt;normal install page&lt;/a&gt; and scroll right to the bottom at Command Line Tools. There you’ll find the zips which are only around 150MB. Extract and set your &lt;code&gt;ANDROID_SDK_ROOT&lt;/code&gt; environment variable to the extracted location.&lt;/p&gt;

&lt;p&gt;And that’s it! Gradle should pick this up automatically. (Note: Gradle stores the SDK location in the &lt;code&gt;local.properties&lt;/code&gt; file, which as we saw before isn’t saved to source control).&lt;/p&gt;

&lt;h2&gt;
  
  
  Initialize Gradle
&lt;/h2&gt;

&lt;p&gt;To start our project from scratch we initialize a folder using Gradle. First install Gradle. I downloaded the binary-only version from the Manual section and added the &lt;code&gt;bin&lt;/code&gt; folder to my &lt;code&gt;PATH&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;gradle&lt;/code&gt; command should now work from your command line. Note: you need to have Java 7 or higher installed as well. Here is what you see when you initialise an empty folder with &lt;code&gt;gradle init&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iUtZq4YD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/gradle-init-2b9194f1f68c141b53d1fac698d953d083ddc7e3c05ee6f645c6715d3c671e67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iUtZq4YD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/gradle-init-2b9194f1f68c141b53d1fac698d953d083ddc7e3c05ee6f645c6715d3c671e67.png" alt="Gradle init output" width="472" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See how all these files are in the Android Studio project output ? For a great explanation of what these files are see the &lt;a href="https://guides.gradle.org/creating-new-gradle-builds/"&gt;Gradle create build guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an Android Build
&lt;/h2&gt;

&lt;p&gt;Next we need to set up our project to build Android. The first step is to change &lt;code&gt;settings.gradle&lt;/code&gt; to simply include the app module (which is just a folder).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;include ':app'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, put the following into your root &lt;code&gt;build.gradle&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This primarily defines where to download our Gradle libraries from.&lt;/p&gt;

&lt;p&gt;Next, create the &lt;code&gt;/app&lt;/code&gt; directory and place the following into &lt;code&gt;app/build.gradle&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    defaultConfig {
        applicationId "com.example.karl.myapplication"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:appcompat-v7:253.1'
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This uses the Android Gradle plugin (com.android.application) and sets some values like the SDK version and Proguard (which optimizes our output size). Also, in the dependencies section it gives any libraries we want to import (here we import two, both used in building our interface later).&lt;/p&gt;

&lt;p&gt;Now create &lt;code&gt;app/src/main/res/values/styles.xml&lt;/code&gt; which we’ll use to set our app’s theme.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;resources&amp;gt;

    &amp;lt;!-- Base application theme. --&amp;gt;
    &amp;lt;style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"&amp;gt;
        &amp;lt;!-- Customize your theme here. --&amp;gt;
    &amp;lt;/style&amp;gt;

&amp;lt;/resources&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally put the following into &lt;code&gt;app/src/main/AndroidManifest.xml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.karl.myapplication"&amp;gt;

    &amp;lt;application
        android:label="Demo App"
        android:theme="@styles/AppTheme"&amp;gt;

        &amp;lt;activity android:name=".MainActivity"&amp;gt;
            &amp;lt;intent-filter&amp;gt;
                &amp;lt;action android:name="android.intent.action.MAIN" /&amp;gt;
                &amp;lt;category android:name="android.intent.category.LAUNCHER" /&amp;gt;
            &amp;lt;/intent-filter&amp;gt;
        &amp;lt;/activity&amp;gt;
    &amp;lt;/application&amp;gt;

&amp;lt;/manifest&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This defines the package, label and main activity of our app.&lt;/p&gt;

&lt;p&gt;Now when you run &lt;code&gt;gradlew build&lt;/code&gt; you should see BUILD SUCCESSFUL. And in &lt;code&gt;app/build/outputs/apk/debug&lt;/code&gt; you should see &lt;code&gt;app-debug.apk&lt;/code&gt;. You’ve just set up an Android build from scratch!&lt;/p&gt;

&lt;p&gt;To deploy this simply say &lt;code&gt;gradlew installDebug&lt;/code&gt; with your phone plugged in (and [USB Debugging enabled[(&lt;a href="https://www.howtogeek.com/129728/how-to-access-the-developer-options-menu-and-enable-usb-debugging-on-android-4.2/)"&gt;https://www.howtogeek.com/129728/how-to-access-the-developer-options-menu-and-enable-usb-debugging-on-android-4.2/)&lt;/a&gt;). You should then see a new app called Demo App. It will crash when you run it because you haven’t written any Java code yet!&lt;/p&gt;

&lt;h2&gt;
  
  
  Write the Java Application
&lt;/h2&gt;

&lt;p&gt;With your build set up next we need to write the Java. We only need two files for this: the main activity Java file, and the layout XML.&lt;/p&gt;

&lt;p&gt;Put the following into &lt;code&gt;app/src/main/java/com/example/karl/myapplication/MainActivity.java&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.karl.myapplication;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It just creates a new Activity (a core process-like idea in Android) and sets the view to a layout in the Resources folder.&lt;/p&gt;

&lt;p&gt;Put this into &lt;code&gt;app/src/main/res/layout/activity_main.xml&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"&amp;gt;

    &amp;lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" /&amp;gt;

&amp;lt;/android.support.constraint.ConstraintLayout&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This just creates a “Hello World!” message center-screen.&lt;/p&gt;

&lt;p&gt;Now run &lt;code&gt;gradlew build&lt;/code&gt; and you should see BUILD SUCCESSFUL again. Use &lt;code&gt;gradlew installDebug&lt;/code&gt; to install to your phone and you should see the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D0vvXz9l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/hello-world-2d12ddd0d978d4bc09026451420fe92032ea08c6fef8f64778c3fceb3854d279.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D0vvXz9l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/hello-world-2d12ddd0d978d4bc09026451420fe92032ea08c6fef8f64778c3fceb3854d279.png" alt="Hello world Android screen" width="800" height="1422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ve just made a working Android app with nothing but a text editor :).&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Authentication with Okta
&lt;/h2&gt;

&lt;p&gt;Most modern apps require some level of security, so it’s worthwhile to know how to build authentication simply and easily. For this, we’ll use the &lt;a href="https://github.com/okta/okta-sdk-appauth-android"&gt;OktaAppAuth&lt;/a&gt; wrapper library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Okta?
&lt;/h3&gt;

&lt;p&gt;At Okta, our goal is to make &lt;a href="https://developer.okta.com/product/user-management/"&gt;identity management&lt;/a&gt; a lot easier, more secure, and more scalable than what you’re used to. Okta is a cloud service that allows developers to create, edit, and securely store user accounts and user account data, and connect them with one or multiple applications. Our API enables you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.okta.com/product/authentication/"&gt;Authenticate&lt;/a&gt; and &lt;a href="https://developer.okta.com/product/authorization/"&gt;authorize&lt;/a&gt; your users&lt;/li&gt;
&lt;li&gt;Store data about your users&lt;/li&gt;
&lt;li&gt;Perform password-based and &lt;a href="https://developer.okta.com/authentication-guide/social-login/"&gt;social login&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Secure your application with &lt;a href="https://developer.okta.com/use_cases/mfa/"&gt;multi-factor authentication&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;And much more! Check out our &lt;a href="https://developer.okta.com/documentation/"&gt;product documentation&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Are you sold? &lt;a href="https://developer.okta.com/signup/"&gt;Register for a forever-free developer account&lt;/a&gt;, and when you’re done, come on back so we can learn more about building secure mobile apps!&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication in Java
&lt;/h3&gt;

&lt;p&gt;Create a new Activity called &lt;code&gt;LoginActivity.java&lt;/code&gt; and place it in the same folder as MainActivity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.karl.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import com.okta.appauth.android.OktaAppAuth;
import net.openid.appauth.AuthorizationException;

public class LoginActivity extends Activity {

    private OktaAppAuth mOktaAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mOktaAuth = OktaAppAuth.getInstance(this);

        // Do any of your own setup of the Activity

        mOktaAuth.init(
            this,
            new OktaAppAuth.OktaAuthListener() {
                @Override
                public void onSuccess() {
                    // Handle a successful initialization (e.g. display login button)
                }

                @Override
                public void onTokenFailure(@NonNull AuthorizationException ex) {
                    // Handle a failed initialization
                }
            }
        );
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This initializes the &lt;code&gt;OktaAppAuth&lt;/code&gt; object and handles the Success or Failure conditions. Next, change &lt;code&gt;AndroidManifest.xml&lt;/code&gt; to point to &lt;code&gt;LoginActivity&lt;/code&gt; instead of &lt;code&gt;MainActivity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now add the following to the &lt;code&gt;defaultConfig&lt;/code&gt; section of &lt;code&gt;app/build.config&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;android.defaultConfig.manifestPlaceholders = [
            "appAuthRedirectScheme": "com.okta.example"
        ]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, add the following to the same file’s dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;implementation 'com.okta.android:appauth-android:0.1.0'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should build and deploy. You can use &lt;code&gt;logcat&lt;/code&gt; to see what is happening in the background. Looking at the &lt;a href="https://github.com/okta/okta-sdk-appauth-android/blob/master/library/src/main/java/com/okta/appauth/android/OktaAppAuth.java"&gt;source code for the main library class&lt;/a&gt; we see the tag we need to use is “OktaAppAuth”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VpbgyQaz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/logcat-output-1770d20f94ce0b85227f1d650480d2de4acf49607fb9448a30a4e6a473a85dcd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VpbgyQaz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/logcat-output-1770d20f94ce0b85227f1d650480d2de4acf49607fb9448a30a4e6a473a85dcd.png" alt="Logcat output" width="730" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right after trying to create the service we get a &lt;code&gt;Configuration was invalid&lt;/code&gt; error. We need to connect our app to an Okta account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connect to Okta for Authentication
&lt;/h3&gt;

&lt;p&gt;Since you already have an &lt;a href="https://developer.okta.com/signup/"&gt;Okta developer account&lt;/a&gt;, you can move right on to configuration. From the developer console select the Applications tab and then New Application. Select Native and click next. The fields should auto-populate correctly. The most important part is the redirect URL. Click done.&lt;/p&gt;

&lt;p&gt;On the Assignments tab, click the Assign dropdown and choose Assign to Groups. Click Assign next to the Everyone group. Now, anyone in your Okta organization will be able to authenticate to the application.&lt;/p&gt;

&lt;p&gt;You now should have enough to populate &lt;code&gt;app/src/main/res/raw/okta_app_auth_config.json&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "client_id": "{clientIdValue}",
  "redirect_uri": "{redirectUriValue}",
  "scopes": ["openid", "profile", "offline_access"],
  "issuer_uri": "https://dev-628819.oktapreview.com/oauth2/default"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change the &lt;code&gt;appAuthRedirectScheme&lt;/code&gt; field in &lt;code&gt;app/build.gradle&lt;/code&gt; to the base of your redirect URI, e.g. &lt;code&gt;"appAuthRedirectScheme": "com.oktapreview.dev-628819"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Configuration should now be complete! If you &lt;code&gt;gradlew installDebug&lt;/code&gt; and do the logcat as before you should no longer be seeing the error when you open the app, and should see a message saying &lt;code&gt;Warming up browser instance for auth request&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set Up Your Login Page
&lt;/h3&gt;

&lt;p&gt;Let’s add a button and progress bar to our login page. Create app/src/main/res/layout/activity_login.xml.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"&amp;gt;

    &amp;lt;LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center"&amp;gt;

            &amp;lt;TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingBottom="8pt"
                android:text="Demo App"
                style="@style/Base.TextAppearance.AppCompat.Title"/&amp;gt;

            &amp;lt;ProgressBar
                    android:id="@+id/progress_bar"
                    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:indeterminate="true"/&amp;gt;

            &amp;lt;Button
                android:id="@+id/auth_button"
                style="@style/Widget.AppCompat.Button.Colored"
                android:text="Login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:visibility="gone" /&amp;gt;

    &amp;lt;/LinearLayout&amp;gt;

&amp;lt;/android.support.constraint.ConstraintLayout&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initially the button is hidden. We’ll show that (and hide the progress bar) once Okta has finished initializing. To do that put the following into the onSuccess() method in &lt;code&gt;LoginActivity.java&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    findViewById(R.id.auth_button).setVisibility(View.VISIBLE);
    findViewById(R.id.progress_bar).setVisibility(View.GONE);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, before the Okta init set the layout to the XML we just created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    setContentView(R.layout.activity_login);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you &lt;code&gt;installDebug&lt;/code&gt; and run the app you should see a title with a login button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y0OgdXAA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/main-screen-demo-f4f07697a9028fbe8eb781c9a73c23445530a73313b134b1b190df63ae882ad6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y0OgdXAA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/main-screen-demo-f4f07697a9028fbe8eb781c9a73c23445530a73313b134b1b190df63ae882ad6.png" alt="Main screen for Demo App" width="800" height="1422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wire Up Login
&lt;/h3&gt;

&lt;p&gt;To capture the login details, i.e. username/password, we can use a default page. First create the landing page for when we are authorized in &lt;code&gt;AuthorizedActivity.java&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.karl.myapplication;

import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.view.View;

import com.okta.appauth.android.OktaAppAuth;
import static com.okta.appauth.android.OktaAppAuth.getInstance;
import net.openid.appauth.AuthorizationException;

public class AuthorizedActivity extends Activity {

    private OktaAppAuth mOktaAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mOktaAuth = getInstance(this);

        setContentView(R.layout.activity_authorized);

        Button button = (Button) findViewById(R.id.sign_out);
        button.setOnClickListener(new View.OnClickListener()
{
        @Override
        public void onClick(View v)
        {
            mOktaAuth.logout();

            Intent mainIntent = new Intent(v.getContext(), LoginActivity.class);
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mainIntent);
            finish();
        }
    }
      );
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We attach a listener to the button logging us out and taking us back to the login page. Now in &lt;code&gt;activity_authorized.xml&lt;/code&gt; put&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"&amp;gt;

    &amp;lt;LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center"&amp;gt;

            &amp;lt;TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingBottom="8pt"
                android:text="Authorized"
                style="@style/Base.TextAppearance.AppCompat.Title"/&amp;gt;

            &amp;lt;Button
                android:id="@+id/sign_out"
                style="@style/Widget.AppCompat.Button.Colored"
                android:text="Logout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" /&amp;gt;

    &amp;lt;/LinearLayout&amp;gt;

&amp;lt;/android.support.constraint.ConstraintLayout&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As with the login page it’s just a title with a button. Wire up the login button in &lt;code&gt;LoginActivity.java&lt;/code&gt; by placing the following at the end of the onCreate method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Button button = (Button) findViewById(R.id.auth_button);
button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent completionIntent = new Intent(v.getContext(), AuthorizedActivity.class);
            Intent cancelIntent = new Intent(v.getContext(), LoginActivity.class);

            cancelIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            mOktaAuth.login(
                v.getContext(),
                PendingIntent.getActivity(v.getContext(), 0, completionIntent, 0),
                PendingIntent.getActivity(v.getContext(), 0, cancelIntent, 0)
            );
        }
    }
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when you click the Login button you should see an Okta login page asking for your details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aJ8eDLx_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/okta-login-screen-768b9d673c71580195af2cc73a6c1c52b2ab540f2017b2488915364ccc229efa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aJ8eDLx_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/okta-login-screen-768b9d673c71580195af2cc73a6c1c52b2ab540f2017b2488915364ccc229efa.png" alt="Okta login screen" width="800" height="1422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you enter in user details which are correct for the Application we made previously (your Okta portal credentials should work) you are taken to an authorized page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pgXzvZIH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/authorized-screen-a502b99ce0324894ca9a62a773c9fdd861b8f7a778f7f95be6542b7b049a64c6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pgXzvZIH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.okta.com/assets/blog/build-android-app-without-ide/authorized-screen-a502b99ce0324894ca9a62a773c9fdd861b8f7a778f7f95be6542b7b049a64c6.png" alt="Authorized screen" width="800" height="1422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clicking the logout button should take you back to our first screen.&lt;/p&gt;

&lt;p&gt;And that’s it for authorization!&lt;/p&gt;

&lt;p&gt;Most people think you need Android Studio to make an Android app. In this post, you shattered that notion and built an Android app from scratch. With some configuration and a little code, you integrated authentication into your app with the OktaAppAuth library. Then you created a view that only authenticated users can see. From here, you can build out the rest of your app safe in the knowledge that authentication is handled, thanks to Okta.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn More about Java and Secure App Development
&lt;/h2&gt;

&lt;p&gt;I hope you’ve enjoyed this tutorial on how to build a basic Android app without an IDE. You can find the example created in this tutorial on GitHub at &lt;a href="https://github.com/oktadeveloper/okta-android-example"&gt;https://github.com/oktadeveloper/okta-android-example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ve written some other cool Spring Boot and React tutorials, check them out if you’re interested.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/oktadev/add-authentication-to-any-web-page-in-10-minutes-jhk-temp-slug-9795678"&gt;Add Authentication to Any Web Page in 10 Minutes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.okta.com/blog/2017/12/06/bootiful-development-with-spring-boot-and-react"&gt;Bootiful Development with Spring Boot and React&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.okta.com/blog/2018/03/16/build-react-native-authentication-oauth-2"&gt;Build a React Native Application and Authenticate with OAuth 2.0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have any questions, please don’t hesitate to leave a comment below, or ask us on our &lt;a href="https://devforum.okta.com/"&gt;Okta Developer Forums&lt;/a&gt;. Follow us on Twitter &lt;a href="https://twitter.com/oktadev"&gt;@oktadev&lt;/a&gt; if you want to see more tutorials like this one!&lt;/p&gt;

</description>
      <category>mobile</category>
      <category>android</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
