Please conduct the following tasks alone. For implementation details you can refer to the lecture slides or the Android developer website. Please do not hesitate to ask me or the tutor if you have any questions. Under „Tip“ you can find some hints for the task. The „Checklist“ specifies what should happen when your implementation is correct.
In this assignment you will create a Sceneform based ARCore app. Sceneform empowers Android developers to work with ARCore without learning 3D graphics and OpenGL. This is why we will use it here, it is much simpler than using ARCore alone.
To create an ARCore app, you require an Android Studio Version 3.1 or later with Android SDK Platform version 7.0 (API level 24) or higher.
Prepare your device or emulator. You can run AR apps on a supported device or emulator. On the emulator, sign into the Play Store or Update ARCore manually.
Notice: if you are working on a Mac, it will not be possible to use the Emulator for Sceneform Apps (like this one). You will need a supported device.
Now start in Android Studio on MacOS, Linux or Windows by using the wizard to create a new Android project. Choose the Basic Activity template as a base. Use the default name “MainActivity” for the activity.
In the gradle script we need to enable Java 8 and add the dependencies for Sceneform.
Sceneform uses language constructs from Java 8. For projects that have a min API level less than 26, you need to explicitly add support for Java 8.
In the android {} section of app/build.gradle add:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
In the app/build.gradle file add dependencies for the Sceneform API and the Sceneform UX elements.
In the dependency {} section add:
implementation 'com.google.ar.sceneform:core:1.0.0'
implementation "com.google.ar.sceneform.ux:sceneform-ux:1.0.0"
Press the “Sync now” link to update the project.
That does it for the project setup! Now let’s add the Sceneform fragment to the view layout.
There are several aspects of making a great AR experience that involve interacting with multiple views. Things like displaying a non-text indicator that the user should move the phone in order for ARCore to detect planes and handling gestures for moving and scaling objects. To do this add ArFragment to the app/res/layout/content_main.xml
file.
Open content_main.xml and let’s add the fragment and the view. Here’s the text of the layout file, but feel free to use the graphical view if that is more comfortable for you.
Replace the existing TextView element with the fragment:
<fragment
android:id="@+id/sceneform_fragment"
android:name="com.google.ar.sceneform.ux.ArFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
ARCore requires entries in the AndroidManifest. These declare that the camera will be used by the application, and that the AR features are used along with designating the application as requiring ARCore to run. This last metadata entry is used by the Play Store to filter out applications for users on devices that are not supported by ARCore.
Open app/manifests/AndroidManifest.xml
and in the <manifest>
section add these elements:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.ar" android:required="true" />
Then add the metadata in the <application>
section:
<meta-data android:name="com.google.ar.core" android:value="required" />
We’ll be referencing the fragment a lot as we work with the AR scene. To make things easier, open MainActivity
and add a member variable at the top of the class:
private ArFragment fragment;
Remember to Import classes!
Initialize it at the bottom of onCreate()
. Since we’re using a fragment, we need to use the fragment manager to find the fragment:
fragment = (ArFragment)
getSupportFragmentManager().findFragmentById(R.id.sceneform_fragment);
Great! Now we have the minimum amount of code to start using ARCore and make sure it works. Next, let’s try it out!
Connect your phone via USB and enable developer mode, if not already done (see your phone manufacturers guide for how to do this). Prepare your device or emulator
You can run AR apps on a supported device or emulator.
On the emulator, sign into the Play Store or Update ARCore manually.
There are additional requirements to run Sceneform apps in the emulator:
You need Android Emulator version 27.2.9 or later.
OpenGL ES 3.0 or higher must be supported and enabled in the Android Emulator (currently not available on Macs).
Make sure your emulator is configured to use the latest version. In the Extended controls panel ( on the Toolbar), select Settings > Advanced > OpenGL ES API level > Renderer maximum (up to OpenGL ES 3.1), and then restart the emulator.
Run the emulator and check whether OpenGL ES 3.0 or higher is being used:
adb logcat | grep eglMakeCurrent
If you see ver 3 0 or higher version, then you can run Sceneform apps. If you see a lower version, then your desktop GPU does not support OpenGL ES 3.0 and you must use a supported device to run Sceneform apps.
More information on this can be found here
Checklist | |
---|---|
The app runs on the phone showing the dottet pattern when moving around. This is where we will place our objects. |