一些公司的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'
...
}
“?.”是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: '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;





































苏公安网备 32011402010933号