Thursday, March 14, 2013

Reference a JAR file

Roughly there are two types of JAR files: JAR files that act like a stub and JAR files that contain an actual implementation.

Stub JAR 

The Google maps API (v1) is a good example of a stub JAR. The maps funtionality is implemented elsewhere and the stub basically forwards calls to it.

We already support this type of JAR file. When you right-click your project in Visual Studio, you will see a menu item 'Add Jar Reference...'. This will generate some C# code in the background that reflects the API of the stub JAR. You can code against it and dot42 will link this accordingly during compilation. In order to link to the actual implementation, you had to enter the library name in the "Add Jar Reference..." dialog. This library name is used in the generated C# code in an assembly attribute like this:
[assembly: UsesLibrary("com.google.android.maps")]
This assembly attribute is the equivalent of the uses-library element as in the following application's manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.package.name">
  ...
  <application android:name="MyApplication" >
    <uses-library android:name="com.google.android.maps" />
    ...
  </application>
  ...
</manifest>

Implementation JAR

The second type contains an actual implementation. The Google Cloud Message JAR (gcm.jar) falls under this category. Unfortunately we do not support this one yet. The good news is that it is not on our 5-years roadmap but is being implemented right now. It involves converting Java byte code to Dex just like we already convert IL code to Dex. We expect to release it in the next 4 weeks.

How to determine the type of JAR?

So, how to tell what type a given JAR file is? dot42 ships with a handy tool: dot42ApkSpy. Although its name suggests differently, it allows you to inspect both APK and JAR files. You can find this tool at "c:\Program files\dot42\dot42ApkSpy.exe".

When we open the Google Cloud Messaging gcm.jar and navigate to onReceive, we see that there is actual code:


So, this is a JAR that contains the actual implementation.

But if we then open the Google Maps maps.jar, and navigate to method Overlay.draw, we see the following:


It has no implementation. It just throws a runtime exception. Also note the "stub" in the code. This is a stub JAR.


5 comments:

  1. How does the referenced implementation JAR get deployed to the client?

    ReplyDelete
    Replies
    1. Actually, JARs are not deployed to the client, only APKs. How to ensure that the corresponding APK is deployed to the client depends on the APK itself. E.g., for Google Play Services, you can only check if the APK is present. If not, you can prompt the user to update Google Play on your device. On Android there is no way to include all dependent libs in your APK (as far as we know atleast).

      By the way, if your app requires certain libs that are not present on your device, Google Play will not show your app. This is referred to as Filters. Read more:
      http://developer.android.com/google/play/filters.html

      Delete
  2. Is the "Implementation Jar" finally working?
    I cant get it to work.

    ReplyDelete
  3. This should work (as of version 1.0.1.77, released September 4, 2013).

    Which JAR are you referencing?

    ReplyDelete
    Replies
    1. osmdroid, i do not get an error message,
      it is just when i debug into a demo project it can't get past the osmdroid calls when it comes to instantiating the osmdroid object.

      thanks

      Delete