Commit 220501f3 authored by dsq's avatar dsq

导入脚本

parent b284b188
...@@ -80,14 +80,13 @@ kotlin { ...@@ -80,14 +80,13 @@ kotlin {
baseName = "kn" // 共享库名称为kn baseName = "kn" // 共享库名称为kn
export(libs.compose.multiplatform.export) // 导出compose多平台库的接口 export(libs.compose.multiplatform.export) // 导出compose多平台库的接口
} }
// val main by compilations.getting // 获取主编译内容 val main by compilations.getting // 获取主编译内容
// val resource by main.cinterops.creating { val resource by main.cinterops.creating {
// //配置C interop(cinterop)资源 //配置C interop(cinterop)资源
// defFile(file("src/ohosArm64Main/cinterop/resource.def")) // cinterop定义文件 defFile(file("src/ohosArm64Main/cinterop/resource.def")) // cinterop定义文件
// includeDirs(file("src/ohosArm64Main/cinterop/include")) // cinterop包含目录 includeDirs(file("src/ohosArm64Main/cinterop/include")) // cinterop包含目录
// compilerOpts("-fno-modules") // 禁用模块以避免平台库依赖 compilerOpts("-fno-modules") // 禁用模块以避免平台库依赖
// } }
} }
// 配置各平台的依赖关系 // 配置各平台的依赖关系
......
#!/bin/bash
#
# Eazytec is pleased to support the open source community by making CPF-KMP-CMP available.
# Copyright (C) 2026 Eazytec. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
set -e
# Record script start time
START_TIME=$(date +%s)
# ====================== [1. Default Configuration and Parameter Parsing] ======================
DEFAULT_PLATFORM="ohosArm64"
DEFAULT_TARGET_ID="127.0.0.1:5555"
DEFAULT_BUNDLE_NAME="com.example.harmonyapp"
DEFAULT_ABILITY_NAME="EntryAbility"
LOCAL_OHOS_PATH=""
usage() {
echo "Usage: $0 [options] [PLATFORM] [TARGET_ID]"
echo ""
echo "Parameters:"
echo " PLATFORM Build platform (default: $DEFAULT_PLATFORM)"
echo " TARGET_ID Device ID (default: $DEFAULT_TARGET_ID)"
echo ""
echo "Options:"
echo " -b BUNDLE Set bundle name (current: $DEFAULT_BUNDLE_NAME)"
echo " -a ABILITY Set Ability name (current: $DEFAULT_ABILITY_NAME)"
echo " -p PATH Set external OHOS project path (localOhosPath)"
echo " -h Show this help message"
echo ""
echo "Examples:"
echo " $0 ohosArm64 127.0.0.1:5555"
echo " $0 -b com.test.app -a MainAbility"
echo " $0 -p /path/to/external/ohos/project"
exit 0
}
# Preset variables
BUNDLE_NAME=$DEFAULT_BUNDLE_NAME
ABILITY_NAME=$DEFAULT_ABILITY_NAME
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Parse options
while getopts "b:a:p:h" opt; do
case $opt in
b) BUNDLE_NAME=$OPTARG ;;
a) ABILITY_NAME=$OPTARG ;;
p) LOCAL_OHOS_PATH=$OPTARG ;;
h) usage ;;
?) usage ;;
esac
done
# Remove parsed options
shift $((OPTIND-1))
# Get positional parameters
PLATFORM=${1:-$DEFAULT_PLATFORM}
TARGET_ID=${2:-$DEFAULT_TARGET_ID}
echo -e "\033[32m▶ Run environment configuration:\033[0m"
echo " - Platform: $PLATFORM"
echo " - Device: $TARGET_ID"
echo " - Bundle: $BUNDLE_NAME"
echo " - Ability: $ABILITY_NAME"
if [ -n "$LOCAL_OHOS_PATH" ]; then
echo " - External OHOS path: $LOCAL_OHOS_PATH"
fi
echo "------------------------------------------------------------"
# --- Configuration ---
# Usually DevEco default path on Mac
DEVECO_PATH="/Applications/DevEco-Studio.app"
MIN_VERSION="6.0.0"
echo "🔍 Checking environment configuration..."
# 1. Check if DevEco exists
if [ ! -d "$DEVECO_PATH" ]; then
echo "❌ Error: DevEco Studio not found at $DEVECO_PATH."
exit 1
fi
# 2. Get version (from Info.plist CFBundleShortVersionString)
# Output example: 5.0.3.406
CURRENT_VERSION=$(defaults read "$DEVECO_PATH/Contents/Info.plist" CFBundleShortVersionString)
echo "Current DevEco version: $CURRENT_VERSION"
echo "Minimum required version: $MIN_VERSION"
CUR_MAJOR=$(echo "$CURRENT_VERSION" | cut -d. -f1)
CUR_MINOR=$(echo "$CURRENT_VERSION" | cut -d. -f2)
REQ_MAJOR=$(echo "$MIN_VERSION" | cut -d. -f1)
REQ_MINOR=$(echo "$MIN_VERSION" | cut -d. -f2)
if [ "$CUR_MAJOR" -lt "$REQ_MAJOR" ] 2>/dev/null || \
{ [ "$CUR_MAJOR" -eq "$REQ_MAJOR" ] && [ "$CUR_MINOR" -lt "$REQ_MINOR" ]; }; then
echo "-------------------------------------------------------"
echo -e "${RED}⛔️ Error: DevEco version is too low!${NC}"
echo -e "${RED}Current version: $CURRENT_VERSION${NC}"
echo -e "${RED}Minimum required: $MIN_VERSION+${NC}"
echo -e "${RED}Please upgrade DevEco Studio and run this script again.${NC}"
echo "-------------------------------------------------------"
exit 5
fi
echo "✅ DevEco version meets requirements ($CURRENT_VERSION)"
# ====================== [2. Environment Path and SDK Configuration] ======================
SDK_HOME=/Applications/DevEco-Studio.app/Contents
HDC_BIN=$SDK_HOME/sdk/default/openharmony/toolchains/hdc
export DEVECO_SDK_HOME=$SDK_HOME/sdk
export PATH=$DEVECO_SDK_HOME:$SDK_HOME/jbr/Contents/Home/bin:$SDK_HOME/tools/node/bin:$SDK_HOME/tools/ohpm/bin:$SDK_HOME/tools/hvigor/bin:$PATH
$HDC_BIN -t $TARGET_ID shell aa force-stop $BUNDLE_NAME
# ====================== [3. Gradle Build] ======================
echo "Working path: $(pwd)"
echo "Building OpenHarmony ARM64..."
if [ "$PLATFORM" = "ohosArm64" ]; then
# Execute Gradle build in project root
if [ -n "$LOCAL_OHOS_PATH" ]; then
echo "Using external OHOS path: $LOCAL_OHOS_PATH"
./gradlew :composeApp:publishDebugBinariesToHarmonyApp -PharmonyAppPath="$LOCAL_OHOS_PATH"
else
./gradlew :composeApp:publishDebugBinariesToHarmonyApp
fi
elif [ "$PLATFORM" = "iosSimulatorArm64" ]; then
./gradlew :composeApp:linkDebugFrameworkIosSimulatorArm64
else
echo -e "\033[31mError: Unsupported platform '$PLATFORM'\033[0m"
exit 4
fi
# SO packaging end time
HAP_START_TIME=$(date +%s)
# --- Subsequent install logic (hdc install etc.) ---
# ====================== [4. HAP Package Build] ======================
# Switch to harmonyApp directory for subsequent OHOS commands
# Use external path if specified, otherwise use default harmonyApp directory
if [ -n "$LOCAL_OHOS_PATH" ]; then
HARMONY_APP_DIR="$LOCAL_OHOS_PATH"
else
HARMONY_APP_DIR="harmonyApp"
fi
if [ ! -d "$HARMONY_APP_DIR" ]; then
echo -e "\033[31mError: harmonyApp directory not found: $HARMONY_APP_DIR\033[0m"
exit 4
fi
cd "$HARMONY_APP_DIR"
echo "Switched to harmonyApp directory: $(pwd)"
echo "Running Hvigor sync and HAP packaging..."
ohpm install --all
node $SDK_HOME/tools/hvigor/bin/hvigorw.js --sync -p product=default --analyze=normal --parallel
node $SDK_HOME/tools/hvigor/bin/hvigorw.js --mode module -p module=entry@default -p product=default -p requiredDeviceType=phone assembleHap --analyze=normal --parallel -p buildM
# ====================== [5. Install and Push Debug Components] ======================
AVAILABLE_TARGETS=$($HDC_BIN list targets)
HAP_DIR="./entry/build/default/outputs/default"
SIGNED_HAP="entry-default-signed.hap"
HAP_FILE="$HAP_DIR/$SIGNED_HAP"
echo "Package path: $HAP_DIR/$SIGNED_HAP"
# Check if signed HAP package exists
if [ ! -f "$HAP_FILE" ]; then
echo -e "\033[31mError: Signed HAP package not found!\033[0m"
echo ""
echo -e "\033[33mExpected path:\033[0m"
echo " $HARMONY_APP_DIR/entry/build/default/outputs/default/entry-default-signed.hap"
echo ""
echo -e "\033[33mPossible causes:\033[0m"
echo " • HAP package has not been built yet"
echo " • Build process failed"
echo " • HAP package is not signed"
echo ""
echo -e "\033[36mSuggested actions:\033[0m"
echo " 1. Check the build logs above for errors"
echo " 2. Verify DevEco Studio signing configuration is correct"
echo " 3. Manually build and sign HAP package in DevEco Studio"
echo " 4. Check files in entry/build/default/outputs/default/ directory"
exit 6
fi
AVAILABLE_TARGETS=$($HDC_BIN list targets)
if ! echo "$AVAILABLE_TARGETS" | grep -q "$TARGET_ID"; then
echo -e "\033[31mError: Device $TARGET_ID is offline!\033[0m"
exit 5
fi
echo "Pushing debug components and installing HAP..."
echo " - Device: $TARGET_ID"
# Install HAP (using temp directory)
REMOTE_HAP_DIR="/data/local/tmp/debug_install"
$HDC_BIN -t $TARGET_ID shell mkdir -p $REMOTE_HAP_DIR
$HDC_BIN -t $TARGET_ID file send $HAP_FILE $REMOTE_HAP_DIR
INSTALL_OUTPUT=$($HDC_BIN -t $TARGET_ID shell bm install -p "$REMOTE_HAP_DIR/$SIGNED_HAP" 2>&1)
sleep 1
INSTALL_RESULT=$?
echo "Install status: $INSTALL_OUTPUT"
if echo "$INSTALL_OUTPUT" | grep -qE "failed"; then
echo "Install failed detected, uninstalling and reinstalling..."
$HDC_BIN -t $TARGET_ID shell bm uninstall -n "$BUNDLE_NAME" >/dev/null 2>&1 || true
sleep 5
# Reinstall logic...
INSTALL_OUTPUT=$($HDC_BIN -t $TARGET_ID shell bm install -p "$REMOTE_HAP_DIR/$SIGNED_HAP" 2>&1)
sleep 5
fi
# ====================== [6. App Launch and Debug Mount] ======================
echo -e "\033[33mLaunching app and starting debug listener...\033[0m"
$HDC_BIN -t $TARGET_ID shell rm -rf $REMOTE_HAP_DIR
# Get system version
#SYSTEM_VERSION=$($HDC_BIN -t $TARGET_ID shell param get const.ohos.apiversion 2>/dev/null || echo "unknown")
#echo "Detected system version: $SYSTEM_VERSION"
# Check screen lock status and prompt
echo ""
echo -e "\033[33m⚠ Important:\033[0m"
echo -e " If device screen is locked, please unlock it manually"
echo -e " System cannot auto-unlock in developer mode (security restriction)"
echo ""
# Step 1: Start app (without -D debug mode, compatible with 5.1)
echo " -> Executing aa start (launch app)..."
AA_START_OUTPUT=$($HDC_BIN -t $TARGET_ID shell aa start -a $ABILITY_NAME -b $BUNDLE_NAME 2>&1)
# Package push complete time
END_TIME=$(date +%s)
AA_START_RESULT=$?
# Check if screen lock error
if echo "$AA_START_OUTPUT" | grep -q "10106102\|screen is locked"; then
echo -e "\033[31mError: Device screen is locked!\033[0m"
echo ""
echo -e "\033[33mPlease follow these steps:\033[0m"
echo " 1. Manually unlock device screen"
echo " 2. Keep screen awake (recommended for dev: Settings -> Display & Brightness -> Screen timeout -> Never)"
echo " 3. Run this script again"
echo ""
echo -e "\033[36mNote: Auto-unlock is not available in developer mode (system security restriction)\033[0m"
exit 1
fi
# If start failed but not screen lock error, continue anyway
if [ $AA_START_RESULT -ne 0 ]; then
echo -e "\033[33m⚠ App start command returned non-zero exit code, but continuing...\033[0m"
fi
# Push lldb-server
$HDC_BIN -t $TARGET_ID shell mkdir -p /data/local/tmp/debugserver
$HDC_BIN -t $TARGET_ID shell rm -f /data/local/tmp/debugserver/lldb-server
$HDC_BIN -t $TARGET_ID file send $SDK_HOME/sdk/default/hms/native/lldb/aarch64-linux-ohos/lldb-server /data/local/tmp/debugserver
$HDC_BIN -t $TARGET_ID shell chmod 755 /data/local/tmp/debugserver/lldb-server
# Step 2: Get app PID
get_pid_func() {
$HDC_BIN -t $TARGET_ID shell "pidof $BUNDLE_NAME" 2>/dev/null | tr -d '\r' | tr -d '\n' | awk '{print $1}'
}
echo -n "Waiting for app to start"
MAX_WAIT=20
COUNT=0
APP_PID=""
while [ $COUNT -lt $MAX_WAIT ]; do
APP_PID=$(get_pid_func)
if [[ "$APP_PID" =~ ^[0-9]+$ ]]; then
echo -e "\nApp started (PID: $APP_PID)"
break
fi
echo -n "."
sleep 1
let COUNT=COUNT+1
done
if [ -z "$APP_PID" ]; then
echo -e "\n\033[31mFailed: App did not start within expected time!\033[0m"
echo ""
echo -e "\033[33mPossible causes:\033[0m"
echo " • Device screen is locked (most common)"
echo " • App installation failed"
echo " • Device performance issue causing startup timeout"
echo ""
echo -e "\033[36mSuggested actions:\033[0m"
echo " 1. Ensure device screen is unlocked"
echo " 2. Check device connection: hdc list targets"
echo " 3. Manually launch app to verify it runs"
exit 1
fi
$HDC_BIN -t $TARGET_ID shell aa attach -b $BUNDLE_NAME
# Step 3: Start lldb-server and attach to process
echo " -> Starting lldb-server and attaching to process (PID: $APP_PID)..."
$HDC_BIN -t $TARGET_ID shell aa process -a $ABILITY_NAME -b $BUNDLE_NAME -D "/data/local/tmp/debugserver/lldb-server platform --listen unix-abstract:///lldb-server/platform.sock"
echo "------------------------------------------------------------"
echo -e "\033[32mBuild, install and app launch completed!\033[0m"
# Calculate total duration
ELAPSED_TIME=$((END_TIME - START_TIME))
MINUTES=$((ELAPSED_TIME / 60))
SECONDS=$((ELAPSED_TIME % 60))
echo -e "App info:"
echo -e " - Bundle: $BUNDLE_NAME"
echo -e " - PID: $APP_PID"
echo -e " - Device: $TARGET_ID"
ELAPSED_TIME=$((HAP_START_TIME - START_TIME))
MINUTES=$((ELAPSED_TIME / 60))
SECONDS=$((ELAPSED_TIME % 60))
echo -e " - SO build time: ${MINUTES}m${SECONDS}s (${ELAPSED_TIME}s)"
ELAPSED_TIME=$((END_TIME - HAP_START_TIME))
MINUTES=$((ELAPSED_TIME / 60))
SECONDS=$((ELAPSED_TIME % 60))
echo -e " - HAP package and push time: ${MINUTES}m${SECONDS}s (${ELAPSED_TIME}s)"
ELAPSED_TIME=$((END_TIME - START_TIME))
MINUTES=$((ELAPSED_TIME / 60))
SECONDS=$((ELAPSED_TIME % 60))
echo -e " - Total time: ${MINUTES}m${SECONDS}s (${ELAPSED_TIME}s)"
echo -e "\033[36mNote: You can now connect via LLDB to debug on the device\033[0m"
echo "------------------------------------------------------------"
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment