semiColon
semiColon

Reputation: 201

Connecting Azure to Android Studios

I am trying to connect Azure to Android Studios. I have followed the tutorial on Azure and created a mobile service. It then gave me some code to add to my app so that they are connected. However, the code is throwing an exception which I cannot figure out. I have searched the internet from head to toe and cannot find an answer. I think it has something to do with the dependencies or the way the library was imported (which was by pasting a jar file into the libs folder and adding a dependency).

The code throwing causing the exception (provided by Azure):

try {
        mClient = new MobileServiceClient(
                "https://atm.azure-mobile.net/",
                "JWjetFMUVaAXzHmqDVqhkkRhTGGjeW70",
                this
        );
        Item item = new Item();
        item.Text = "Awesome item";
        mClient.getTable(Item.class).insert(item, new TableOperationCallback<Item>() {
            public void onCompleted(Item entity, Exception exception, ServiceFilterResponse response) {
                if (exception == null) {
                    // Insert succeeded
                    Toast.makeText(context, "WOOHOO", Toast.LENGTH_LONG).show();
                } else {
                    // Insert failed
                    Toast.makeText(context, "FAILED", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    catch(MalformedURLException d) {
    }

The exception:

java.lang.NoClassDefFoundError: com.google.gson.GsonBuilder
        at com.microsoft.windowsazure.mobileservices.MobileServiceClient.createMobileServiceGsonBuilder(MobileServiceClient.java:192)
        at com.microsoft.windowsazure.mobileservices.MobileServiceClient.<init>(MobileServiceClient.java:179)
        at com.microsoft.windowsazure.mobileservices.MobileServiceClient.<init>(MobileServiceClient.java:158)
        at com.atmlocator.hooper.kenneth.atmlocator.HomeActivity.onCreate(HomeActivity.java:218)
        at android.app.Activity.performCreate(Activity.java:5047)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2051)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2112)
        at android.app.ActivityThread.access$700(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4917)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
        at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 2757

Answers (2)

semiColon
semiColon

Reputation: 201

I have solved the problem by updating the .jar file. Although the Microsoft documentation told me to get:

'gson:2.2.2'

This version does not work. Instead I downloaded a newer version:

'gson:2.3.1'

This .jar file solves the issue

Upvotes: 1

Peter Pan
Peter Pan

Reputation: 24138

Per my experience, the exception "NoClassDefFoundError" means your project missing a dependency library jar file. At here, your project miss gson.jar file.

You can download a zip file "azuresdk-android-2.0.3.zip" from the section "Mobile" -> "Android install" of the page https://azure.microsoft.com/en-us/downloads/. After decompress the zip file to a directory, you can find the "gson-2.2.2.jar" file and other dependency jar files in the directory "azuresdk-android-2.0.3/mobileservices".

Then you can import these jar library files into directory "libs" of your project to resolve the issue. Please refer to the SO thread Android Studio - Importing external Library/Jar to learn how to import external library/jar.

Best Regards.

Upvotes: 1

Related Questions