辛宝Otto

辛宝Otto 的玄酒清谈

北漂前端程序员儿 / 探索新事物 / Web Worker 主播之一/内向话痨
xiaoyuzhou
email

Forward-looking - How to Debug UTS Code on an Emulator

Forward - How to Debug UTS Code on the Simulator

Recently studied cross-end, recording some usage scenarios, first record, further organize later.

The front end and client collaborate to complete the development of functions. For real machine testing, it is still necessary to test on both the simulator and the real machine.

For example, when we implement Event, the actual return value of the event may differ on different devices, and it can only be confirmed by running it on a real machine.

The following tasks need to be completed:

  • Switch the x-ios project to dev
  • Switch uni-app to dev
  • Ensure both folders are in the same directory
  • Compile and run x-ios on Xcode to successfully run a project
  • Find the resource files of the application in this simulator, see below

Find the Resource Files of the Application in This Simulator#

Find the Device ID of the Current Simulator#

Select xcode - window - Devices and Simulators - Your Device

Pasted image 20240126171917

Find Identifier

image.png

For example, my simulator's ID is
79FEB78D-E6F7-44E3-9741-44F6DAD53BC0

Enter the Simulator Resource Manager#

拼一下地址

Use Finder to open cmd+shift +g and enter the address below, the string at the end is your ID

open ~/Library/Developer/CoreSimulator/Devices/Copy your device ID

# open ~/Library/Developer/CoreSimulator/Devices/79FEB78D-E6F7-44E3-9741-44F6DAD53BC0

Generally looks like this

image.png

Use cmd+shift+g or command to enter hidden directories:

# Relative directory
cd data/Containers/Data/Application

# Absolute directory, for reference only
cd /Users/otto/Library/Developer/CoreSimulator/Devices/79FEB78D-E6F7-44E3-9741-44F6DAD53BC0/data/Containers/Data/Application

This is all installed apps, only one of them is the app, you can sort by the latest modification time or directly search for the uni-app-x folder, usually the last one or the second to last.

Pasted image 20240126172311

Find uni-app-x/apps, for reference only, the actual will change. It is recommended to save a Finder bookmark for quick access later.

Pasted image 20240126172452

cd /Users/otto/Library/Developer/CoreSimulator/Devices/79FEB78D-E6F7-44E3-9741-44F6DAD53BC0/data/Containers/Data/Application/2222EA54-73C7-4F89-ABCA-E1D35D80CFD7/Documents/uni-app-x

Generally contains the following content:

image.png

Supplement: This -dev.js also needs to be brought over.

The www directory here is the actual resources that need to be run, and you can replace the resources of hx.

Next, I will introduce how to replace.

hx's compilation output

Pasted image 20240126172711

Quick Code#

During subsequent debugging, it is necessary to frequently move the build dist folder of the IDE into the package of the simulator app. Here are a few shortcut commands, using soft links to achieve directory and file mapping.

This places the compilation output of buiderx onto the phone for debugging, and you can usually use the simulator for this.

Pasted image 20240126173433

Note that the simulator here has an additional native-app-service.js, which we will look into later.

Related

  • [What is uni-app-x-framework-dev.js?]
  • What is app-service?

Below is the logic for replacing files

#!/bin/bash

source_dir="/Users/otto/mycode/dcloud/basic-hx-vue2/basic-uts-0117/unpackage/dist/dev/app"

target_dir="/Users/otto/Library/Developer/CoreSimulator/Devices/79FEB78D-E6F7-44E3-9741-44F6DAD53BC0/data/Containers/Data/Application/2222EA54-73C7-4F89-ABCA-E1D35D80CFD7/Documents/uni-app-x"

www_dir="$target_dir/apps/HBuilder/www"


# app-config.js

ln -sf "$source_dir/app-config.js" "$www_dir"
ln -sf "$source_dir/app-service.js" "$www_dir"
ln -sf "$source_dir/manifest.json" "$www_dir"
# Here you need to delete the original static first before executing
ln -sf "$source_dir/static" "$www_dir"

ln -sf /Users/otto/mycode/dcloud/about-ios/uvue-jsframework/packages/uni-framework/dist/uni-app-x-framework-dev.js "$target_dir"

# Below is the jsframework

How to Test the Latest Framework#

In uvue-jsframework, perform build:framework to obtain dist/uni-app-x-framework-dev.js.

Copy this file to the uni-app-x directory shown above, at the same level as apps.

Kill the application on the simulator, and restart to get the latest version.

Next is how to set breakpoints and observe the actual running situation. Open the developer tools in Safari, this will be organized later.

Select Develop - Simulator - jscontext

