」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 使用 Web 工具進行 Android 開發:使用 Ionic React 進行生產的最快方式

使用 Web 工具進行 Android 開發:使用 Ionic React 進行生產的最快方式

發佈於2024-11-08
瀏覽:167

Investing in Android development can yield a huge device market share, expanded market reach, and high return on investment.

With over 6.8 billion smartphone users worldwide. Android holds approximately 70% of the global market share, translating to about 4.76 billion users, finding your niche is within reach. It's all about producing high-quality, fast applications.

Ionic, with native web components, enables you to achieve performant and high-quality Android apps using familiar tools like HTML, JavaScript, and CSS, while tapping into native functionalities with Capacitor.

This is more than just a tutorial on Ionic; it's about building quality and performant Android applications ready for production.

This article is an introduction to a series where we'll cover the basics of Ionic with React as our frontend and later explore the bridge between native and web technologies using Capacitor.

Ionic framework: An Introduction

Runtimes or bridges between different technologies are nothing new!

Take Node.js, for example. Only through Node can JavaScript become a systems language.

   [JS]
   [byte code]
   [Node] --> [N API] --> [C/C   modules]
   [bindings]
   [Cpp][V8][libuv]
   [OS]

Consider hybrid desktop applications using HTML, JavaScript, and CSS as a view via the webview. Go Wails, a very performant desktop development framework, is based on this idea. Similarly, Rust Tauri apps operate on this principle.

Bindings have existed and been tested for a while in the mobile world, with examples like React Native and NativeScript.

The development world is realizing the importance of not just UI but beautiful and responsive UI. There are no frameworks as advanced as web technologies in this field.

Android native development is shifting towards the direction of React with composable UIs in Kotlin, moving away from the less favored XML.

This trend brings the best of both worlds: native speed with beautiful composable UI. That's where Ionic stands out among its peers. The difference is that Ionic is easy to grasp—I successfully built a client's application in under a month.

Setup

Create a new project folder and run the following:

npx ionic start

This will take you through the Ionic setup. Choose React as the frontend framework for this article.

Ionic still uses Webpack and Create React App (CRA) because Vite does not yet support Stencil.js, the core of Ionic web components.

Once everything is installed, open the project in VSCode. I prefer to remove npm and use pnpm (this step is optional). If you want to do the same:

  • Delete the node_modules folder.

  • Delete the package-lock.json file, not package.json.

  • Run pnpm install.

To run an Ionic application, use:

npx ionic serve

The Ionic CLI will take care of everything. You can also use the --lab option for a phone-like preview (note, this is not an Android or iOS emulator, but a "view"):

pnpm add -D @ionic/lab

npx ionic serve --lab

Android Dev with web Tools: fastest way to production with Ionic React

This allows us to preview how the UI will look on a phone-like view.

Going Over the structure

I assume you have the project open in an IDE of your choice. If you do not have React experience, this may be a bit challenging. I suggest taking a basic React tutorial and learning about React routers.

The entry point is a standard React application render in index.tsx:

root.render(      



  );



In App.tsx, it's a router and tab navigation bar, using Ionic router and components. Ionic components are native web components built with Stencil.js, designed to look like a mobile application.

Ionic provides CSS files and themes to match the standards of both iOS and Android. Use Ionic components over HTML for a natural mobile application look and feel.

Let's break down App.tsx, starting with the router. It works similarly to the React web router, matching a path to a component and rendering the matching component on navigation.



import Tab1 from './pages/Tab1';

import Tab2 from './pages/Tab2';

import Tab3 from './pages/Tab3';



    

If you're familiar with backend, the path is like an endpoint, and the component is a handler.

   Tab 1Tab 2Tab 3

The IonTabBar creates a tab bar at the provided slot, in our application its bottom. The magic is in the tab button: triggers the router using hrefs. All normal React code, wrapped in Ionic components.

Follow one of the tab pages; they are essentially just pages.

   Tab 1Tab 1

Using the Ionic page component handles things like scrolling and responsiveness out of the box.

The standard structure of an Ionic page includes a header with a toolbar and a content area, similar to most mobile applications.

Header:

  Tab 1

Content area:



    Tab 1

The content area occupies most of the screen, where most of the application lives. The ExploreContainer acts as a slot; we can conditionally render components based on the name prop.

When name is "Tab 1," we render a component for that tab. You can hard code components for each tab, but the slot method is more flexible and composable.

