【原创教程】在新版本 Android Studio Bumblebee 中集成 HMS Core 的全流程
【原创教程】在新版本 Android Studio Bumblebee 中集成 HMS Core 的全流程 AppGallery Connect 初始配置
- 打开AppGallery Connect网站。
- 点击我的项目->添加项目,根据提示创建项目。
- 点击添加应用,根据提示创建应用。
- 以上操作完成后,将自动跳转到对应应用信息页面。
Android工程 创建
- 启动Android Studio。
- 选择菜单栏中的File->New->New Project,选择Empty Activity->Next。
- 填写应用信息,注意应用名称、包名应当与AppGallery Connect上创建应用的对应信息一致,点击Finish。
- 等待创建完毕即可。
Android签名证书 创建
- 在对应工程内的菜单栏中选择Build->Generare Signed Bundle / APK…。
- 点击APK->Next以选择类别。
- 如果已有签名文件则点击**Choose existing…选择对应的签名文件,否则点击Create new…**创建一个新的签名文件。
- 签名文件创建/选择后,对话框中会显示对应签名文件的信息,然后点击Next。
- 选择文件目录,选中release,点击Finish。
- 将会在上一步对应目录中生成一个已签名的APK文件。
Android签名证书指纹 获取与配置
获取指纹
- 打开命令提示符cmd,进入JDK的bin目录下。
- 使用
keytool -list -v -keystore "xxx.jks"
查看签名文件信息。 - 输入签名文件密钥库口令。
- 获取证书指纹SHA256。
配置指纹
- 打开AppGallery Connect,找到项目设置->常规->应用部分。
- 将之前获取的证书指纹SHA256填入对应输入框中。
- 点击√保存指纹。
AppGallery Connect 开通服务
- 打开AppGallery Connect,找到项目设置->API管理页面。
- 按照需求开启对应的API服务。
AppGallery Connect配置文件 添加
- 打开AppGallery Connect,找到项目设置->常规->应用部分。
- 下载
agconnect-services.json
配置文件。 - 将
agconnect-services.json
文件放置到Android工程的应用级根目录下。
Android工程Maven仓库 配置
- 打开项目级
build.gradle
文件并添加以下内容。
buildscript {
dependencies {
...
classpath 'com.android.tools.build:gradle:7.1'
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
}
}
- 打开项目级
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工程依赖 配置
- 将签名文件
xxx.jks
放置到应用级根目录下。 - 打开应用级
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,进行相关配置的同步。
参考资料
赞 (๑•̀ㅂ•́)و✧ 👍👍
在Android Studio Bumblebee版本中集成HMS Core的全流程如下:
-
安装Android Studio Bumblebee:确保已安装最新版本的Android Studio Bumblebee。
-
创建或打开项目:在Android Studio中创建新项目或打开现有项目。
-
配置Gradle文件:在项目的
build.gradle
文件中添加HMS Core的Maven仓库地址:repositories { maven { url 'https://developer.huawei.com/repo/' } }
-
添加HMS Core依赖:在
app
模块的build.gradle
文件中添加所需的HMS Core SDK依赖,例如:dependencies { implementation 'com.huawei.hms:location:6.0.0.300' }
-
同步项目:点击“Sync Now”同步项目,确保依赖项正确加载。
-
配置
agconnect-services.json
文件:从AppGallery Connect下载agconnect-services.json
文件,并将其放置在app
模块的根目录下。 -
初始化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); } }
-
配置权限:在
AndroidManifest.xml
文件中添加必要的权限,例如:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
-
使用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);
-
测试和调试:运行应用,确保HMS Core功能正常工作,并根据需要进行调试。
以上步骤完成了在Android Studio Bumblebee中集成HMS Core的全流程。
在Android Studio Bumblebee中集成HMS Core的步骤如下:
-
安装HMS Core插件:打开Android Studio,进入
File > Settings > Plugins
,搜索并安装HMS Core Toolkit
插件。 -
配置项目:在
build.gradle
文件中添加HMS Core依赖:dependencies { implementation 'com.huawei.hms:hwid:6.4.0.300' }
-
同步项目:点击
Sync Now
同步项目,确保依赖正确加载。 -
初始化HMS Core:在
Application
类中初始化HMS Core:HMSCore.init(this);
-
使用HMS服务:根据需求调用HMS Core提供的API,如账号登录、地图服务等。
完成以上步骤后,即可在应用中集成并使用HMS Core服务。