image.png

After opening, select the source, see the screenshot, set breakpoints, for example, the touch event can be triggered.

image.png

How to Test Your Page and Demo#

Prerequisites include an Apple developer certificate, hbuilderx dev version, and an updated hosts file.

In hbuilderx Dev, create a new default blank project, select uts, and run it. If the certificate is prepared in advance, you can connect to the iOS device and start it.

image.png

The above is the compilation of custom pages to obtain the output. Below is how to replace the custom output into the simulator being tested.

Find unpackage/dist/dev/app, this is the output.

Open the uni-app-x/apps/www in the simulator application and make a replacement.

For example, mine is

cd /Users/otto/Library/Developer/CoreSimulator/Devices/B2B64986-2B82-4194-8CFE-1A8D0CD8A4C3/data/Containers/Data/Application/29CA23C3-A092-4959-9837-9DADE92874EB/Documents/uni-app-x/apps/HBuilder/www

Restart the application. This way, the content is replaced.

Subsequently, after implementing the function or needing to test, compile through hx. Place the output into the www directory of the simulator just now, equivalent to using hx as the compilation process for testing the output.

How to Replace Application Resources on the Phone#

Resources installed on the real phone need to use a phone assistant, such as iTunes Assistant, to achieve folder access and resource replacement.

image.png

Through such operations, resource file replacement can be achieved.

How to Start an Android Emulator#

First, start Android Studio, open Tools - Device Manager, find a prepared emulator, click edit on the right, and find the name of this emulator.

image.png

It opens like this, copy the AVD Name.

image.png

Register emulator to the global variable, it's better to place it under android.

# android
export ANDROID_HOME="/Users/otto/Library/Android/sdk"
## android studio simulator
export PATH="/Users/otto/Library/Android/sdk/emulator:$PATH"

Later, it was discovered that there is a command to list the inventory, using this method is simpler.

# The return value is the avd name
emulator -list-avds
# Later directly use the following quick start
emulator -avd Pixel_3a_API_34_extension_level_7_arm64-v8a

Appendix related command lines

OptionDescription
-list-avdsList available AVDs.
-sysdir <dir>Search for system disk images in the specified directory.
-system <file>Read the initial system image from the specified file.
-vendor <file>Read the initial vendor image from the specified file.
-writable-systemMake the system and vendor images writable after "adb remount".
-delay-adbDelay ADB communication until boot is complete.
-datadir <dir>Write user data to the specified directory.
-kernel <file>Use a specific simulated kernel.
-ramdisk <file>Use the specified ramdisk image (default is system/ramdisk.img).
-image <file>Deprecated; use -system <file>.
-initdata <file>Same as -init-data <file>.
-data <file>Use the specified data image (default is datadir/userdata-qemu.img).
-encryption-key <file>Read the initial encryption key image from the specified file.
-logcat-output <file>Output logcat to the specified file (default is none).
-partition-size <size>Set the size of the system/data partition, in MB.
-cache <file>Use the specified cache partition image (default is a temporary file).
-cache-size <size>Set the size of the cache partition, in MB.
-no-cacheDisable the cache partition.
-nocacheSame as -no-cache.
-sdcard <file>Use the specified SD card image (default is datadir/sdcard.img).
-quit-after-boot <timeout>Exit the emulator after the guest machine fully boots or after the specified timeout (in seconds).
-qemu-top-dir <dir>Use the emulator in the specified directory (relative or absolute path).
-monitor-adb <verbose_level>Monitor ADB messages between the guest and host, disabled by default.
-snapstorage <file>Store all state snapshots in the specified file (default is datadir/snapshots.img).
-no-snapstorageDisable all snapshot functionality.
-snapshot <name>Automatically start and save using the specified snapshot from the storage file (default is "default-boot").
-no-snapshotPerform a full boot without automatic saving, but qemu vmload and vmsave operate on snapstorage.
-no-snapshot-saveDo not automatically save to snapshot on exit: discard changed state.
-no-snapshot-loadDo not automatically start from snapshot: perform a full boot.
-snapshot-listDisplay a list of available snapshots.
-no-snapshot-update-timeDo not attempt to correct snapshot time upon restoration.
-wipe-dataReset the user data image (copy it from initdata).
-avd <name>Use a specific Android virtual device.
-avd-arch <target>Use a specific target architecture.
-skindir <dir>Search for skins in the specified directory (default is system/skins).
-skin <name>Select the given skin.
-no-skinDeprecated: create an AVD without a skin.
-noskinSame as -no-skin.

How to Start an iOS Simulator#

After installing Xcode, you can directly start simulator.

image.png

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.