For example, open the ExploreContainer component under the components folder and create three new components:

const Tab1Content = () => { return ( "I am tab 1 content" ); }

const Tab2Content = () => { return ( "I am tab 2 content" ); }

const Tab3Content = () => { return ( "I am tab 3 content" ); }

Now update the container to conditionally render based on the name prop:

{name.includes("Tab 1") ? : name.includes("Tab 2") ? : }

This is just an example; you can create an easy-to-follow pattern matching method. The updated preview should show "I am tab x content" based on the tab clicked.

This application is still web-based. We haven't installed or initialized Capacitor, which is responsible for turning our application into a native app.

Android Dev with web Tools: fastest way to production with Ionic React

Capacitor is a cross-platform native runtime for web apps, allowing us to create cross-platform iOS, Android, and Progressive Web Apps with JavaScript, HTML, and CSS.

Enabling Capacitor in Ionic

First, install Capacitor:

pnpm add @capacitor/core
pnpm add -D @capacitor/cli

Next, initialize the Capacitor configuration file:

npx cap init

The package ID should uniquely identify your app. We use an inverted domain name, e.g., com.example.app.

Capacitor is initialized. Run the following commands to install a Capacitor platform and primitive plugins:

pnpm add @capacitor/android



pnpm add @capacitor/app @capacitor/haptics @capacitor/keyboard @capacitor/status-bar


The following command will create the native android project structure and files in your ionic project:

npx cap add android

Important: Build the web app:

pnpm run build

to avoid this error before we run sync

[error] Could not find the web assets directory: .\build.

... More info: https://capacitorjs.com/docs/basics/workflow#sync-your-project

Once the build is finished, you can sync, copying the built web app; into the native webview:

npx cap sync

Believe it or not, we are ready to either build or preview the native application in an emulator.

We'll dive deeper into Capacitor and native development, environment setup, etc., in the next article.

Since we are still getting a feel for Ionic, let's play with a few Ionic components and wrap up with a simple application example.

PokeApp Example

You can easily find Ionic components in the documentation.

We'll implement a simple app that fetches Pokémon data from the PokeAPI, for a compact card view, and then build it into an APK.

Android Dev with web Tools: fastest way to production with Ionic React

From the results, we can already see how decent the app looks with no styling—thanks to the power of Ionic components.

Open the ExploreContainer component, and we'll work on Tab 2.

Update the component and add the following:

const BASE_LINK = "https://pokeapi.co/api/v2/pokemon/"



const Tab2Content = () => { 



 const [pokemon, setPokemon] = useState("pikachu")



 useEffect(()=> {    

if(pokemon != ""){    

  fetch(BASE_LINK   pokemon).then(async(poke)=> {

       console.log(await poke.json())              

    }).catch((err)=>console.log(err))   

 } 



 }, [pokemon])  



// add some padding to the div below

return  (

  
I am tab 2 content
)}

We've added a state to track the Pokémon we want to look up, with the default being Pikachu:

const [pokemon, setPokemon] = useState("pikachu")

On load, we fetch the Pokémon data from the PokeAPI:

 useEffect(()=> {    

if(pokemon != ""){    

  fetch(BASE_LINK   pokemon).then(async(poke)=> {

       console.log(await poke.json())              

    }).catch((err)=>console.log(err))    } 

 }, [pokemon])  



Android Dev with web Tools: fastest way to production with Ionic React

The useEffect hook runs twice in React strict mode.

Instead of logging our result, let's turn it into a state so we can use it in our card component.

First, add a new useState under the Pokémon one:

 const [showResults, setResults] = useState()

Then, update the useEffect to set the results::



 useEffect(()=> {
    if(pokemon != ""){
      fetch(BASE_LINK   pokemon).then(async(poke)=> {


       const results = await poke.json()
       const {front_default} = results.sprites
       setResults({front_default})


      }).catch((err)=> console.log(err))
    }
  }, [pokemon])

The PokeAPI returns a lot of data. We are interested in the Pokémon image, specifically the front-facing image in the sprites object:

       const results = await poke.json()
       const {front_default} = results.sprites
       setResults({front_default})

If you are familiar with React, you know we have created the re-render on state change loop already. Now, we just need to consume the data:



 return  (
  
{pokemon}使用 Web 工具進行 Android 開發:使用 Ionic React 進行生產的最快方式
)

We use an Ion card component to show the retrieved image:



   使用 Web 工具進行 Android 開發:使用 Ionic React 進行生產的最快方式

