前言:
最近在学习kotlin 语言 然后因为工作是做手游SDK 开发的 之前都是用Java语言开发 现在想用kotlin 尝试一下
效果图:
具体实现:
-
sdk 源码:
对外接入类封装
package com.app.gamesdkkottin
import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.app.gamesdkkottin.bean.DataStore
import com.app.gamesdkkottin.bean.RoleInfo
import com.app.gamesdkkottin.listener.LoginListener
import com.app.gamesdkkottin.listener.LoginOutListener
import com.app.gamesdkkottin.listener.RoleinfoListener
import com.app.gamesdkkottin.pupopwindow.ExitDidlog
import com.app.gamesdkkottin.ui.LoginActivity
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.StringCallback
import okhttp3.Call
import org.json.JSONObject
class NinefunSdk private constructor(){
val TAG="NinefunSdk"
companion object{
private var ninefunSdk:NinefunSdk?=null
val instance: NinefunSdk?
get() {
if (ninefunSdk == null) {
synchronized(NinefunSdk::class.java) {
if (ninefunSdk == null) {
ninefunSdk = NinefunSdk()
}
}
}
return ninefunSdk
}
}
fun checklogin(activity: Activity,loginListener: LoginListener){
DataStore.instance!!.loginListener=loginListener
var token: String? = SharedPreferenceUtil.getPreference(activity ,"token","") as String?
Log.e(TAG,"token --- > "+token)
if(!TextUtils.isEmpty(token)){
tokenSign(activity,token!!,loginListener)
}else {
var intent:Intent= Intent(activity, LoginActivity::class.java)
activity!!.startActivity(intent)
}
}
fun exit(activity: Activity){
var dialog:ExitDidlog= ExitDidlog(activity)
dialog.show()
}
fun subimtData(roleInfo: RoleInfo,roleinfoListener: RoleinfoListener){
val preSign = StringBuilder()
preSign.append("sid=").append(roleInfo.sid)
preSign.append("&role_id=").append(roleInfo.role_id)
preSign.append("&role_name=").append(roleInfo.role_name)
preSign.append("||").append("0a649d6b-df9f-4583-bf7d-3fae84838519")
val sign = MD5Tool.calcMD5(preSign.toString().toByteArray())
Log.e(TAG,"sign ---- >"+sign)
OkHttpUtils.post().url(Config.SUBMI_DATA)
.addParams("gid",roleInfo.gid)
.addParams("sid",roleInfo.sid)
.addParams("role_name",roleInfo.role_name)
.addParams("role_level",roleInfo.role_level)
.addParams("role_id",roleInfo.role_id)
.addParams("sign",sign)
.build()
.execute(object : StringCallback() {
override fun onResponse(response: String?, id: Int) {
Log.e(TAG,response!!)
roleinfoListener!!.loginSuccess(response)
}
override fun onError(call: Call?, e: Exception?, id: Int) {
roleinfoListener!!.loginError()
}
})
}
fun tokenSign(context:Activity,token:String ,loginListener: LoginListener){
OkHttpUtils.get().url(Config.TOKEN_SIGN)
.addParams("token",token)
.build()
.execute(object : StringCallback(){
override fun onResponse(response: String?, id: Int) {
Log.e(TAG,"response---- > " + response )
var jsonObject:JSONObject= JSONObject(response)
var code:Int=jsonObject!!.optInt("code")
var msg:String=jsonObject.optString("msg")
if(code==200){
loginListener.loginSuccess(response!!)
}else{
Toast.makeText(context,msg, Toast.LENGTH_SHORT).show()
}
}
override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
}
})
}
fun loginout(activity: Activity,loginOutListener: LoginOutListener){
SharedPreferenceUtil.removeParam(activity,"token")
loginOutListener.loginSuccess()
}
fun pay(){
}
}
为了方便演示接入流程 我这边就简化了 只写了几对外方法 init login pay exit 主要的几个对外接口
-
打包aar
我们在app这一层的build.gradle 里面的配置 apply plugin: 'com.android.application ' 改成 apply plugin: 'com.android.library'
然后将我们的 applicationid这行配置注释 否则我们将无法打包aar
我们在终端执行 gradlew assembleRelease
-
aar 包位置
接入
-
将aar复制到libs目录下
-
在build.gradle 里面添加如下配置
repositories{
flatDir {
dirs 'libs'
}
}
-
如图
-
然后依赖aar
implementation (name: 'game',ext: 'aar')
-
具体接入代码
package com.app.ktdemoone
import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.app.gamesdkkottin.NinefunSdk
import com.app.gamesdkkottin.bean.RoleInfo
import com.app.gamesdkkottin.listener.LoginListener
import com.app.gamesdkkottin.listener.LoginOutListener
import com.app.gamesdkkottin.listener.RoleinfoListener
class TestActivity : AppCompatActivity() ,View.OnClickListener{
var TAG="MainActivity"
private var initBtn: Button?=null
private var loginBtn: Button?=null
private var payBtn: Button?=null
private var exitBtn: Button?=null
private var activity: Activity =this
private var submitdata: Button?=null
private var loginout: Button?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
initData()
}
private fun initData() {
initBtn=findViewById(R.id.init)
loginBtn=findViewById(R.id.login)
payBtn=findViewById(R.id.pay)
exitBtn=findViewById(R.id.exit)
submitdata=findViewById(R.id.submit_data)
loginout=findViewById(R.id.login_out)
submitdata!!.setOnClickListener(this)
initBtn!!.setOnClickListener(this)
loginBtn!!.setOnClickListener(this)
payBtn!!.setOnClickListener(this)
exitBtn!!.setOnClickListener(this)
}
override fun onClick(v: View?) {
when(v?.id) {
R.id.init ->
Toast.makeText(this, "初始化", Toast.LENGTH_SHORT).show()
R.id.login ->
NinefunSdk.instance!!.checklogin(activity,loginListener)
R.id.pay ->
Toast.makeText(this, "支付", Toast.LENGTH_SHORT).show()
R.id.exit ->
// Toast.makeText(this, "退出", Toast.LENGTH_SHORT).show()
NinefunSdk.instance!!.exit(activity)
R.id.submit_data ->
/* Toast.makeText(this,"数据上报",Toast.LENGTH_SHORT).show()*/
submitData()
R.id.login_out ->
NinefunSdk.instance!!.loginout(activity,object : LoginOutListener {
override fun loginSuccess() {
NinefunSdk.instance!!.checklogin(activity ,loginListener)
}
})
}
}
var loginListener : LoginListener =object :LoginListener {
override fun loginSuccess(data: String) {
Log.e(TAG, "data --- > " + data)
}
override fun loginError() {
}
}
fun submitData(){
var roleInfo : RoleInfo = RoleInfo()
roleInfo.gid="1"
roleInfo.sid="2"
roleInfo.role_id="56"
roleInfo.role_level="20"
roleInfo.role_name="宇智波斑"
NinefunSdk.instance!!.subimtData(roleInfo,object : RoleinfoListener {
override fun loginSuccess(data: String) {
Log.e(TAG,"data-- >"+data)
}
override fun loginError() {
}
})
}
}
到此kotlin 开发手游·SDK 就讲解完了 那些细节UI界面的代码我就没有贴上来 有兴趣的同学可以去码云仓库去检出我的代码
最后总结
使用kotlin开发手游sdk 和Java 开发手游SDK差别并不大 官方宣称是2种语言可以互相调用的 理论上我们接入直接用Java去接也会可以 但是我封装对外接口类写法有点问题 后期改一下尝试下 如果有兴趣的同学可以下载代码自己去看详细代码 最后希望我的文章能帮助到各位解决问题 ,以后我还会贡献更多有用的代码分享给大家。各位同学如果觉得文章还不错 ,麻烦给关注和star,小弟在这里谢过啦!
项目地址:
https://gitee.com/qiuyu123/kotlingame_sdk