Search code examples
androidkotlinbinance

Does Binance Api support Kotlin?


I'm trying to developing a mobile application in kotlin about binance. I have problem with Binance api issues.Does Binance Api support Kotlin aswell like Java?

I mean I want to develop that project on binance and when I asked to chatGPT for help.It gives me an implementation and some codes. Implementation is implementation 'com.binance.api:binance-java-api:1.1.10' and the code is:

import com.binance.api.client.BinanceApiClientFactory

import com.binance.api.client.BinanceApiRestClient

val apiKey = "YOUR_API_KEY"
val secretKey = "YOUR_SECRET_KEY"

val factory = BinanceApiClientFactory.newInstance(apiKey, secretKey)
val client = factory.newRestClient()

I was just trying to do a simple connection for binance with api key and secret key in my app.But with that implemantation I mention above I can't import this two libraries

import com.binance.api.client.BinanceApiClientFactory

import com.binance.api.client.BinanceApiRestClient

That's why the rest of the code also gives an error.

What I tried:

  • I tried to adding maven as dependencies to build.gradle but it didn't effect (I don't know if I should add.It is one of ChatGPT's advice)

  • I also tried to this two libraries as external.I mean I found that two import library in this github repo https://github.com/binance-exchange/binance-java-api .It's for Java but I guess Kotlin supports Java Libraries too.That's why I added this two import libraries to libs directory like below.But imports still didn't effected. enter image description
here

  • Lastly I tried retrofit for that. I also heard some developers did it with that way.

interface BinanceService

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

interface BinanceService {
    @GET("api/v3/account")
    fun getAccountInfo(
        @Query("apiKey") apiKey: String,
        @Query("secretKey") secretKey: String
    ): Call<AccountInfoResponse>
}

AccountInfoResponse data class

data class AccountInfoResponse(
    @SerializedName("balances")
    val balances: List<AssetBalance>,
)

data class AssetBalance(
    @SerializedName("asset")
    val asset: String,
    @SerializedName("free")
    val free: String,
    @SerializedName("locked")
    val locked: String
)

Usage in code is like this also

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

val apiKey = "YOUR_API_KEY"
val secretKey = "YOUR_SECRET_KEY"

val retrofit = Retrofit.Builder()
    .baseUrl("https://api.binance.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build()

val binanceService = retrofit.create(BinanceService::class.java)
val call = binanceService.getAccountInfo(apiKey, secretKey)

call.enqueue(object : Callback<AccountInfoResponse> {
    override fun onResponse(call: Call<AccountInfoResponse>, response: Response<AccountInfoResponse>) {
        val accountInfo = response.body()
       
    }

    override fun onFailure(call: Call<AccountInfoResponse>, t: Throwable) {
       
    }
})

Here is my build.gradle (app) file

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android-extensions'
}

android {
    namespace 'com.example.binancetradebot'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.binancetradebot"
        minSdk 22
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

}



def lottieVersion = "3.7.0"
dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation "com.airbnb.android:lottie:$lottieVersion"
    implementation 'com.binance.api:binance-java-api:1.1.10'
    implementation files('libs/binance-api-1.0.9.jar')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

But with that code the response always returns null even my connection and keys are true.

So I have questions

  1. Can I do that this app with kotlin?
  2. If I can do which path I should try? (implementation way, adding Java library or retrofit usage)
  3. And if I select one of them what is my wrong about it?

Solution

  • Basically if the binance-java-api is not in the Maven Central or any other repository that you are currently using, you won't be able to import it. Is seems like a chatGPT hallucination.

    If you want to use https://github.com/binance-exchange/binance-java-api you could do a couple of things:

    Or

    • You could package the binance-java-api as a .jar file and copy it to the libs directory in your app and include it in your build.gradle. There are a lot of resources on how to do that.

       implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
      

    Or simply go to File > Project Structure > Dependencies and import the .jar from there.

    Here is the dialog: Add local dependencies in Android Studio