You can create an app in AppGallery Connect. Then AppGallery Connect will automatically generate basic configuration information for it.

  1. Sign in to AppGallery Connect.
  2. Click My projects.
  3. Click Add project. In the dialog box that is displayed, enter a project name, and click OK.
  4. Go to Project settings > General information. On the page displayed, click Add app.
  5. On the Add app page, set the app name, package name, platform, device, app category (app or game), and language for your app as needed.
  6. Check the app package name and other information in your project under My projects.

Create a Xamarin project in Visual Studio. The package name of the created app must be the same as that entered in AppGallery Connect.

  1. Open Visual Studio and click Create a new project to create a Xamarin project.

  2. Search for xamarin, select Android App (Xamarin), and click Next.
  3. Set Project name and click Create.
  4. Select Single View App as the app template and click OK.

Android Setup

  1. Implement LazyInputStream to read the agconnect-services.json file.
    a) Right-click the project and go to Add > New Item from the shortcut menu.

    b) In the opened window, select Class and give your new class a name such as HmsLazyInputStream.cs.

    c) Inherit from the LazyInputStream class and implement your new class.
    using System; using System.IO; using Android.Content; using Android.Util; using Huawei.Agconnect.Config; namespace agcdemo { public class HmsLazyInputStream : LazyInputStream { public HmsLazyInputStream(Context context) : base(context) { } public override Stream Get(Context context) { try { return context.Assets.Open("agconnect-services.json"); } catch (Exception e) { Log.Error("Hms", $"Failed to get input stream" + e.Message); return null; } } } }
    d) Repeat a and b to create a class named XamarinCustomProvider.cs, which inherits from ContentProvider and reads the agconnect-services.json file before the app launch. Specify the class as a content provider, and set its authorities and InitOrder attributes.
    Sample code:
    using System; using Android.Content; using Android.Database; using Huawei.Agconnect.Config; namespace agcdemo { [ContentProvider(new string[] {"APP_PACKAGE_NAME.XamarinCustomProvider"},InitOrder =99)] public class XamarinCustomProvider : ContentProvider { public override int Delete(Android.Net.Uri uri, string selection, string[] selectionArgs) { throw new NotImplementedException(); } public override string GetType(Android.Net.Uri uri) { throw new NotImplementedException(); } public override Android.Net.Uri Insert(Android.Net.Uri uri, ContentValues values) { throw new NotImplementedException(); } public override bool OnCreate() { AGConnectServicesConfig config = AGConnectServicesConfig.FromContext(Context); config.OverlayWith(new HmsLazyInputStream(Context)); return false; } public override ICursor Query(Android.Net.Uri uri, string[] projection, string selection, string[] selectionArgs, string sortOrder) { throw new NotImplementedException(); } public override int Update(Android.Net.Uri uri, ContentValues values, string selection, string[] selectionArgs) { throw new NotImplementedException(); } } }
  2. Set the package name.
    a) Right-click your project and choose Properties from the shortcut menu.

    b) On the page that is displayed, click Android Manifest and set Package name.

iOS Setup

  1. Setting the Bundle Identifier
    a) Open the created Xamarin project, double-click the Info.plist file, click the Application tab, and set Bundle Identifier to the app package ID entered in AppGallery Connect during app creation.

    b) Go to File > Save or press Command+S to save the settings.
  2. Right-click the project, choose Options from the shortcut menu, select iOS Bundle Signing, set Provisioning Profile, and set Custom Entitlements to Entitlements.plist.
Code copied