We have a basic structure already, but we can only show the default Pokémon. We need a way to accept user input (a Pokémon name) and make a fetch request based on that input.

The basic React approach is to have an input element bound to a useState value, updating it on onChange. However, in our case, this is problematic because every keystroke will trigger our useEffect, making multiple erroneous requests to the PokeAPI.

Instead, we need the user to type fully and press a search button to initiate the API call. Copy the code below and paste it on top of the Ion card:



  PokeSearch() }>search

From the code above, we need two things: a useRef pointing to our Ion input and a PokeSearch function triggered by an Ion button.

const Tab2Content = () => {
  const [pokemon, setPokemon] = useState("pikachu")
  const [showResults, setResults] = useState()
  const pokeNameref = useRef(null)

  const PokeSearch = () => {

    if(pokeNameref.current){

      console.log(pokeNameref.current.value)
         setPokemon(pokeNameref.current.value.toLocaleLowerCase())
    }
  }



....



}

The code below is responsible for updating the state, triggering the effect

 if(pokeNameref.current){

      console.log(pokeNameref.current.value)
         setPokemon(pokeNameref.current.value.toLocaleLowerCase())
    }



The entire component:

const Tab2Content = () => {
  const [pokemon, setPokemon] = useState("pikachu")
  const [showResults, setResults] = useState()
  const pokeNameref = useRef(null)

  const PokeSearch = () => {

    if(pokeNameref.current){

      console.log(pokeNameref.current.value)
      setPokemon(pokeNameref.current.value.toLocaleLowerCase())
    }
  }

  useEffect(()=> {
    if(pokemon != ""){
      fetch(BASE_LINK   pokemon).then(async(poke)=> {
       const results = await poke.json()
       console.log(results.sprites)
       const {front_default} = results.sprites
       setResults({front_default})
      }).catch((err)=> console.log(err))
    }
  }, [pokemon])

  return  (
  
PokeSearch() }>search{pokemon}使用 Web 工具進行 Android 開發:使用 Ionic React 進行生產的最快方式
) }

Our simple PokeApp is complete. Make sure ionic serve --lab is running and type a few Pokémon names, such as:

bulbasaur dragonite

If everything is set up correctly, the Pokémon should change on search.

Not a life-changing application, but enough for learning Ionic. The next step requires Android Studio . Download it and follow the defaults while installing it.

PokeApp to APK

If you have never seen Android Studio, it’s probably the most complex IDE and has a steep learning curve!

I suggest following the defaults on installation and letting it run its course. My only suggestion is to select the option to install an emulator, which makes it easier to build and review the APK before bundling it.

When you download Android Studio for the first time, it'll download a lot of dependencies and set up Gradle, which may take some time. Let it do its thing. Gradle is a build tool for Android, similar to how we use Webpack or Vite in web development.

When you are ready and Android Studio is installed, navigate to our PokeApp in the terminal.

As an extra precaution, build and sync before opening the project in Android Studio to ensure there are no errors:

pnpm run build

npx cap sync

If the build is successful, we can rest assured there are no errors in our application. Next, open the project in Android Studio:

npx cap open android

Let the Gradle processes run:

Android Dev with web Tools: fastest way to production with Ionic React

When Gradle is done, try running the app in an emulator (top middle) in the IDE. If the app runs on the emulator, you can be sure it'll bundle to a standalone APK:

Android Dev with web Tools: fastest way to production with Ionic React

Check this extensive link for more ways to debug and run your APK: android studio run

Notes on Building the APK

There are a few steps involved in building an actual production APK for the Google Play Store, from setting up an Android console to creating banner images, which are tedious but essential tasks.

Note: The Android development account is a one-time fee. You can buy and set it up on Google Console.

Design, search keywords, and banners are beyond coding. This series is about getting the coding part right! I promise everything else will fall into place with practice and getting used to the tediousness of the Google Play Console.

In short, I will skip the Google Play Console for a few reasons:

  • It takes a while (2 weeks minimum) to get approved.
    When approved, the APK goes through a vetting process (takes time, may fail).

  • You can't submit an APK on Google Console unless you have banners and icons.

  • There is a lot of editing and icon generation for different screens.

These reasons make it impractical to include in a tutorial. But rest assured, what I will show you in this and upcoming articles will prepare you to build production-ready applications to publish in any store besides Google or for self-hosting.

However, if you already have a Google Play account, there are many articles and videos on publishing an Ionic Android app.

