Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Getting Started with ArkTS: Project Layout and First Steps

Tech Oct 23 1

Project anatomy

A ArkTS application is organized as a multi-level tree of configuration and source files:

  • AppScope/app.json5 – global settings for the whole application.
  • entry/ – the primary module; when built it becomes an installable HAP package.
  • entry/src/main/ets/ – ArkTS source code.
  • entry/src/main/ets/entryability/ – the UIAbility that starts the app.
  • entry/src/main/ets/entrybackupability/ – optional extension for backup & restore.
  • entry/src/main/ets/pages/ – individual UI pages.
  • entry/src/main/resoucres/ – images, strings, layouts, media, etc.
  • entry/src/main/module.json5 – module-level manifest (device capabilities, permissions, etc.).
  • entry/build-profile.json5 – compiler options and target definitions for the module.
  • entry/hvigorfile.ts – modulee build script.
  • entry/obfuscation-rules.txt – ProGuard-style obfuscation rules.
  • entry/oh-package.json5 – package metadata (name, version, entry point, dependencies).
  • oh_modules/ – third-party dependencies.
  • build-profile.json5 (root) – project-wide build settings.
  • hvigorfile.ts (root) – project build script.
  • oh-package.json5 (root) – global configuration.

Writing your first screen

@Entry
@Component
struct WelcomePage {
  @State greeting: string = 'Hello World';

  build() {
    Stack() {
      Text(this.greeting)
        .id('welcomeText')
        .fontSize(48)
        .fontWeight(FontWeight.Medium)
        .align(Alignment.Center)
    }
    .width('100%')
    .height('100%')
  }
}

Adding navigation

// WelcomePage.ets
import { router } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WelcomePage {
  @State greeting: string = 'Hello World';

  build() {
    Column({ space: 24 }) {
      Text(this.greeting)
        .fontSize(48)
        .fontWeight(FontWeight.Medium)

      Button('Next')
        .type(ButtonType.Capsule)
        .fontSize(28)
        .fontColor('#FFFFFF')
        .backgroundColor('#007AFF')
        .width('50%')
        .onClick(() => {
          router.pushUrl({ url: 'pages/Detail' })
            .then(() => console.info('Navigated to Detail'))
            .catch((err: BusinessError) =>
              console.error(`Navigation failed: ${err.code} ${err.message}`)
            );
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

Creating a second page

Add the new page to the route table in main_pages.json:

{
  "src": [
    "pages/WelcomePage",
    "pages/Detail"
  ]
}
// Detail.ets
import { router } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Detail {
  @State subtitle: string = 'Welcome to the second page';

  build() {
    Column({ space: 24 }) {
      Text(this.subtitle)
        .fontSize(36)
        .fontWeight(FontWeight.Regular)

      Button('Back')
        .type(ButtonType.Capsule)
        .fontSize(24)
        .backgroundColor('#007AFF')
        .width('50%')
        .onClick(() => {
          try {
            router.back();
            console.info('Returned to WelcomePage');
          } catch (err) {
            const error = err as BusinessError;
            console.error(`Back navigation failed: ${error.code} ${error.message}`);
          }
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.