<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kartik Mittal on Medium]]></title>
        <description><![CDATA[Stories by Kartik Mittal on Medium]]></description>
        <link>https://medium.com/@mittalkartik1?source=rss-daddfd52897b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UDqwEDs2I7ptzHKfFIAMbw.jpeg</url>
            <title>Stories by Kartik Mittal on Medium</title>
            <link>https://medium.com/@mittalkartik1?source=rss-daddfd52897b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 21:30:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mittalkartik1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Exploring React 19: The New use API with Suspense]]></title>
            <link>https://mittalkartik1.medium.com/susexploring-react-19-the-new-use-api-with-suspense-4be658cf7ee2?source=rss-daddfd52897b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4be658cf7ee2</guid>
            <dc:creator><![CDATA[Kartik Mittal]]></dc:creator>
            <pubDate>Sun, 09 Feb 2025 15:04:48 GMT</pubDate>
            <atom:updated>2025-02-09T15:07:18.170Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*r2taflTLLN05iSs7KqwSxA.jpeg" /></figure><p>React 19 has introduced a lot of exciting features, but in this article we are going to focus on new use API with Suspense. Lets take a usual scenario where you need to hit a backend api as soon as your component mounts. So most probably you are going to implement this in following manner —</p><ol><li>Declaring atleast two state variables, one for loading state and other one for storing response or error.</li><li>Adding a <strong>useEffect</strong> hook with empty dependency array in which you are going to call your async function to update loading and response state variable which will update the UI.</li><li>Other than this you are going to handle different cases on the basis of state variables like what to render if state is loading or if response is null.</li></ol><p>What if I say, you don’t need to do all this from now. Interesting right? Let’s directly deep dive into code comparing the old and new approach to see how we can achieve this.</p><p><strong>Old Approach —</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76727932d72f1d1b2456af2b0f0eb7a9/href">https://medium.com/media/76727932d72f1d1b2456af2b0f0eb7a9/href</a></iframe><p>In above code as you can see that we are using <strong>isLoading</strong> and <strong>response </strong>state variables for handling loading, response and error state in our child component (AlbumList), which further using <strong>useEffect</strong> hook to trigger the api when component mounts.</p><p><strong>New Approach —</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd034cca75fc6b834f6e85d5544d3dda/href">https://medium.com/media/fd034cca75fc6b834f6e85d5544d3dda/href</a></iframe><p>So, what’s happening here? Let’s see step by step —</p><ol><li>We have wrapped our child component (AlbumList) with Suspense which allow us to define a fallback component, that will be shown to user until promise passed to the child component will not get fulfilled (reject or resolve).</li><li>For getting data from backend api, we are using new use API at <strong>line 5</strong>, which suspends the execution of component until promise passed to it will not get fulfilled. It means code written below this line will not get executed for that time whether there is any useEffect or any return method. It’s kind of freezing the component until the execution gets completed. Once promise gets fulfilled, component will be <strong>rerendered</strong> and data will be shown.</li><li>Also, if you don’t want to handle promise error by yourself, then you can omit try catch block (see <strong>getAlbums</strong> function above) and can rely on <a href="https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary"><strong>ErrorBoundary</strong></a><strong> </strong>component in react. Just you need to wrap &lt;Suspense /&gt; with &lt;ErrorBoundary /&gt; providing a fallback that will be rendered if any error occurs.</li></ol><h3>Points to remember —</h3><ul><li>use API<strong> </strong>does not support promises created in render. So, you cannot create a promise inside child component and call inside use. Like this —</li></ul><pre>const AlbumsList = () =&gt; {<br><br>  async function getAlbums() {<br>    await new Promise(resolve =&gt; {<br>      setTimeout(resolve, 3000);<br>    });<br>  <br>    return [<br>      {<br>        id: 13,<br>        title: &#39;Let It Be&#39;,<br>        year: 1970,<br>      }<br>    ];<br>  }<br><br>  const albumsListOrError = use(getAlbums()); //This will trigger unnecessary rerenders and execution will not go beyond this point.<br>  return &lt;&gt;&lt;/&gt;;<br>}</pre><ul><li>Unlike React Hooks, use can be called within loops and conditional statements like <strong>if</strong>. Like React Hooks, the function that calls use must be a Component or a Hook.</li><li>use can be used without Suspense but only <strong>Suspense-enabled</strong> data sources will activate the Suspense component. They include:</li></ul><ol><li>Data fetching with Suspense-enabled frameworks like <a href="https://relay.dev/docs/guided-tour/rendering/loading-states/"><strong>Relay</strong></a> and <a href="https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming#streaming-with-suspense"><strong>Next.js</strong></a></li><li>Lazy-loading component code with <a href="https://react.dev/reference/react/lazy">lazy</a></li><li>Reading the value of a cached Promise with <a href="https://react.dev/reference/react/use">use</a></li></ol><h3><strong>References</strong> —</h3><ul><li><a href="https://react.dev/blog/2024/12/05/react-19#new-feature-use">https://react.dev/blog/2024/12/05/react-19#new-feature-use</a></li><li><a href="https://react.dev/reference/react/use">https://react.dev/reference/react/use</a></li><li><a href="https://react.dev/reference/react/Suspense">https://react.dev/reference/react/Suspense</a></li></ul><p><strong><em>Enjoyed this article? Have thoughts or questions about React.js or React Native?</em></strong></p><p>Follow me on <a href="https://linkedin.com/in/mittalkartik1">LinkedIn</a> for more insights and updates, and leave a comment below — I’d love to hear from you! If you found this article helpful, give it a round of applause and share it with your colleagues to spread the knowledge.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4be658cf7ee2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transitioning from Microsoft App Center: Exploring GitHub Actions, Jenkins and Fastlane]]></title>
            <link>https://mittalkartik1.medium.com/transitioning-from-microsoft-app-center-exploring-github-actions-jenkins-and-fastlane-116fbe3c0a1b?source=rss-daddfd52897b------2</link>
            <guid isPermaLink="false">https://medium.com/p/116fbe3c0a1b</guid>
            <category><![CDATA[appcenter]]></category>
            <category><![CDATA[fastlane]]></category>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[mobile-app-automation]]></category>
            <category><![CDATA[jenkins]]></category>
            <dc:creator><![CDATA[Kartik Mittal]]></dc:creator>
            <pubDate>Fri, 24 May 2024 17:44:56 GMT</pubDate>
            <atom:updated>2024-05-24T17:47:54.528Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q67s22fzYyTBpIx2ge_nbQ.png" /></figure><blockquote>This article focuses on the journey of finding an alternative solution for app build and deployment automation. It details the decision-making process, challenges faced, and the ultimate solution chosen, without delving into specific code implementations.</blockquote><h4><strong>Introduction —</strong></h4><p>When Microsoft announced that Visual Studio App Center is scheduled for retirement on <em>March 31, 2025</em>, it left many developers searching for an alternative solution for automating their app build and deployment processes. So, I started to find out an another alternative for implementing CI/CD that would be both reliable and cost-effective. I wanted a solution that will fulfil my following requirements —</p><ol><li>I can<strong> manually trigger</strong> the app build and deployment process instead of having it automatically triggered by events such as code pushes to a specific branch or the creation of pull requests etc.</li><li>I <strong>don’t want to use a MAC</strong> for this. Allocating a Mac machine would not be a cost-effective solution.</li><li>Before starting the process, I can <strong>incorporate inputs</strong> such as modifying environment variables in my code, specifying the platform for the build (Android or iOS), or triggering builds for staging and production environments separately.</li></ol><h4><strong>Exploring Jenkins —</strong></h4><p>Jenkins is a popular open source automation server for building and deploying softwares. When I started to explore it, I found that it was best for all my requirements, but only issue was that I wanted to use a linux machine for this and building iOS apps in not possible on linux machines. So for android, I can use this but for iOS I need to find an another solution.</p><h4><strong>Exploring CircleCI —</strong></h4><p>CircleCI is also a popular CI/CD tool. I agreed to use it for building my iOS app since it will provide it’s own virtual machine, until I found out that I cannot trigger manual builds from this, which was one of my primary requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kWNDlj2ZesDCtgxInDoF_Q.png" /></figure><h4><strong>Exploring Xcode Cloud —</strong></h4><p>Xcode cloud was great for building and deploying iOS apps, but again, it does not support api, which I can hit from jenkins job to start building my app whenever I want. So, I still need to find an another alternative to build my iOS app.</p><h4><strong>Adopting Github Actions —</strong></h4><p>After I explored github actions, I found out that it was best suited for all my primary requirements. I can trigger build process manually, passing some input options using it’s api, that can be integrated <strong>inside Jenkin’s job</strong> to trigger the iOS build and deployment process.</p><h4><strong>On the way of executing the plan —</strong></h4><p>I can now start to create a job in Jenkins for automatic build and deployment process of my android and iOS apps, but another challenge I faced was that it was kind of complex to write code manually for deployment processes. I need to handle so much things for both platforms such as handling some specific apis and their responses for uploading process. For iOS, I also need to manage the certificates, provisioning profiles and private keys. So, I started to explore if I can avoid handling so much things all by myself and guess what, I found <strong>Fastlane</strong>, an open source platform for simplifying app’s deployment processes. It can handle the deployment process all by itself, just I need to write and execute some simple script for this.</p><h4><strong>Where To Upload Builds —</strong></h4><p>For both android and iOS, I need to deploy staging and production builds. So, for staging, I consider 2 options, either to use <strong>firebase app distribution</strong> for both platforms or to use <strong>internal testing track</strong> in Google play console for android, and <strong>testflight</strong> for iOS. So, after comparing both options, I choose firebase app distribution for android because it was easy to share and test builds using this and testflight for iOS because for firebase I need to create iOS build using adhoc profile and need to add tester’s udid in apple app store connect, but for testflight, I just need to add tester’s email.</p><h4><strong>The Plan —</strong></h4><p>Since I already installed necessary things for building android and iOS apps such as JAVA, android command line tools or android studio for android app, Xcode for iOS app, react native or flutter sdk for building cross platform’s apps, I started to setup the following things —</p><ol><li>Jenkins server on my machine.</li><li>Fastlane</li><li>Firebase cli, for distributing apps using firebase app distribution.</li></ol><p>After setting up above things, I started to execute my plan as follows —</p><ol><li>I wrote a script that can be executed in my jenkins job to build and deploy apps. In this, I added the code to get the parameterized variable’s value that can be used to determine which platform’s build to execute (android or iOS) and for which flavor, build needs to be deployed (staging or production).</li><li>Then, I setup fastlane for both platforms by adding Fastfile, Appfile and Matchfile in both android and iOS directories.</li><li>After that, for iOS, I added <strong>.github/workflows/main.yml</strong> file in my project, that contains code for iOS app build and deployment. This file will be used by github actions api to build and deploy ios app. Basically this file contains steps that choose which mac machine to be used for building iOS app, setting up the xcode, configuring react native or flutter (for cross platform apps), take some custom enviroment variables, and execute a custom script that can handle ios app build and deployment. For the custom script, I used fastlane for building and deploying the iOS app.</li></ol><p>Then, I modified my jenkins script specified in step 1, and added commands for building android app and then added fastlane command to deploy my android app. For iOS, I added the command to execute github action’s api that can trigger the manual workflow on github on the basis of <strong>main.yml</strong> file specified in previous step passing some custom env variables.</p><h4><strong>Useful Links —</strong></h4><ol><li><strong>Installing Jenkins</strong> — <a href="https://www.jenkins.io/doc/book/installing/linux/#debianubuntu">https://www.jenkins.io/doc/book/installing/linux/#debianubuntu</a></li><li><strong>Github Actions Api</strong> — <a href="https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event">https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event</a></li><li><strong>Fastlane</strong> — <a href="https://docs.fastlane.tools/">https://docs.fastlane.tools</a>/</li><li><strong>Fastlane match</strong> (<strong><em>To handle iOS certificates, profiles and private keys</em></strong>) — <a href="https://docs.fastlane.tools/actions/match/">https://docs.fastlane.tools/actions/match/</a></li><li><strong>Firebase CLI</strong> — <a href="https://firebase.google.com/docs/cli#install-cli-mac-linux">https://firebase.google.com/docs/cli#install-cli-mac-linux</a></li><li><strong>Firebase android app distribution — </strong><a href="https://firebase.google.com/docs/app-distribution/android/distribute-cli?apptype=apk#step_2_distribute_your_app_to_testers">https://firebase.google.com/docs/app-distribution/android/distribute-cli?apptype=apk#step_2_distribute_your_app_to_testers</a></li></ol><p><strong><em>Enjoyed this article? Have thoughts or questions about app build automation?</em></strong></p><p>Follow me on <a href="https://linkedin.com/in/mittalkartik1">LinkedIn</a> for more insights and updates, and leave a comment below — I’d love to hear from you! If you found this article helpful, give it a round of applause and share it with your colleagues to spread the knowledge.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=116fbe3c0a1b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Complete Guide to Deploy Flutter Builds Using App Center]]></title>
            <link>https://mittalkartik1.medium.com/the-complete-guide-to-deploy-flutter-builds-using-app-center-3f784fefd5fe?source=rss-daddfd52897b------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f784fefd5fe</guid>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[appcenter]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Kartik Mittal]]></dc:creator>
            <pubDate>Tue, 04 Jul 2023 05:46:26 GMT</pubDate>
            <atom:updated>2023-07-04T05:46:26.581Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HuNVVfmNlNEwgoj3WYWJkg.jpeg" /></figure><p>In today’s fast-paced world of mobile app development, having a streamlined and efficient process for building and distributing applications is crucial. Flutter, Google’s UI toolkit, has gained immense popularity for its cross-platform capabilities and rapid development cycle. However, while App Center by Microsoft is a powerful tool for mobile app distribution and continuous integration, it does not currently offer direct support for creating Flutter builds.<br>In this article, we will explore the step-by-step process of creating Flutter Android and iOS builds using App Center. By following the detailed instructions, code snippets, and relevant screenshots provided in each section, you will gain a thorough understanding of how to configure App Center for Flutter builds and automate the build process.</p><p>Get ready to unlock the potential of Flutter and App Center, empowering you to deliver high-quality mobile apps with ease. Let’s dive in!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*WOWh1SVKg648MBJdqwMNzg.gif" /></figure><p><strong>ANDROID —</strong></p><ol><li>First you need to add a new app in App Center and enter the mandatory fields as shown in screenshot —</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QGD_LrNTY70ZnnusrKpPfw.png" /></figure><p><strong>2. </strong>Now go to the <strong>Build</strong> tab and connect to the repo using preferred service (Eg:- Github) as shown in the screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZkXZxCtKqZkRQI7nG4-vgg.png" /></figure><p><strong>3.</strong> Once you have connected your repo to the appcenter, now you need to configure the build for the automatic deploying process. For that, go to the <strong>Build </strong>tab and select the branch from which you want to create the app. But if you try to configure the build, you will see a message in the appcenter saying ‘<strong><em>We couldn’t find any Android projects in your branch.</em></strong>’ Since, appcenter is not able to find the android project, you need to comment 3 ignored files from <strong>android/.gitignore</strong> file. The files are —<strong> gradle-wrapper.jar</strong>, <strong>gradlew</strong> and <strong>gradlew.bat </strong>as shown in below screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TY5jLq1utUzL16Wlyap9Qg.png" /></figure><p><strong>4.</strong> Now if you try again to configure build, you will see that appcenter is able to find the android project successfully as shown in screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P5--ICJTIhDFL6IQ_f3fUA.png" /></figure><p>After this, you need to add some scripts for the appcenter’s build process so that android apk or bundle can be successfully created. If you don&#39;t have basic understanding about appcenter’s scripts then you can check <a href="https://learn.microsoft.com/en-us/appcenter/build/custom/scripts/">this</a> link. For our project we need to add <strong>appcenter-post-clone.sh</strong> file in <strong>android/app</strong> directory and add the following content into it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1373a9e3e8db81356e0e6aefb5b1b5f/href">https://medium.com/media/b1373a9e3e8db81356e0e6aefb5b1b5f/href</a></iframe><p>This script will setup the flutter environment in the appcenter before starting the build process.</p><p><strong>5.</strong> Now if you go to the appcenter and click on configure build again, you will see that Post-clone script is shown in <strong>Build scripts</strong> section as shown in below screenshot. It means that script is successfully added and you are good to go. Now you need to add an environment variable in the configuration as — <strong>JAVA_HOME : $(JAVA_HOME_11_X64)</strong>. Now just click on <strong>Save &amp; Build </strong>and your app will start building. But wait, if you try to create a build then build <strong>might</strong> fail after sometime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OyWZ6PnL3_BPNnouWUe5Lw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q_cbzxB7c3rNJxGD4R5qww.png" /></figure><p>If you check the logs, you will see some issues in <strong>settings.gradle </strong>file in android directory as shown in below screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IF_CV_y7WS4Io_VR0-Iwpw.png" /></figure><p>If you have faced same issue, then you need to follow below step—</p><p><strong>6.</strong> (<strong>Need to be implemented in case you faced above issue</strong>) — Go to the android/.gitignore file and comment the <strong>/local.properties</strong> line there.<br>This will let you commit the local.properties file to your repo. But it is not advisable to commit this file because every system has its own version of this file, so we are going to commit the empty file and modify this file while creating the build by modifying <strong>settings.gradle</strong> file which is present in android directory as below —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6817a7a67c93fe3ff5bdc9146afdd1d0/href">https://medium.com/media/6817a7a67c93fe3ff5bdc9146afdd1d0/href</a></iframe><p>As you can see that we are using an environment variable ‘<strong>IS_CI_BUILD</strong>’ in above file to check whether building is getting created from appcenter or manually. For this you need to add the environment variable in config of you appcenter build and set its value to true.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FeXQkqTUUD6Wb4PGAzisCQ.png" /></figure><p><strong>7.</strong> Now if you will try to create the build, then it will be successfully created, but cannot be distributed since appcenter will try to find the build in a specific directory which is different from the path of the build where flutter usually places the apk or the bundle after building.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5b-mTCp4Zb5kWmpWQkDVKg.png" /></figure><p>So, you need to add a gradle task in <strong>android/app/build.gradle</strong> file inside the <strong>android</strong> section to copy the apk or bundle from the actual path to the destination path where appcenter is trying to find the build.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1be5af39cba8e796c1118d5417ef573d/href">https://medium.com/media/1be5af39cba8e796c1118d5417ef573d/href</a></iframe><p>Please read the comments carefully in above gradle task to customize the task according to your project configuration. <strong>IS_CI_BUILD </strong>is an environment variable in appcenter that will allow the task to only run for appcenter builds, same as specified in step 6.</p><p><strong>8.</strong> Now try again to build the android app using appcenter, and this time build will be successful without any issues and our custom gradle task also works fine as you can see in below screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cFzYGJwiK7_YgOEKYf7y-A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*2ow0c1N8zdqx5rVZZQwEig.gif" /></figure><p><strong>iOS —</strong></p><p>For iOS it is quite straightforward. You just need to add an appcenter script that will run after the code is cloned by appcenter. Steps are as follows —</p><ol><li>Just add a new app like step 1 in android in appcenter with <strong>iOS</strong> as OS and <strong>Swift/Objective-C</strong> as Platform.</li><li>Connect the repo same as step 2 in Android.</li><li>Now if you try to configure build you will that iOS project is visible in Build app section. No need to comment any files in .gitignore file.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oOQegTZvIJp_NBAmaq2IYg.png" /></figure><p>4. Now add an appcenter script (<strong>appcenter-post-clone.sh</strong>) in iOS directory and add the following content into it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f41e1e9cb31049d1ef553ea1537a1eb3/href">https://medium.com/media/f41e1e9cb31049d1ef553ea1537a1eb3/href</a></iframe><p>5. Just try to build the app now, and you will see that build is successful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*j76GLb-HDg7yumx-kd8o3g.gif" /></figure><p>Thank you for taking the time to read the article. If you encounter any challenges or have any questions, please feel free to reach out to me. I would be happy to assist you. Stay connected by connecting with me on <a href="https://www.linkedin.com/in/kartik-mittal-18ab23106">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f784fefd5fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Native Modules in React Native using java and swift]]></title>
            <link>https://mittalkartik1.medium.com/native-modules-in-react-native-using-java-and-swift-c05b402674b4?source=rss-daddfd52897b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c05b402674b4</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[native-modules]]></category>
            <category><![CDATA[mobile-application-dev]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Kartik Mittal]]></dc:creator>
            <pubDate>Sat, 21 May 2022 11:02:47 GMT</pubDate>
            <atom:updated>2022-05-28T05:27:24.306Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wTW-l3JaJTcpIa9u4YjSYw.png" /></figure><p>If you are here, reading this article, then probably you are at a point where you are unable to achieve the desired functionality with react native packages and you have to implement some code on the native side to achieve that. So, if you are thinking that you do not have native side knowledge and you are unable to write code in java or swift, then you are probably wrong because this article will explain the <strong>easiest</strong> way by which you can implement native modules in just a few minutes.<br>I am assuming that you have already set up your react-native project at this point, and if not, then maybe this article is too early for you to start with.</p><blockquote>The maximum version of React Native supported for this is <strong>0.67.4</strong>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*YRtc0mX0SwlDXZeeb02HGg.jpeg" /></figure><p>Let’s start the code from android first —</p><h3>Android —</h3><ul><li>Open your project’s android folder in Android Studio.</li><li>Navigate to the folder which contains <strong>MainApplication.java</strong> and create a new java class there (Let’s say <strong>NativeAppModule.java</strong>).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W2aJnVhusQH1nrUwkHy9oQ.png" /></figure><ul><li>Now extends the java class with <strong>ReactContextBaseJavaModule </strong>and implement abstract function <em>getName()</em> as shown below.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c75fa5089548bccf802c97e2293e99a/href">https://medium.com/media/0c75fa5089548bccf802c97e2293e99a/href</a></iframe><ul><li>Now you need to add functions in this class annotating them with <strong>@ReactMethod</strong> to call those on react native side. I have created 4 different functions to cater to different scenarios according to one’s needs. Comments are added in the below code to know more about their usage. Feel free to add accordingly —</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2462cbd1e7efe41826862e3b3e64817/href">https://medium.com/media/e2462cbd1e7efe41826862e3b3e64817/href</a></iframe><ul><li>If you want to listen to certain events or callbacks from the android side to the react-native side then you will need to create an emitter function and call that function in your java class accordingly to emit the values to the react-native side as shown below —</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9239553170597fc91a62bbe370305a90/href">https://medium.com/media/9239553170597fc91a62bbe370305a90/href</a></iframe><ul><li>Now we need to make these functions accessible to react-native. For that, firstly we need to create a new class (Let’s say <strong>MyPackage.java</strong>) and <em>implements </em>that class with <strong>ReactPackage </strong>and override the necessary functions <strong><em>createNativeModules</em></strong> and <strong><em>createViewManagers </em></strong><em>as shown below —</em></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8aee1dfa9056321a39e126c64dcf2e4/href">https://medium.com/media/e8aee1dfa9056321a39e126c64dcf2e4/href</a></iframe><ul><li>Now the last step is to add the previously created class (<strong>MyPackage.java</strong>) to the list of packages in <em>getPackages()</em> in your autogenerated <strong>MainApplication.java</strong> class as shown below —</li></ul><pre>@Override<br>protected List&lt;ReactPackage&gt; <strong>getPackages</strong>() {<br>  @SuppressWarnings(&quot;UnnecessaryLocalVariable&quot;)<br>  List&lt;ReactPackage&gt; packages = new PackageList(this).getPackages();<br>  <em>// Packages that cannot be autolinked yet can be added manually here, for example:<br>   </em><strong>packages.add(new MyPackage());</strong><br>  return packages;<br>}</pre><ul><li>Now you are all set to use your android functions in react native. But let’s first see how to implement these functions for iOS in swift so that both platforms will be covered before switching to react native code.</li></ul><h3><strong>iOS —</strong></h3><ul><li>Open your project in Xcode by navigating to your project’s <strong>ios </strong>folder and double-clicking on <strong>{PROJECT_NAME}.xcworkspace</strong> file.</li><li>Now create a new swift file (Let’s say <strong>NativeAppModule.swift</strong>) in XCode as shown below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wIeA0LaAz8t8NUy_iJ6x1Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OiIdKOAfoHAfYc_5HsPPTQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o9qnwJh62W168IFglb2QYQ.png" /></figure><ul><li>After clicking on <em>Create</em> in the above image, if there is no bridging file added to your project, then Xcode will ask to create one as shown below. Click on <strong>Create Bridging Header</strong>. It will be used to access react-native objective-c functions in a swift file.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kXBCmkukmlDOKYlwRWkU1w.png" /></figure><ul><li>Now you need to add the below line to your <strong>{PROJECT_NAME}-Bridging-Header.h</strong> file that you have created in the previous step —</li></ul><pre><strong>#import “React/RCTBridgeModule.h”</strong></pre><ul><li>Now you need to add some functions to your <strong>NativeAppModule</strong> file that you want to expose to your react native code same as we have done before for android. Comments are added to the following code to know more about their usage.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9c59c55177f2e92f74f10e23526dc11/href">https://medium.com/media/d9c59c55177f2e92f74f10e23526dc11/href</a></iframe><ul><li>If you want to listen to certain events or callbacks from the iOS side to the react-native side then you will need to create two new classes named <strong>NativeEventEmitter.swift</strong> and <strong>ReactNativeEventEmitter.swift </strong>and add the below code to them to emit data from the native code.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/876cf029d1cf78744a7fc28bb282acde/href">https://medium.com/media/876cf029d1cf78744a7fc28bb282acde/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cee10f9521334dbfdc7e8d53a7bbcbcc/href">https://medium.com/media/cee10f9521334dbfdc7e8d53a7bbcbcc/href</a></iframe><ul><li>Now you can emit events from swift to react-native using the code below —</li></ul><pre><strong>NativeEventEmitter</strong>.sharedInstance.dispatch(name: &quot;eventName&quot;, body: &quot;This is body&quot;)</pre><ul><li>Now we need to make these functions accessible to react-native. For that, firstly we need to create a new objective-c class (Let’s say <strong>NativeAppModule.m</strong>) and add the code <em>as </em>shown below<em> —</em></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67a8e5d00db8f88587a2b5f532cbeead/href">https://medium.com/media/67a8e5d00db8f88587a2b5f532cbeead/href</a></iframe><h3>React Native —</h3><p>Since you have completed the implementation by adding some functions on the native side, so now the only thing left is to test them by using them in your react native file as shown below —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70343b81ea150f088a5ad9b4034c244f/href">https://medium.com/media/70343b81ea150f088a5ad9b4034c244f/href</a></iframe><p>Thanks for reading the article. Let me know if you face any issues. You can connect with me on <a href="https://www.linkedin.com/in/kartik-mittal-18ab23106">LinkedIn</a> and <a href="https://www.facebook.com/kartik.mittal.7739">Facebook</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c05b402674b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Secure your firebase’s google-services.json file in android]]></title>
            <link>https://mittalkartik1.medium.com/secure-your-firebases-google-services-json-file-in-android-16680f8e5fb4?source=rss-daddfd52897b------2</link>
            <guid isPermaLink="false">https://medium.com/p/16680f8e5fb4</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[google-services]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[crashlytics]]></category>
            <dc:creator><![CDATA[Kartik Mittal]]></dc:creator>
            <pubDate>Sun, 11 Apr 2021 08:54:07 GMT</pubDate>
            <atom:updated>2022-02-19T07:20:59.580Z</atom:updated>
            <content:encoded><![CDATA[<h3>Secure your firebase&#39;s <strong>google-services.json</strong> file in android</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*buwRtr4cMKeoBpXLCulXtg.jpeg" /></figure><p>Nowadays firebase is commonly used in android projects as it provides a quick solution for logging crashes, providing a database for storing app’s data, and for many other things also. The most commonly used way for implementing the firebase in the android app includes downloading the <strong>google-services.json </strong>file from the firebase console and placing it in our project’s app directory. This file is responsible for auto initializing the firebase on app startup. <br>But do you know that this file contains some important keys and URLs that should not be leaked as it may tamper with the security of your app? Also, have you ever wondered how easy is it to get those keys and URLs from your app with the help of reverse engineering? <br>If you decompile your apk using some reverse engineering tools like <a href="https://ibotpeaches.github.io/Apktool/install/">Apktool</a>, then you will find that those keys are present in your app’s strings.xml file as you can see below —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*tAZ9GguVeghOGQjHNH860A.png" /></figure><p>And it can be extracted by anyone and may tamper with the security of your app.</p><p><strong>How do keys and URLs get stored in strings.xml?<br></strong>So, what happens basically is, firebase will put these keys from the <strong>google-services.json</strong> file in the strings.xml file at compile time and use these at the time of auto initializing. <br>So, is it possible to stop this so that these keys and URLs cannot be retrieved from our app which can help in reducing the chances of tampering with the app?<br>And the answer is <strong>YES.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*ZQMHL5RDGMEtp3dtetBtJQ.gif" /></figure><p><strong>How can this be done?<br></strong>The first step is to get the required keys from the <strong>google-services.json </strong>file as specified <em>below</em> and store them in your app. You can check out my previous <a href="https://mittalkartik1.medium.com/secret-management-in-android-c2a44f23fc59">article</a> to know how to securely store secret keys in android.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*qfhK8GQBFVw8IeeaHeHXng.png" /></figure><p>After storing these keys in your app, you can delete the <strong>google-services.json </strong>file from your app. Also, if you have added the below line in your app-level build.gradle file, then you have to <strong>remove</strong> this.</p><pre>apply plugin: ‘com.google.gms.google-services’</pre><p>Now you want to disable auto initialization of firebase by adding the below code to manifest file —</p><pre>&lt;provider<br>    android:name=&quot;com.google.firebase.provider.FirebaseInitProvider&quot;<br>    android:authorities=&quot;${applicationId}.firebaseinitprovider&quot;<br>    tools:node=&quot;remove&quot;<br>    /&gt;</pre><p>Now the final step is to manually initialize the firebase by placing the respective keys that you have previously gotten from the <strong>google-services.json</strong> file —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d36de0a5624f3a9944f6ee2b4ddb65f/href">https://medium.com/media/6d36de0a5624f3a9944f6ee2b4ddb65f/href</a></iframe><p>Note — If you have added firebase crashlytics in your project, then you have to add the following code in your app-level ‘<em>build.gradle</em>’ file to prevent the automatic uploading of mapping file to firebase crashlytics at release time:-</p><pre>android <strong>{<br>    </strong><em>//...<br>    </em>buildTypes <strong>{<br>        </strong>release <strong>{<br>            </strong><em>//...<br>            </em><strong><em>firebaseCrashlytics {<br>                mappingFileUploadEnabled false<br>            }</em><br>        }<br>    }<br>}</strong></pre><p>But after preventing the automatic upload of the mapping file, your info will not be preserved and you will no longer see a particular line number and the activity name where the crash occurs as you can see in the below screenshot —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pOuRVpKIyCdGGG6ycItXrQ.png" /></figure><p>But don’t worry, in order to preserve the info and get the proper crash reports, you have to add the following code to your app’s <strong>proguard-rules.pro </strong>file —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd2b1de8d7e8a8543585c5056d9d5a03/href">https://medium.com/media/cd2b1de8d7e8a8543585c5056d9d5a03/href</a></iframe><h3><strong>Update — 13/02/2022</strong></h3><p>If you are getting obfuscated crash reports in firebase crashlytics, then you might need to upload a mapping file to firebase manually after generating the apk. But since firebase has not provided any official documentation for this, we are gonna use android studio to customize the gradle task of uploading the mapping file so that this can be achieved.</p><p>But before starting, let’s see what happens in the background when you generate an apk —</p><blockquote>So, basically, when you generate an apk, then firebase crashlytics generates an <strong>UUID </strong>with the help of <strong><em>injectCrashlyticsMappingFileId</em></strong> gradle task and associate that to the app. After that <strong><em>uploadCrashlyticsMappingFile</em></strong><em> </em>gradle task is executed which uploads the mapping file to the firebase for that specific UUID. So ultimately, when a crash occurs in the specific apk, then firebase will check the mapping file belonging to the crashlytics UUID present in the apk, to deobfuscate the crash report.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*fcwLWixk10G27dEqiHOM-Q.gif" /></figure><h3>STEP — 1</h3><p>You need to set the <strong><em>mappingFileUploadEnabled </em></strong>to <strong>true</strong> again in your app-level ‘<em>build.gradle’</em> if you have set this to false before. This will ensure us that a 32 digit UUID is generated every time we generate an apk.</p><pre>android <strong>{<br>    </strong><em>//...<br>    </em>buildTypes <strong>{<br>        </strong>release <strong>{<br>            </strong><em>//...<br>            </em><strong><em>firebaseCrashlytics {<br>                mappingFileUploadEnabled true<br>            }</em><br>        }<br>    }<br>}</strong></pre><h3>STEP — 2</h3><p>You need to restrict the execution of the <strong>uploadCrashlyticsMappingFileRelease</strong> task by adding the following code at the bottom of your project-level <em>build.gradle</em> —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7fe8c9dd6dcee6abe614cddf2b38e5b/href">https://medium.com/media/f7fe8c9dd6dcee6abe614cddf2b38e5b/href</a></iframe><blockquote>Note — Task name may vary depending upon the build flavour for which you are going to generate the apk. For example — If you are having a build variant ‘uat’, then the name of the task will be uploadCrashlyticsMappingFile<strong>Uat</strong>Release.</blockquote><h3>STEP — 3</h3><p>Generate the apk for the build variant that you want. After successfully creating the apk, a 32 digit UUID and mapping file will be generated that will be used in manual uploading of the mapping file.</p><h3>STEP — 4</h3><p>Now you need to replace the code that you have added in your project-level <em>build.gradle</em> from the code as shown below. Also, note that syntax may vary depending upon the gradle version that you are going to use. I have used gradle version <strong>7.2</strong> for this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f43f90f9ccabe3267cf15b9369f5d2f9/href">https://medium.com/media/f43f90f9ccabe3267cf15b9369f5d2f9/href</a></iframe><ul><li><strong><em>mappingFileId</em></strong> — It’s a 32 digit UUID that will be obtained from the <em>com_crashlytics_build_id</em><strong><em>.</em></strong><em>xml</em> file present in the “<em>/app/build/generated/crashlytics/res/{buildVariant}/values”</em> path as you can see in the screenshot below —</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uygyhisLsWzgp-mkc0MOyw.png" /></figure><ul><li><strong><em>path_to_mapping_file</em></strong><em> —It is the absolute path of the already generated mapping file which </em>is present in<em> “/app/build/outputs/mapping/{buildVariant}/mapping.txt” path </em>as you can see in the screenshot below —</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ib91ZMhqfyam4rwqkRRZLw.png" /></figure><ul><li><strong><em>path_to_resource_root</em></strong><em> — </em>For this, you will need to create a file named ‘<em>values.xml’</em> and put that in the ‘values’ named folder, and use the absolute path of the parent directory of the values folder. For example — If path to the values.xml is “<em>C:\\Users\\HP\\Downloads\\values\\values.xml”</em> then ‘<em>path_to_resource_root’</em> will be “<em>C:\\Users\\HP\\Downloads”. </em>Content for the values.xml file is shown below —</li></ul><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;resources&gt;<br>    &lt;string name=&quot;google_app_id&quot; translatable=&quot;false&quot;&gt;           <strong><em>mobilesdk_app_id </em></strong>&lt;/string&gt;<br>&lt;/resources&gt;</pre><h3>STEP — 5</h3><p>The last step is to execute the customized task that you have specified in your project-level <em>build.gradle</em> before, using the terminal in android studio as follows —</p><pre>gradlew uploadCrashlyticsMappingFileRelease</pre><p>Wait for the task execution to get completed and you are good to go now.</p><p>Thanks for reading the article. Let me know if you face any issues. You can connect with me on <a href="https://www.linkedin.com/in/kartik-mittal-18ab23106">LinkedIn</a> and <a href="https://www.facebook.com/kartik.mittal.7739">Facebook</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16680f8e5fb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Secret management in android]]></title>
            <link>https://mittalkartik1.medium.com/secret-management-in-android-c2a44f23fc59?source=rss-daddfd52897b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2a44f23fc59</guid>
            <category><![CDATA[android-ndk]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <dc:creator><![CDATA[Kartik Mittal]]></dc:creator>
            <pubDate>Sun, 04 Apr 2021 12:03:17 GMT</pubDate>
            <atom:updated>2021-05-30T18:54:59.295Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*yvVaFOXB3iZhYfJJ0c7ajw.jpeg" /></figure><p>When it comes to the android app, then you may find that it is very easy to reverse engineer an Android app to a greater extent. There are many tools available to do so. Some of them are:-<br>1. <a href="http://ibotpeaches.github.io/Apktool/">ApkTool</a> <br>2. <a href="https://github.com/pxb1988/dex2jar">Dex2jar</a><br>3. <a href="https://java-decompiler.github.io/">JD-GUI</a></p><p>So, it becomes very important to protect your secret keys so that they cannot be misused by another person. Example: a key for Google Maps, Facebook, Crashlytics, or some other third-party library keys. Generally, when it comes to storing these keys, a developer&#39;s first choice will be placing them in <em>strings.xml </em>and accessing them all over the app. But it can be dangerous to place them in <em>strings.xml</em> as anyone can decompile your apk and easily get that file and thus access all secret keys and misuse them.</p><p>So, the question is, can we completely prevent someone to get the secret keys by reverse engineering stored in the app? So, the answer is <strong>NO.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*x7IXv8BoOCVKekqatcL0tg.gif" /></figure><p>It is not possible to protect our secret keys 100% but we can make it as harder as possible to extract the keys. So, basically, there are some approaches that can be used by you to do so: —</p><p>1. If you do not want to store your secret keys in the app then you can place them on your server and get the same at runtime. But still, some hackers can use MITM attacks or any other attacks to get them from your server.<br>2. You can encrypt the keys using some algorithms and place them somewhere else in the app other than <em>strings.xml</em> and can decrypt them at runtime when there is a need to use them. <br>For this, you can use the following approaches to place your encrypted keys:-</p><p><strong><em>a. Using app-level build.gradle file</em></strong><em> — </em>You can store the encrypted secret keys in app-level Gradle file as following<em>-</em></p><pre>android <strong>{<br>    //...<br>    </strong>buildTypes <strong>{<br>        //...<br>        </strong>release <strong>{</strong><br>            <strong>buildConfigField</strong> &quot;String&quot;, &quot;MY_SECRET_KEY&quot;, &quot;\&quot;ENCRYPTED_SECRET_KEY_DEBUG_VALUE\&quot;&quot;<br>        <strong>}<br>        </strong>debug <strong>{<br>            buildConfigField</strong> &quot;String&quot;, &quot;MY_SECRET_KEY&quot;, &quot;\&quot;ENCRYPTED_SECRET_KEY_RELEASE_VALUE\&quot;&quot;<br>        <strong>}<br>    }<br>}</strong></pre><p>After compiling the code you can access them in the code as follows -</p><pre>BuildConfig.<strong>MY_SECRET_KEY</strong></pre><p>But still generated BuildConfig file can be extracted from the apk easily and hackers can get the encrypted stored keys and can possibly decrypt them to get the original keys.</p><p>b. <strong>Using NDK (Native Development Kit) in android — </strong>It allows us to write code in C/C++ in our app which is a lot more difficult to decompile and get the stored keys from it. Although, as I previously said that it is impossible to completely prevent the hacker to steal the keys from our app but we can make it as harder as possible. So, let’s get started-</p><p><strong>STEPS-<br>1. </strong>First install the following components from SDK manager in android studio to get started with NDK.</p><ul><li><strong>The Android Native Development Kit (NDK)</strong>: a set of tools that allows you to use C and C++ code with Android.</li><li><strong>CMake</strong>: an external build tool that works alongside Gradle to build your native library. You do not need this component if you only plan to use ndk-build.</li><li><strong>LLDB</strong>: the debugger Android Studio uses to debug native code. By default, LLDB will be installed alongside Android Studio.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*EQfxYykDwlRK8HZHXqLX7Q.png" /></figure><p><em>Note</em>- By default LLDB will be installed with the android studio so, you may or may not get that option in SDK manager to install.</p><p><strong>2</strong>. Specify the CMake path and NDK version in your app-level Gradle file.</p><pre>android <strong>{<br>    //...<br>    <br>    </strong>ndkVersion &quot;23.0.7196353&quot;<strong><br>    </strong>externalNativeBuild <strong>{<br>        </strong>cmake <strong>{<br>            </strong><em>// Provides a relative path to your CMake build script.<br>            </em>path &quot;CMakeLists.txt&quot;<br>        <strong>}<br>    }<br>    //...<br>}</strong></pre><p>You can get the NDK version from the SDK manager by selecting the <strong>Show Package Details</strong> checkbox as shown in the below image —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*hyZAuTEBkXdFkIWRrAWHqw.png" /></figure><p><strong>3. </strong>Let’s start coding…</p><ul><li>Create a new project or use an existing one.</li><li>Create a folder named ‘<strong>cpp</strong>’ under src/main.</li><li>Now create a C/C++ file under the above-created cpp folder named ‘<strong>native-lib.cpp</strong>’ and write the following code to it-</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ea1cd5fbbf20e3d934463ab5a0d39e7/href">https://medium.com/media/0ea1cd5fbbf20e3d934463ab5a0d39e7/href</a></iframe><p>In the above code <em>Java_com_package_name_MainActivity_getEncryptedKey represents the package name </em><strong><em>com.package.name</em></strong><em> </em>and <strong>MainActivity</strong> is the activity in which key needs to be accessed and <strong>getEncryptedKey</strong> is the static method that is used in the specified activity to access the key. So, basically, the combination will be — <strong>{Java}_{Package Name </strong><em>(. replaced with underscores)</em><strong>}_{Activity Name}_{Method Name}</strong></p><ul><li>Now create a CMake script named <strong>CMakeLists.txt </strong>under the <em>app</em> folder and the following content to it —</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6000c5210579895d07516c73536a92c6/href">https://medium.com/media/6000c5210579895d07516c73536a92c6/href</a></iframe><ul><li>Now to access the encrypted key in your activity you have to add a static block and make a function as follows-</li></ul><p>If you are using java then —</p><pre>static {<br>    System.<em>loadLibrary</em>(“native-lib”);<br>}</pre><pre>public native String getEncryptedKey();</pre><p>If you are using kotlin then —</p><pre>companion object {<br>    init {<br>        System.<em>loadLibrary</em>(“native-lib”)<br>    }<br>}</pre><pre>external fun getEncryptedKey(): String</pre><p>Now you can call this function and voila, you will get the encrypted key, and then you can decrypt that using the same algorithm that you have used to encrypt it.<br>Thanks for reading the article. You can connect with me on <a href="https://www.linkedin.com/in/kartik-mittal-18ab23106">LinkedIn</a> and <a href="https://www.facebook.com/kartik.mittal.7739">Facebook</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2a44f23fc59" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>