24

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.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
toddmo
  • 20,682
  • 14
  • 97
  • 107

3 Answers3

49

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

Talha Q
  • 4,350
  • 4
  • 28
  • 40
  • Always love to help ..Cheers :) – Talha Q Apr 14 '15 at 18:32
  • 1
    Oh, one followup question. For future versions, I need to be able to make my own jar file without depending on a dropbox url. How would I do that? – toddmo Apr 14 '15 at 22:15
  • wont you run into this issue ? http://stackoverflow.com/questions/8172454/android-market-this-application-is-available-to-over-0-devices – EK_AllDay Jul 06 '15 at 03:51
  • Awesome answeir, thnks! – Anton Kizema Jul 06 '15 at 11:04
  • @EthanKeiser i solved the issue by following this http://stackoverflow.com/questions/29565203/supported-devices-0-on-google-play – Talha Q Sep 27 '15 at 13:44
  • Why I can't find the .jar file on the official page? –  May 10 '17 at 15:26
  • How did you generate the signalr-client-sdk-android.JAR, should it not be an AAR? I tried to compile and I got issues with android.net.http.AndroidHttpClient not being found. – Lzh May 21 '17 at 12:52
  • @NathielBarros well they're not providing any compiled binaries (JARs or AARs). When compiling I get the signalr-client-sdk.jar, but I get an Android Archive (AAR) for the other. See my comment above... – Lzh May 21 '17 at 12:54
  • @TalhaQ:- what are the original source of signalr sdk jar files, which you providing in dropbox ? – JSB Aug 27 '17 at 17:39
  • @TalhaQ when installing the apk to devices api 26, I got the INSTALL_FAILED_NO_MATCHING_ABIS error. Even if I added splits - abi commands to the build.gradle it doesn't work. – a.u.b Mar 28 '18 at 11:57
18

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!

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • That's GREAT!..Can you please share with me the jar or aar files ? Thanks – Talha Q Apr 16 '15 at 10:13
  • @Johnny, ok, here's the [aar](https://app.box.com/s/k2m05ig0eg1qjey4ps6028ymtbdiijdc) from signalr-client-sdk-android commit `98ae5a47426da4d416a896c547080797bbafefae` – toddmo Apr 17 '15 at 01:42
  • @toddmo you make no mention of referencing `signalr-client-sdk.jar` - is this something you had to do too ? – wal Mar 22 '16 at 01:09
  • @wal i found it is also required to add signalr-client-sdk.jar, becasue Platform class is available only in sdk.jar. the answer is missing this. 1. places jar in project /libs/ 2. compile fileTree(include: ['*.jar'], dir: 'libs') under dependencies to include all jar automatically – Bamboo Mar 23 '16 at 06:58
  • @toddmo aar will give you AndroidPlatformComponent ref. you still need to additional steps to include signalr-client-sdk.jar to your project to get Platform ref. If you extract aar, then extract classes.jar inside, you will find it is missing Platform class. – Bamboo Mar 24 '16 at 06:34
  • @Bamboo, I have redone / rediscovered the process and edited my answer. Thank you. – toddmo May 30 '16 at 22:27
  • This is better than downloading some Jar from dropbox. signalr-client-sdk-android-release is indeed an AAR not JAR. – Lzh May 21 '17 at 11:40
  • What I'm wondering about is while I already had flatDir in my project's gradle, I had to duplicate it in the app's module gradle to get this to work! Any idea why? – Lzh May 21 '17 at 11:41
  • @Mzn, no sir, it's been too long since I was doing this to say. Try removing from the project's build.gradle and see what happens. – toddmo May 21 '17 at 23:51
  • It works when it's in the module's gradle and not in the project's gradle. It's already not in project's gradle. I think the explanation is simple: module gradle just doesn't inherit project gradle. thanks anyways – Lzh May 22 '17 at 00:13
0

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')