0

There is many subject on stack regarding this, but none of them worked :/

I'm running android studio, i added this librarie in gradle, then gradle clean, built project and run app.

Unfortunately each time i run the app it fail

Process: com.example.gta.fab1, PID: 5775
            java.lang.NoClassDefFoundError: com.loopj.android.http.AsyncHttpClient
                at com.example.gta.fab1.TabFragments.WhoFragment.uploadToServer(WhoFragment.java:129)
                at com.example.gta.fab1.TabFragments.WhoFragment.access$100(WhoFragment.java:41)
                at com.example.gta.fab1.TabFragments.WhoFragment$1.onClick(WhoFragment.java:85)
                at com.facebook.FacebookButtonBase.callExternalOnClickListener(FacebookButtonBase.java:216)
                at com.facebook.share.widget.ShareButtonBase.access$000(ShareButtonBase.java:39)
                at com.facebook.share.widget.ShareButtonBase$1.onClick(ShareButtonBase.java:167)
                at com.facebook.FacebookButtonBase$1.onClick(FacebookButtonBase.java:385)
                at android.view.View.performClick(View.java:4438)
                at android.view.View$PerformClick.run(View.java:18422)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5001)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                at dalvik.system.NativeStart.main(Native Method)

my gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.gta.fab1"
        multiDexEnabled true
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.loopj.android:android-async-http:1.4.9'
}

My class:

package com.example.gta.fab1.TabFragments;


import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.loopj.android.http.*;

import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;
import cz.msebera.android.httpclient.Header;


public class WhoFragment extends Fragment {

    CallbackManager callbackManager;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(
                R.layout.who_fragment, container, false);

                JSONObject post_json = jSonBuilder(facebook_id, activity);
                try{
                    uploadToServer(post_json);
                    Log.d("output", post_json.toString(2));
                } catch(java.io.IOException | org.json.JSONException e){
                    throw new RuntimeException(e);
                }

        return mRelativeLayout;
    }

    private JSONObject jSonBuilder(String fb_id,FragmentMain activity ){
        JSONObject parent = new JSONObject();

        try{
            parent.put("user_fb_id",fb_id);
            parent.put("locations", activity.serialize.getArrayOfRestaurants());
            parent.put("time_propositions", activity.serialize.getArrayOfTimeSchedule());
            return parent;
        }catch(org.json.JSONException e){
            throw new RuntimeException(e);
        }
    }

    private void uploadToServer(JSONObject post_json) throws IOException, org.json.JSONException {

        AsyncHttpClient client = new AsyncHttpClient();

        String url = "http://myurl/api/postjson/1";
        StringEntity se = null;
        try {
            se = new StringEntity(post_json.toString());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

        client.post(null, url, se, "application/json", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
                System.out.print("success");
            }

            @Override
            public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) {
                System.out.print("fail");
            }
        });
    }
}

Some answer were talking about adding manually the jar lib and referencing it in gradle.build, but didn't worked either, on the website of the plugin, the documentation is clear :

https://guides.codepath.com/android/Using-Android-Async-Http-Client

I don't see my mistake here :/

Thanks a lot in advance

EDIT: importing the jar file into the libs folder then, add to library, and importing the libs for the header manually in gradle:

compile 'cz.msebera.android:httpclient:4.3.+'

give different error:

java.lang.NoClassDefFoundError: com.loopj.android.http.LogHandler

There must a bug :( i don't see any other options

EDIT 2: Works fine on API 21..., is this library incompatible with API 19 ??

RaccoonDeveloper
  • 224
  • 2
  • 11

2 Answers2

2
  1. Enable multidex

    defaultConfig {
    ...
     // Enabling multidex support.
    multiDexEnabled true
    }
    
  2. Add dependency

    dependencies {
        compile 'com.android.support:multidex:1.0.1'
    }
    
  3. Add codes in your application

    @Override
    protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
    }
    

That worked for me, i hope it is helpful for you.

0

It may be related to multiDex feature.

You can disable it by removing "multiDexEnabled true" line in your gradle file.

Or follow the multidex guide:

  1. Set multiDexEnabled true
  2. Add dependency 'com.android.support:multidex:1.0.0'
  3. Replace Application to MultiDexApplication
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Richard Chang
  • 281
  • 2
  • 10
  • A missing dependency has nothing to do with Multidex – OneCricketeer Apr 11 '16 at 18:00
  • I would like to mention that after multiple attempts, i just noticed that its not throwing any exception on API 21 ... Is this library incompatible with API 19 ?? – RaccoonDeveloper Apr 11 '16 at 18:10
  • Did you setup multidex as I said? ART (Android 5.0) default supports multidex but Android 4.4 doesn't. – Richard Chang Apr 11 '16 at 18:20
  • Hi Richard, thanks for your answer but actually it was needed this param otherwise it wouldn't even build the project, anyway now that i know the answer about what was the problem, i can give up properly on ever using this library – RaccoonDeveloper Apr 11 '16 at 18:32
  • Hi @RaccoonDeveloper, I think you misunderstood the description. They said "Compatible with Android API 23 and higher" is because Google removed some apache http apis which loopj used before 1.4.8. So 1.4.9 is a whole new package which built in the apache http apis. Btw, you said API21 is fine, right? – Richard Chang Apr 11 '16 at 18:47
  • @RichardChang: Yes its fine on API 21, but my app needs to target much wider than api 21, the bare minimum would have been 17 or 19, not 21 :/ – RaccoonDeveloper Apr 11 '16 at 18:49
  • Please see my answer again, you need to follow this to setup your multidex (for api 19 or lower): https://developer.android.com/intl/zh-tw/tools/building/multidex.html – Richard Chang Apr 11 '16 at 18:53
  • @RichardChang: Ok, will check that tomorrow first hour, its already quite late in france ;) thx for ur suggestion, i let you know asap tomorrow morning – RaccoonDeveloper Apr 11 '16 at 19:04