Steps to get SignalR working in Android Studio

2019-01-13 08:28发布

I'm trying to bring SignalR into my Android Studio project.

I successfully followed the tutorial on getting started with SignalR, so now I have a working Hub. The client in that tutorial is javascript, and I got it working ok in a web page (in Chrome) on PC AND on my Android phone over WiFi on my home LAN.

Then I went to go get the Android java client working. I already had an Android app project in Android Studio, which I want to make talk to that working Hub as a test, as the next step.

I downloaded the official Microsoft java client for Android, as a zip.

Then I extracted it and loaded it as a project into Android Studio. It builds with no errors.

Problem #1: This project only gives me an AAR, not a JAR. I have figured out how to use a jar in Android Studio, so if there's a way to create a jar at this juncture, I can probably make this work.

At this point I included the dependencies into my app project. The readme.md says to:

Add the signalr-client-sdk.jar, signalr-client-sdk-android.jar gson library as a gradle dependency to the project.

so I tried to follow that as closely as possible. Here's from my build.gradle:

compile 'com.google.code.gson:gson:2.3.1'
compile files('libs/signalr-client-sdk.jar')
compile files('libs/signalr-client-sdk-android-release.aar')

Problem #2: Having imported the aar as a module into my app project in Android Studio, it still does not see the necessary namespace to make the primary statement work. Here is the primary statement (the hello world of SignalR in Android, compiler-wise):

Platform.loadPlatformComponent(new AndroidPlatformComponent());

At first it wasn't picking up anything, then it picked up import microsoft.aspnet.signalr.client.Platform; for Platform. It still does not have a clue what to import for AndroidPlatformComponent, so I'm stuck until I can get past this.

As you know there are a lot of files in Android Studio, and I don't want to just spew them all out here before I know which files you want to see. I'll add them as an edit if you tell me which files you want to see.

3条回答
乱世女痞
2楼-- · 2019-01-13 09:00

I also got this working with just Android Studio and the files from the GitHub project but my gradle dependencies are set like this:

            implementation files('libs/signalr-client-sdk.jar')
            implementation files('libs/signalr-client-sdk-android.aar')
查看更多
狗以群分
3楼-- · 2019-01-13 09:11

Well you can download the jar files from this link

Step 1

Create a libs folder inside your package and simply paste these jar files here.

enter image description here

Step 2

Now add "compile files" inside dependencies of build.gradle

enter image description here

Now after this press "Sync Project with Gradle file" enter image description here

Step 3

Even after syncing and importing jar successfully you still face some import errors etc. Now try Invalidate cache and restart.After couple of times it will be fine and imports all the classes

enter image description here

Step 4

Now you will see the imports for Platform.loadPlatformComponent(); without any problems

enter image description here enter image description here

查看更多
相关推荐>>
4楼-- · 2019-01-13 09:13

I got this working with just Android Studio and the files from the GitHub project.

Here's what I had to do, for posterity's sake.

  1. Extract the zip file from github
  2. Open the contents in Android Studio as a separate project, and Build. It produces:
    • signalr-client-sdk-android-debug.aar in java-client\signalr-client-sdk-android\build\outputs\aar
    • signalr-client-sdk.jar in java-client\signalr-client-sdk\build\libs
  3. Copy these 2 files into the libs folder of your app.
  4. Go into module's build.gradle and add:
repositories {
    flatDir{
        dirs 'libs'
    }
}
dependencies {
    compile 'com.google.code.gson:gson:2.3.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(name: 'signalr-client-sdk-android-release', ext: 'aar')
}
  1. Tools, Android, Sync Project with Gradle Files, and when that was successful I did a Build.

  2. Then finally, this line of code gave me the ALT+Enter prompt to generate the imports for this line of code:

Platform.loadPlatformComponent(new AndroidPlatformComponent());

which gave me:

import microsoft.aspnet.signalr.client.Platform;
import microsoft.aspnet.signalr.client.http.android.AndroidPlatformComponent;

Whew!

查看更多
登录 后发表回答