Huawei Unity SDK Setup

Step 1. Unity OneSignal SDK setup

Step 2. Huawei Setup

  • Place this inside Assets/Plugins/Android/OneSignalConfig

Step 3. Generating a Signing Certificate Fingerprint

You can skip this step if you already have added your SHA-256 certificate fingerprint to Huawei's dashboard for a different Huawei service.

Step 4. Add Huawei Gradle Plugin and Dependencies

Unity 2019.3 Or Newer

4a.1 Go to File -> Build Settings -> Player Settings -> Android -> Publishing Settings and click under
Custom Main Gradle Template. This will create a mainTemplate.gradle.

4a.2 On mainTemplate.gradle remove the comment
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

  • Under dependencies add implementation 'com.huawei.hms:push:5.3.0.304'

4a.3 Create a new file named Assets/Plugins/Android/settingsTemplate.gradle with the following contents.

import java.nio.file.Files

// This file contents should be placed at Assets/Plugins/Android/settingsTemplate.gradle
// This works around Unity's 2019.3 bug where their root build.gradle is placing buildscript under allprojects
// On it's own it doesn't create issues however doing so means including a buildscript block in any sub projects
//   such as "unityLibrary" which is generated from the template Assets/Plugins/Android/mainTemplate.gradle does not work.
// It results in a build error of "Configuration with name 'compileClasspath' not found." on a lint task.
// Normally adding "lintOptions { abortOnError false }" bypasses any lint task errors however
//   either due to a bug with the Android Gradle plugin or an order of operations this does seem to be applying in this case.
// Until Unity fixes their root build.gradle we will need to keep using this file to enable any additional Gradle plugins.

static void enableJetifier(Project project) {
    project.ext['android.useAndroidX'] = true
    project.ext['android.enableJetifier'] = true
}

static void addBuildscript(Project project) {
    project.buildscript {
        repositories {
            maven { url 'https://plugins.gradle.org/m2/' } // Gradle Plugin Portal
            maven { url 'http://developer.huawei.com/repo/' }
        }
        dependencies {
            // OneSignal-Gradle-Plugin
            classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.7, 0.99.99]'
            classpath 'com.huawei.agconnect:agcp:1.2.1.301'
        }
    }
}

static void applyPlugins(Project project) {
    // Only apply OneSignal-Gradle-Plugin to the :app project. (Unity calls this :launcher)
    if (project.name != 'launcher')
        return

    project.afterEvaluate {
        it.apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
        it.apply plugin: 'com.huawei.agconnect'
    }
}

static void copyHMSFile(Project project) {
    // Only apply OneSignal-Gradle-Plugin to the :app project. (Unity calls this :launcher)
    if (project.name != 'launcher')
        return

    def newFile = new File("${project.rootDir}/launcher/agconnect-services.json")

    if (newFile.exists())
        return

    def file = new File("${project.rootDir}/unityLibrary/OneSignalConfig/agconnect-services.json")
    Files.copy(file.toPath(), newFile.toPath())
}

gradle.rootProject {
    it.afterEvaluate {
        it.allprojects {
            // Since Unity 2019.3 enabling Jetifier via mainTemplate.gradle is no longer working
            // Enabling it for all gradle projects here.
            enableJetifier(it)

            addBuildscript(it)
            applyPlugins(it)
            copyHMSFile(it)
        }
    }
}


// Per Unity's docs /*/*INCLUDES/*/* should be at the bottom.
// https://docs.unity3d.com/Manual/android-gradle-overview.html

// However it seem to have left out this include line
include ':launcher', ':unityLibrary'
**INCLUDES**

Unity 2019.2 Or Older

4b.1 Go to File -> Build Settings -> Player Settings -> Android -> Publishing Settings and click under
Custom Gradle Template. This will create a mainTemplate.gradle.

4b.2 On mainTemplate.gradle remove the comment
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

  • Under buildscript { repositories } add maven { url 'http://developer.huawei.com/repo/' }
  • Under buildscript { dependencies } add classpath 'com.huawei.agconnect:agcp:1.2.1.301'
  • Under allprojects { repositories } add maven { url 'http://developer.huawei.com/repo/' }
  • After adding these dependencies, add below allprojects {} the following
allprojects {
  ...
}

task copyReport(type: Copy) {
    from file("$projectDir/OneSignalConfig/agconnect-services.json")
    into file("$projectDir")
}

allprojects {
    afterEvaluate {
        for (def task in it.tasks)
            if (task != rootProject.tasks.copyReport)
                task.dependsOn rootProject.tasks.copyReport
    }
}
  • Under dependencies add implementation 'com.huawei.hms:push:4.0.3.301'
  • At the bottom of the file add apply plugin: 'com.huawei.agconnect'

Step 5. Run and test your app

Run your app on a real Huawei device to make sure your device is subscribed to notifications as a Huawei device and can receive notifications sent from the OneSignal dashboard.

Step 6. Configure Huawei Location Service (Optional)

  • 6.1 On mainTemplate.gradle under dependencies add implementation 'com.huawei.hms:location:<HUAWEI HMS VERSION #>
  • 6.2 Make sure to also add the location permission to your AndroidManifest.xml if you don't have this already
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

  • HMS error code reference to help debug logs and successfully integrate HMS Core SDK with the OneSignal SDK.

Did this page help you?