Xamarin bindings for the twitter android sdk
In this document we document how we got very rough but working Xamarin bindings for the twitter android sdk core. Disclaimer: this is probably not how you do it.
Do not use these bindings directly. They are really quick and really dirty. If you still want to:
- Clone this repository into an arbitrary folder.
- Select your solution in visual studio.
- File -> Add -> Existing Project -> Choose this .csproj file.
- Add a reference in your android project.
Xamarin Android has to use a jdk with at least version 1.7.
The main reference is this guide.
We created a new project from the "Xamarin Bindings library" project.
We located the relevant java archive at a private twitter maven repository. The java archive is hidden in another archive (ending in .aar but actually just a .zip archive). In this archive there is a classes.jar file that we extracted and renamed to twitter-core-1.4.0.jar.
In the maven repository for twitter core we looked at the pom file and manually and transitively chased dependencies on other artifacts. This got us gson-2.2.4.jar, fabric-1.3.4.jar and retrofit-1.6.1.jar. We noticed that for some maven artifacts some dependencies are optional and omitted all of those.
We added all four jars to the Jars folder in our project. We chose build action EmbeddedJar for twitter-core-1.4.0.jar and fabric-1.3.4.jar and build action EmbeddedReferenceJar for gson-2.2.4.jar and retrofit-1.6.1.jar. Contrary to what the accompanying AboutJars.txt says we did not choose InputJar and ReferenceJar. We did not know beforehand that we would have to directly use fabric. Only later we set its build action from EmbeddedReferenceJar to EmbeddedJar to generate bindings for it as well.
The errors we had to fix fall into four categories:
- Methods with the same name as the class. Easy to fix by specifying a renaming in
Metadata.xmllike so:<attr path="/api/package[@name='com.twitter.sdk.android.core.models']/class[@name='Coordinates']/field[@name='coordinates']" name="name">Coordinats</attr> - Some classes were private but should be public. Easy to fix by overriding the visibility in
Metadata.xml:<attr path="/api/package[@name='com.twitter.sdk.android.core.models']/class[@name='Entity']" name="visibility">public</attr> - An interface was missing. We created an empty interface in
Additions/IAppSpiCall.cs. - An overriding method returned
ICollection<ITask>while the overriden method returnedICollection. We added a method that does an explicit cast inAdditions/PriorityTask.cs.
It was helpful that twitter-kit-android is on github. Fixing build errors for fabric was more difficult and involved inspecting its jar because we did not have the source code. The Metadata.xml language has a reference.
Twitter core for android needs resources. They are present in the jar in a res folder. When building an android apk the build tool usually generates and compiles an R.java file and also puts it into the jar. Unfourtunately twitter-core-1.4.0.jar was missing R.class and several related classes like R$layout.class. It did feature an R.txt with the same information as R.java would have, probably for other build tools.
What we had to do is add twitter-corre-1.4.0 as a library project that includes its resources. We created an archive twitter-core-1.4.0.zip with the following structure:
twitter-core-1.4.0.zip
|_bin
|_classes.jar
|_AndroidMainfest.xml
|_res
|_layout
|_ ...
|_drawable
|_ ...
|_ ...
The res folder and AndroidManifest.xml are from the original twitter-core-1.4.0.aar. classes.jar is the original twitter core classes.jar.
In Visual Studio we chose LibraryProjectZip as build action for twitter-core-1.4.0.zip.