You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio中基于空白项目集成Sceneform SDK

嘿,我懂你不想折腾示例项目,就想直接在空白Android项目里集成Sceneform SDK的需求。针对Android Studio 3.1,我整理了一步步的实操步骤,跟着来就行:

集成Sceneform SDK到空白Android项目(Android Studio 3.1)

1. 配置项目级Gradle仓库

首先打开项目根目录下的build.gradle(注意是Project级别的那个,不是app模块的),确保添加了Google的Maven仓库——Sceneform SDK托管在这里:

allprojects {
    repositories {
        jcenter()
        google() // 必须添加这个,否则找不到SDK依赖
    }
}

2. 添加Sceneform依赖到模块级Gradle

打开app模块下的build.gradle(Module: app),先检查minSdkVersion至少设为24(Sceneform要求Android 7.0及以上),然后在dependencies块里添加核心依赖:

android {
    defaultConfig {
        minSdkVersion 24 // 最低要求,不能低于这个
        // 其他原有配置(比如targetSdkVersion、versionCode等)保留不变
    }
}

dependencies {
    // 保留你项目原有的依赖(比如appcompat、constraintlayout等)
    implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.15.0' // 包含交互组件
    implementation 'com.google.ar.sceneform:core:1.15.0' // 核心AR渲染库
}

3. 配置Manifest权限与AR特性

在AndroidManifest.xml里添加AR必需的权限和声明,确保App能访问相机并识别AR设备:

<!-- 添加相机权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 声明需要AR硬件支持 -->
<uses-feature android:name="android.hardware.camera.ar" android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    
    <!-- 声明ARCore依赖 -->
    <meta-data
        android:name="com.google.ar.core"
        android:value="required" />
        
    <!-- 别忘了注册你后续创建的AR Activity -->
    <activity android:name=".ArActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

4. 创建AR布局文件

在res/layout目录下新建一个布局(比如命名为activity_ar.xml),添加Sceneform的ArFragment作为AR视图的容器:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- ARFragment是Sceneform提供的封装好的AR界面组件 -->
    <fragment
        android:name="com.google.ar.sceneform.ux.ArFragment"
        android:id="@+id/ar_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

5. 编写AR Activity逻辑

创建对应的Activity类(比如ArActivity.java),关联布局并初始化AR场景,后续你可以在这里添加自己的3D模型加载、交互逻辑:

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.ar.sceneform.ux.ArFragment;

public class ArActivity extends AppCompatActivity {

    private ArFragment arFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ar);

        // 获取布局中的ArFragment实例
        arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ar_fragment);

        // 示例:如果要加载自己的3D模型,可以这么写(需要把模型放到res/raw目录下)
        // ModelRenderable.builder()
        //     .setSource(this, R.raw.your_3d_model)
        //     .build()
        //     .thenAccept(renderable -> {
        //         // 模型加载成功后的操作,比如添加到AR场景中
        //     })
        //     .exceptionally(throwable -> {
        //         // 处理模型加载失败的情况
        //         return null;
        //     });
    }
}

6. 测试运行

确保你的测试设备支持ARCore(可以在设备的应用商店搜索“ARCore”,查看是否兼容并安装),然后运行项目,就能看到AR预览界面了。接下来你就可以根据自己的需求扩展3D内容和交互功能啦。

内容的提问来源于stack exchange,提问作者john doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:38:28