Embed a Unity Activity in an Android app

These instructions were found here originally, and are slightly modified to solve some more errors.

1. Support the back button

Check the snippet here.

2. Export your project

  1. Go into ‘Build Settings’ and switch to Android. Change the Texture to ETC2, and make sure the build system is set to ‘Gradle’.
  2. Open the ‘Player settings’ from that screen and set the Bundle Identifier to the package name you desire.
  3. Note that this will probably not save (at least it doesn’t in the current version, 2018.2.2f1) so you will need to edit it again later.
  4. Export.

3. Open the exported project in Android Studio

Don’t import it into a new project as said in the Medium post, just open the project.

  1. Modify the AndroidManifest.xml and remove the IntentFilter. Otherwise you wil end up with a second launcher icon for your app.
  2. Change the application into a library by editing the build.gradle file. Switch this:
     apply plugin: 'com.android.application'
    

    into

     apply plugin: 'com.android.library'
    
  3. Remove the line with applicationId from the gradle file, as libraries can’t specify an application id.
  4. If the ‘Bundle Identifier’ was not saved in the previous section, edit it now. You should modify the AndroidManfiest.xml and the folder structure.

4. Integrate the library in your project

  1. Choose ‘Rebuild project’ in Android Studio to build the library.
  2. Pick the file from build/outputs/aar/MyProject-debug.aar
  3. In your other app, choose File > New Module > Import JAR/AAR package and select your .aar file.
  4. Edit the settings.gradle to include your new module.
  5. Edit the build.gradle1 of your main module to reference the new module: implementation project(":your_aar_file_name") (inside the dependencies block).
  6. Add the Unity Activity to the manifest. It is called UnityPlayerActivity:
     <activity android:name="com.pixplicity.demo.augmentedimage.UnityPlayerActivity" />
    
  7. Launch the new Activity from your app using this snippet.

5. (Optional) disable it for non-ARCore devices.

Follow the steps described here