Introduction to HarmonyOS Next Application UI Generation Tool
Background
During the adaptation and development of HarmonyOS Next, it is difficult to refer to previous logic, and the documentation may be incomplete or take a long time to access. Therefore, developers often have to refer to Android or iOS code. For some scenarios with complex logic, AI tools can be used to convert Android Java code logic into TypeScript (TS) for partial reuse. For UI scenarios, manual coding is required. Although ArkUI improves page development efficiency, the workload remains substantial for numerous pages. The official has provided a tool to convert Android APK to ArkTS pages, and this article guides you through the experience.
Tool Introduction
The UI Generator is designed to quickly generate compilable and runnable HarmonyOS UI projects. It supports converting existing UI layout files (XML) into corresponding HarmonyOS UI code, including basic HarmonyOS projects, page layouts, components and properties, as well as resource files.
Requirement: Use DevEco Studio 5.0.3.700 or a higher version.
Tool Configuration
In DevEco Studio, open the settings page, click on "Plugins", find the installed UI Generator plugin, and enable it:
After enabling and confirming, click "Generate Project from ..." in the toolbar:
Select the path of the Android APK package and the Android SDK path:
Set the "Destination Path" as the path and name for the generated HarmonyOS Next public beta project. Click "Confirm" to start the conversion. The conversion log is as follows:
2025-01-22 12:08:02,885 In /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug
2025-01-22 12:08:02,960 sdkdir: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk
2025-01-22 12:08:02,960 apkpath: /Users/shen/Downloads/app-debug.apk
2025-01-22 12:08:02,960 androidsdk: /Users/shen/Library/Android/sdk
2025-01-22 12:08:02,960 aapt: /Users/shen/Library/Android/sdk/build-tools/34.0.0/aapt2
2025-01-22 12:08:02,962 bash dumpres.sh /Users/shen/Downloads/app-debug.apk /Users/shen/Library/Android/sdk/build-tools/34.0.0/aapt2
2025-01-22 12:08:05,916 run dump apk resources finished.
2025-01-22 12:08:05,917 =================================================
2025-01-22 12:08:05,917 /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/maple-decompile.sh exec success
2025-01-22 12:08:05,917 =================================================
2025-01-22 12:08:05,920 In /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/runs
2025-01-22 12:08:05,939 dexdir: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/unzip
2025-01-22 12:08:05,939 androiddir: /Users/shen/Library/Android/sdk
2025-01-22 12:08:05,939 componentspath: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/runs/components.txt
2025-01-22 12:08:05,950 bash /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/bin/component_analyze.sh --dexdump-dir=/Users/shen/Library/Android/sdk/build-tools/34.0.0/dexdump --dex-dir=/Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/unzip --mplsdk-dir=/Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk --component-include=false /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/runs/components.txt
2025-01-22 12:08:05,965 dexdump-dir: /Users/shen/Library/Android/sdk/build-tools/34.0.0/dexdump
2025-01-22 12:08:05,965 dex-dir: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/unzip
2025-01-22 12:08:05,966 analysis start!
2025-01-22 12:08:05,967 Darwin
2025-01-22 12:08:05,967 xml analysis start
2025-01-22 12:08:05,975 =================================================
2025-01-22 12:08:05,975 /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/runs/maple-files-dex.sh exec success
2025-01-22 12:08:05,975 =================================================
2025-01-22 12:08:44,098 In /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk
2025-01-22 12:08:44,105 Darwin
2025-01-22 12:08:44,129 target is xml file, no need to analyze dex dependency, exit dex-crop.sh
2025-01-22 12:08:44,130 =================================================
2025-01-22 12:08:44,130 /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/bin/dex_crop.sh exec success
2025-01-22 12:08:44,130 =================================================
2025-01-22 12:08:44,143 In /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/components
2025-01-22 12:08:44,173 cp: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/maple_deps/host_maple/bin_darwin/dex2mpl is a directory (not copied).
2025-01-22 12:08:44,180 cp: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/autodeps/bin_darwin/dex2deps is a directory (not copied).
2025-01-22 12:08:44,181 cp: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/autodeps/bin_darwin/gendeps is a directory (not copied).
2025-01-22 12:08:44,183 chmod: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/autodeps/bin/*: No such file or directory
2025-01-22 12:08:44,185 maplebinary: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/maple_deps/host_maple/bin/maple
2025-01-22 12:08:44,185 xmldir: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/decompile
2025-01-22 12:08:44,185 component: layout/activity_preview.xml
2025-01-22 12:08:44,185 sdkdir: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk
2025-01-22 12:08:44,185 apkname: app-debug
2025-01-22 12:08:44,185 sourcedir:
2025-01-22 12:08:44,185 mplfile: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/components/test.mpl
2025-01-22 12:08:44,185 dexdump:
2025-01-22 12:08:44,187 /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/maple_deps/host_maple/bin/maple --infile /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/components/test.mpl --me-opt="--O2 --quiet --no-nativeopt --meverify --threads=4 --no-ignoreipa --enable-ea" --mpl2mpl-opt="--O2 --quiet --regnativefunc --deferred-visit --no-nativeopt --maplelinker --emitVtableImpl --maplelinker-nolocal --sva --android-xml=/Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/decompile --xml-to-analyze=layout/activity_preview.xml --class-to-analyze=activity_preview --config-path=/Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/config_files" --save-temps
2025-01-22 12:08:46,235 Sighandler : signal handler isn't implemented for non-unix os
2025-01-22 12:08:46,235 Sighandler : signal handler isn't implemented for non-unix os
2025-01-22 12:08:46,235 Sighandler : signal handler isn't implemented for non-unix os
2025-01-22 12:08:46,235 Sighandler : signal handler isn't implemented for non-unix os
2025-01-22 12:08:46,235 Sighandler : signal handler isn't implemented for non-unix os
2025-01-22 12:08:46,235 Sighandler : signal handler isn't implemented for non-unix os
2025-01-22 12:08:46,235 Sighandler : signal handler isn't implemented for non-unix os
2025-01-22 12:08:46,763 android activities cnt : 10
2025-01-22 12:08:46,763 => Log File: android-ui-ir-activity_preview.log
2025-01-22 12:08:46,763 ==========> Save UI Generate Report to activity_preview_generation_report.json <==========
2025-01-22 12:08:46,769 =================================================
2025-01-22 12:08:46,769 /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/components/maple.sh exec success
2025-01-22 12:08:46,769 =================================================
2025-01-22 12:08:46,784 In /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/components
2025-01-22 12:08:46,787 Darwin
2025-01-22 12:08:46,793 dexdump-dir: /Users/shen/Library/Android/sdk
2025-01-22 12:08:46,793 dex-dir: /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/build/app-debug/unzip
2025-01-22 12:08:46,793 cropping analysis start!
2025-01-22 12:08:46,793 searching for all dexdump
2025-01-22 12:08:48,812 searching for all dexdump
2025-01-22 12:08:48,812 /Users/shen/Library/Android/sdk/build-tools/30.0.2/dexdump
2025-01-22 12:08:48,812 searching for all dex file
2025-01-22 12:08:48,815 1 dexdump location founded.
2025-01-22 12:08:49,409 =================================================
2025-01-22 12:08:49,409 /Users/shen/Library/Caches/Huawei/DevEcoStudio5.0/ui-generation/maple-sdk/bin/get_base_class.sh exec success
2025-01-22 12:08:49,409 =================================================
2025-01-22 12:08:49,458 Generation IR Time Elapsed: 5.368000 s
2025-01-22 12:09:06,886 Generation Success, Generated Project Location: 2025-01-22 12:09:06,886 /Users/shen/UIGenerationProjects/UIGeneration_app-debug_activity_preview
2025-01-22 12:09:06,886 Generation Project Time Elapsed: 0.129000 s
- After opening the new project, the generated pages are located in the
entry > src > main > ets > pages
directory. You can preview the pages in the Previewer. Theentry > src > main > resources
directory of the new project contains text, image, and color resources.
Example:
Android XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:id="@+id/container"
android:layout_width="213dp"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
Generated ArkTS Code:
@Entry
@Component
export struct activity_preview {
build() {
/*
* FIXME: Unhandled property 'android:orientation'
*/ Scroll() {
Column()
.id('container')
.width('213vp')
.alignItems(HorizontalAlign.Start)
}
.align(Alignment.TopStart)
.width('100%')
.height('100%')
}
}
Components and properties that cannot be generated will be provided in the form of comments to facilitate subsequent location and modification.
Summary
Although some custom components or properties cannot be directly converted, for complex pages, this tool can help us generate the overall framework and most of the code, which can greatly improve our development efficiency. We recommend everyone to give it a try.
Top comments (0)