文档开发者中心
我的

APP采用KotLin开发,开发者如何在KotLin中调用HMS Core的相关能力



一些公司的APP采用KotLin开发,那么开发者如何在KotLin中调用HMS Core的相关能力呢?

本帖将Codelab中Location和In-App Purchase的demo使用Kotlin进行了重写,并在Kotlin中进行了HMS Core的调用。


具体验证步骤如下:


关键步骤:


1、给现有项目添加 Kotlin 支持


只需要按照下面加粗的代码分别修改两个build.gradle即可,修改完以后,同步项目就可以开始使用Kotlin进行开发了。


  • 项目根目录下的 build.gradle:


buildscript {
ext.kotlin_version = '1.3.41'
repositories {
...
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-beta05'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version'
}
}


  • app 目录下的 build.gradle:


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
...
android {
...
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version'
...
}


2、调用JAVA代码 (示例代码:Location Kit中创建位置信息请求体)


“?.”是Kotlin中的中特有的一个操作符, 执行安全调用(如果接收者非空,就调用一个方法或访问一个属性)可以有效的避免空指针异常,Kotlin调用HMS SDK和Java调用


mLocationRequest = LocationRequest()
// Set the interval for location updates, in milliseconds.
mLocationRequest?.interval = 10000
// set the priority of the request
mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY



验证结果:通过,开发者只需遵循Kotlin官网规范,即可正常调用HMS相关能力;



Case background:


In the overseas DTSE technical support, part of the head APP is developed by KotLin. The developer consults how to invoke the relevant capabilities of HMS Core in KotLin to support the overseas DTSE. In this case, the demo of Location and in-app Purchase In Codelab was rewritten using Kotlin, and HMS Core was called In Kotlin. The specific verification steps are as follow


Key steps:


1. Add Kotlin support to existing projects
All you need to do is modify two build.gradle's in bold below, and after that, the synchronization project can be developed using Kotlin.

  • Project root directory’s build.gradel



buildscript {

ext.kotlin_version = '1.3.41'
repositories {
...
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-beta05'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version'
}
}



  • app directory’s build.gradel



apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
...
android {
...
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version'
...
}


2. Call JAVA code (example code: create the request body for Location information in Location Kit)


'?'Is a special operator in Kotlin, which can effectively avoid null pointer exceptions by performing security calls (calling a method or accessing a property if the receiver is not null). Kotlin calls HMS SDK and Java calls are very similar, the main difference is the syntax difference between Kotlin and Java, which is as follows:


mLocationRequest = LocationRequest()
// Set the interval for location updates, in milliseconds.
mLocationRequest?.interval = 10000
// set the priority of the request

mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY


Verification results: OK, developers only need to follow the Kotlin official website specification to properly invoke HMS related capabilities;

点赞
收藏
回复
分享
举报
浏览2619 发布于2019-10-17 07:55未知归属地
全部评论
最多点赞
最新发布
最早发布
暂无评论
暂无评论
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。