For our case, as long as we can generate a debug APK file and install it on an emulator or real phone, the other steps are just a Google search away!

Because this process is tedious, I will dedicate a separate article in this series to go through Android Studio, sign an APK, and build a release. For now, a debug APK will suffice as this article is already long.

Generating a debug apk

Look at your Android Studio top bar left; after the Android icon, there should be a hamburger menu button. Select to expand the menu. The build option is hidden there:

Android Dev with web Tools: fastest way to production with Ionic React

If the APK is generated successfully, a popup should show at the bottom right with a locate option, which will open the explorer to the APK path. You can share or install it on an Android device!

If you want to create a signed APK, the full production deal, Google has an extensive documentation

This was a high-level overview. We will go deeper with each article in the series.

In this article, we introduced Android development using web tools, and our framework of choice was Ionic. We covered the basics of Ionic and Ionic components, how to set up the native runtime bridge Capacitor, and built a debug APK.

If you are ready to dive deep into Capacitor, you can find the next article here: Capacitor JS: The Bridge Between Web Tech & Native—Android, IOS, PWA

This is just the start.

If you are interested in more longer, exclusive, practical content, I have tiers and posts designed to elevate your programming skills at the ko-fi platform.

版本聲明 本文轉載於:https://dev.to/sfundomhlungu/android-dev-with-web-tools-fastest-way-to-production-with-ionic-react-5ag4?1如有侵犯,請聯絡study_golang@163 .com刪除
最新教學 更多>
  • 了解命令式程式設計和聲明式程式設計之間的區別
    了解命令式程式設計和聲明式程式設計之間的區別
    當我剛開始學習React時,我的老師說:「JavaScript是命令式編程,而React是聲明式編程。」然而,一開始這對我來說不太有意義。因此,我決定將其分解以更好地理解其中的差異。 將命令式和聲明式程式設計與披薩進行比較? 為了更容易理解,讓我們來比較一下這兩種烹飪方法。 ...
    程式設計 發佈於2024-11-08
  • 如何使用 JPA 和 Hibernate 以 UTC 格式儲存日期/時間和時間戳記?
    如何使用 JPA 和 Hibernate 以 UTC 格式儲存日期/時間和時間戳記?
    使用JPA 和Hibernate 以UTC 格式儲存日期/時間和時間戳在Java Persistence API (JPA) 和Hibernate 中,管理日期/時間不同時區的時間戳值可能是一個挑戰。為了確保 UTC(協調世界時)時間的一致儲存和檢索,正確配置框架至關重要。 考慮提供的附註解的 JP...
    程式設計 發佈於2024-11-08
  • java.lang.RuntimeException 和 java.lang.Exception 之間的主要區別是什麼?
    java.lang.RuntimeException 和 java.lang.Exception 之間的主要區別是什麼?
    揭示java.lang.RuntimeException 和java.lang.Exception 的獨特本質在Java 異常領域內,兩個經常遇到的異常類別出現:java.lang.RuntimeException 和java.lang.Exception。為了有效地理解異常處理的複雜性,剖析它們之...
    程式設計 發佈於2024-11-08
  • 為什麼嵌入框陰影在透明背景的圖片上消失?
    為什麼嵌入框陰影在透明背景的圖片上消失?
    了解圖像上的插入框陰影問題在網頁設計中,使用插入框陰影在元素內創建深度和尺寸是一種常見技術。然而,在處理包含圖像的容器時,事情並不總是那麼簡單。當嵌入框陰影似乎在嵌入影像上消失時,就會出現問題。 隱形陰影的情況考慮原始問題中提供的範例:body { background-color: #0000...
    程式設計 發佈於2024-11-08
  • 如何在 ReactJS 中維護懸停狀態:解決事件註冊問題
    如何在 ReactJS 中維護懸停狀態:解決事件註冊問題
    在ReactJS 中維護懸停狀態:解決事件註冊問題使用內聯樣式時,您會遇到ReactJS 中懸停和活動事件的問題,因為onMouseEnter 和onMouseLeave 方法被證明是不可靠的。 要解決此問題,請考慮使用下列事件處理程序之一:onMouseDownonMouseEnter onMou...
    程式設計 發佈於2024-11-08
  • 如何在 JavaScript 中準確檢查 Null 值和空字串?
    如何在 JavaScript 中準確檢查 Null 值和空字串?
    檢查 JavaScript 中的 Null 值在 JavaScript 中,確定值是否為 null 有時會令人困惑。為了提供更深入的理解,本文將深入研究在 JavaScript 情境中偵測空值的細節。 檢查空值提供的程式碼片段旨在檢查跨多個變數的null 值:if (pass == null || ...
    程式設計 發佈於2024-11-08
  • 在 Golang 中建立 Google Drive 下載器(第 1 部分)
    在 Golang 中建立 Google Drive 下載器(第 1 部分)
    介绍 在本教程中,我们将构建一个功能强大的下载器,允许从Google Drive和其他云提供商下载文件。借助 Golang 高效的并发模式,您将能够同时管理多个下载、流式传输大文件并实时跟踪进度。无论您是下载一些小文件还是处理大型数据集,该项目都将展示如何构建可扩展且强大的下载器,...
    程式設計 發佈於2024-11-08
  • PHP 4 快速部署
    PHP 4 快速部署
    Servbay 已成為高效配置開發環境的領先工具。在本指南中,我們將引導您完成快速、安全地部署 PHP 8.1 的過程,以展示 Servbay 對簡化部署的承諾。 先決條件 確保您的電腦上安裝了 Servbay。您可以從 Servbay 官方網站輕鬆下載。安裝過程人性化;只需按照安...
    程式設計 發佈於2024-11-08
  • 如何繞過驗證碼
    如何繞過驗證碼
    No matter how many times people wrote that the captcha has outlived itself long time ago and no longer works as effectively as its developers would ha...
    程式設計 發佈於2024-11-08
  • 使用 super 呼叫超類別建構函數
    使用 super 呼叫超類別建構函數
    子類別可以使用 super(parameter-list);. 形式來呼叫其超類別定義的建構函數 parameter-list 必須指定超類別建構子所需的參數。 子類別建構子中執行的第一條語句必須始終是 super(); (或 super(parameter-list); 如果需要傳遞參數). ...
    程式設計 發佈於2024-11-08
  • 你能比較 C++ 中不同容器的迭代器嗎?
    你能比較 C++ 中不同容器的迭代器嗎?
    比較來自不同容器的迭代器:一個警示故事在C 中,迭代器提供了一個強大的遍歷集合的機制。然而,在使用來自不同容器的迭代器時,重要的是要意識到這些限制。 比較不同容器的迭代器是否合法的問題經常出現。考慮以下範例:std::vector<int> foo; std::vector<int...
    程式設計 發佈於2024-11-08
  • 幫助 FastAPI:如何為文件翻譯做出貢獻
    幫助 FastAPI:如何為文件翻譯做出貢獻
    One of the great features of FastAPI is its great documentation ?. But wouldn't it be better if more people around the world had access to this docume...
    程式設計 發佈於2024-11-08
  • 如何使用 CSS 和 AngularJS 建立垂直 HTML 表格?
    如何使用 CSS 和 AngularJS 建立垂直 HTML 表格?
    垂直HTML 表格創建具有垂直行的HTML 表格提供了一種獨特的方式來顯示數據,行標題位於左側而不是頂部。要實現此目的,可以套用 CSS 樣式來轉換表格的結構。 CSS 樣式若要將表格行呈現為垂直列,請遵循下列CSS 規則可使用:tr { display: block; float: lef...
    程式設計 發佈於2024-11-08
  • 透過自訂 Hooks 在 React 中重複使用邏輯:實用指南
    透過自訂 Hooks 在 React 中重複使用邏輯:實用指南
    自訂鉤子是React 中的一項強大功能,與React 內建鉤子不同,它用於更具體的目的,並且它是透過將常見功能封裝到獨立函數中來完成的。自訂掛鉤促進可重複使用性、改進元件組織並整體增強程式碼可維護性。 在本指南中,我們將深入探討使用自訂鉤子的目的,以了解創建自訂鉤子的基礎知識以及如何使用其他元件。...
    程式設計 發佈於2024-11-08
  • 使用 ReactJS 建立免費的 AI 圖像生成器
    使用 ReactJS 建立免費的 AI 圖像生成器
    开发者们大家好, 今天,我将向您展示如何使用 ReactJS 创建图像生成器,并且完全可以免费使用,这要感谢黑森林实验室和 Together AI。 第 1 步:设置项目 在本教程中,我们将使用 Vite 来初始化应用程序并使用 Shadcn 来初始化 UI。我假设您已经设置了项目并...
    程式設計 發佈於2024-11-08

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3