【原创教程】在新版本 Android Studio Bumblebee 中集成 HMS Core 的全流程

【原创教程】在新版本 Android Studio Bumblebee 中集成 HMS Core 的全流程 AppGallery Connect 初始配置

  1. 打开AppGallery Connect网站。
  2. 点击我的项目->添加项目,根据提示创建项目。
  3. 点击添加应用,根据提示创建应用。
  4. 以上操作完成后,将自动跳转到对应应用信息页面。

Android工程 创建

  1. 启动Android Studio
  2. 选择菜单栏中的File->New->New Project,选择Empty Activity->Next
  3. 填写应用信息,注意应用名称包名应当与AppGallery Connect上创建应用的对应信息一致,点击Finish
  4. 等待创建完毕即可。

Android签名证书 创建

  1. 在对应工程内的菜单栏中选择Build->Generare Signed Bundle / APK…
  2. 点击APK->Next以选择类别。
  3. 如果已有签名文件则点击**Choose existing…选择对应的签名文件,否则点击Create new…**创建一个新的签名文件。
  4. 签名文件创建/选择后,对话框中会显示对应签名文件的信息,然后点击Next
  5. 选择文件目录,选中release,点击Finish
  6. 将会在上一步对应目录中生成一个已签名的APK文件。

Android签名证书指纹 获取与配置

获取指纹

  1. 打开命令提示符cmd,进入JDKbin目录下。
  2. 使用keytool -list -v -keystore "xxx.jks"查看签名文件信息。
  3. 输入签名文件密钥库口令。
  4. 获取证书指纹SHA256

配置指纹

  1. 打开AppGallery Connect,找到项目设置->常规->应用部分。
  2. 将之前获取的证书指纹SHA256填入对应输入框中。
  3. 点击保存指纹。

AppGallery Connect 开通服务

  1. 打开AppGallery Connect,找到项目设置->API管理页面。
  2. 按照需求开启对应的API服务。

AppGallery Connect配置文件 添加

  1. 打开AppGallery Connect,找到项目设置->常规->应用部分。
  2. 下载agconnect-services.json配置文件。
  3. agconnect-services.json文件放置到Android工程的应用级根目录下。

Android工程Maven仓库 配置

  1. 打开项目级build.gradle文件并添加以下内容。
buildscript {
    dependencies {
        ...
        classpath 'com.android.tools.build:gradle:7.1'
        classpath 'com.huawei.agconnect:agcp:1.6.0.300'
    }
}
  1. 打开项目级settings.gradle文件并添加以下内容。
pluginManagement {
    repositories {
        ...
        maven { url 'https://developer.huawei.com/repo/' }
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven { url 'https://developer.huawei.com/repo/' }
    }
}

Android工程依赖 配置

  1. 将签名文件xxx.jks放置到应用级根目录下。
  2. 打开应用级build.gradle文件并添加以下内容。
plugins {
    ...
    id 'com.huawei.agconnect'
}
android {
    ...
    signingConfigs {
        release {
            storeFile file('xxx.jks')
            keyAlias 'xxx'
            keyPassword 'xxx'
            storePassword 'xxx'
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }
    ...
    buildTypes {
        release {
            signingConfig signingConfigs.release
            ...
        }
        debug {
            signingConfig signingConfigs.release
            debuggable true
        }
    }
    ...
}
dependencies {
    ...
    implementation 'com.huawei.hms:xxx:xxx'
}

Android工程混淆 配置

如果项目使用了ProGuard,则需要打开proguard-rules.pro文件并添加以下内容。

-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.huawei.hianalytics.**{
    *;
}
-keep class com.huawei.updatesdk.**{
    *;
}
-keep class com.huawei.hms.**{
    *;
}

Android工程权限 配置

打开AndroidManifest.xml文件并添加对应权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.coolkk.codelabs.hmsaccounts">
    ...
    <uses-permission android:name="android.permission.xxx" />
    ...
</manifest>

Android工程 同步

点击工具栏中的Gradle图标或悬浮栏中的Sync Now,进行相关配置的同步。

参考资料

3 回复

赞 (๑•̀ㅂ•́)و✧ 👍👍


在Android Studio Bumblebee版本中集成HMS Core的全流程如下:

  1. 安装Android Studio Bumblebee:确保已安装最新版本的Android Studio Bumblebee。

  2. 创建或打开项目:在Android Studio中创建新项目或打开现有项目。

  3. 配置Gradle文件:在项目的build.gradle文件中添加HMS Core的Maven仓库地址:

    repositories {
        maven { url 'https://developer.huawei.com/repo/' }
    }
  4. 添加HMS Core依赖:在app模块的build.gradle文件中添加所需的HMS Core SDK依赖,例如:

    dependencies {
        implementation 'com.huawei.hms:location:6.0.0.300'
    }
  5. 同步项目:点击“Sync Now”同步项目,确保依赖项正确加载。

  6. 配置agconnect-services.json文件:从AppGallery Connect下载agconnect-services.json文件,并将其放置在app模块的根目录下。

  7. 初始化HMS Core:在Application类或主Activity中初始化HMS Core:

    import com.huawei.hms.api.HuaweiMobileServicesUtil;
    
    public class MyApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            HuaweiMobileServicesUtil.setApplication(this);
        }
    }
  8. 配置权限:在AndroidManifest.xml文件中添加必要的权限,例如:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  9. 使用HMS Core API:在代码中调用HMS Core提供的API,例如使用定位服务:

    import com.huawei.hms.location.FusedLocationProviderClient;
    import com.huawei.hms.location.LocationRequest;
    import com.huawei.hms.location.LocationServices;
    
    FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    LocationRequest locationRequest = new LocationRequest();
    fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
  10. 测试和调试:运行应用,确保HMS Core功能正常工作,并根据需要进行调试。

以上步骤完成了在Android Studio Bumblebee中集成HMS Core的全流程。

在Android Studio Bumblebee中集成HMS Core的步骤如下:

  1. 安装HMS Core插件:打开Android Studio,进入File > Settings > Plugins,搜索并安装HMS Core Toolkit插件。

  2. 配置项目:在build.gradle文件中添加HMS Core依赖:

    dependencies {
        implementation 'com.huawei.hms:hwid:6.4.0.300'
    }
  3. 同步项目:点击Sync Now同步项目,确保依赖正确加载。

  4. 初始化HMS Core:在Application类中初始化HMS Core:

    HMSCore.init(this);
  5. 使用HMS服务:根据需求调用HMS Core提供的API,如账号登录、地图服务等。

完成以上步骤后,即可在应用中集成并使用HMS Core服务。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!