写点什么

ArkUI-X 添加到现有 Android 项目中

作者:龙儿筝
  • 2025-05-28
    湖北
  • 本文字数:1226 字

    阅读完需:约 4 分钟

本教程主要讲述如何利用 ArkUI-X SDK 完成 Android AAR 开发,实现基于 ArkTS 的声明式开发范式在 android 平台显示。包括:


1.跨平台 Library 工程开发介绍


2.AAR 在 Android 应用工程的集成方式

使用 ACE Tools 和 DevEco Studio 集成 ArkUI-X SDK 进行 Android AAR 开发

可以通过通过 ACE Tools 或 DevEco Studio 完成

ACE Tools

1.ace create 命令创建一个跨平台的 library 模版工程:


ace create [project] -t library
复制代码


2.执行 ace build aar 命令,构建 Android aar 包。


ace build aar
复制代码

DevEco Studio

1.创建一个 ArkUI-X Library 工程



2.通过执行 Build APP(s)选项,构建出 Android aar 包



在应用工程初始化 ArkUI-X

通过 Android studio 创建一个应用工程,将我们上述的 aar 包添加到工程目录下的 libs 目录中 Application 部分

继承调用

package com.example.helloworld;
import com.example.myaar.MyApplication;
public class MainApplication extends MyApplication {
}
复制代码

代理类调用

package com.example.helloworld;

import android.app.Application;import android.content.res.Configuration;import android.util.Log;
import ohos.stage.ability.adapter.StageApplicationDelegate;
public class MainApplication extends Application { private StageApplicationDelegate appDelegate = null;
public void onCreate() { super.onCreate(); this.appDelegate = new StageApplicationDelegate(); this.appDelegate.initApplication(this); } public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (this.appDelegate == null) { Log.e("StageApplication", "appDelegate is null"); } else { this.appDelegate.onConfigurationChanged(newConfig); } }}
复制代码

打开 ArkUI-X 页面

使用 Activity 加载页面

在 AndroidManifest.xml 中配置 Activity


 <manifest xmlns:android="http://schemas.android.com/apk/res/android" >
<application android:name="com.example.test_aar_demo.MainApplication" >
<activity android:name="com.example.myaar.EntryMainAbilityActivity" android:windowSoftInputMode="adjustResize |stateHidden" android:configChanges="orientation|keyboard|layoutDirection|screenSize|uiMode|smallestScreenSize" /> </application>
</manifest>
复制代码


使用 intent 打开 Activity 页面


startActivity(new Intent(this, EntryMainAbilityActivity.class));
复制代码

使用 Fragment 加载页面

在 Activity 中加载 StageFragment


StageFragment stageFragment = new StageFragment();stageFragment.setInstanceName("com.example.myaar:entry:EntryMainAbilityActivity:");supportFragmentManager.beginTransaction().replace(R.id.content, it).commitNowAllowingStateLoss();
复制代码



发布于: 刚刚阅读数: 5
用户头像

龙儿筝

关注

还未添加个人签名 2024-10-27 加入

还未添加个人简介

评论

发布
暂无评论
ArkUI-X添加到现有Android项目中_龙儿筝_InfoQ写作社区