简体   繁体   中英

Running Android app produces java.lang.NoClassDefFoundError: retrofit.RestAdapter$Builder

I am making a photo gallery app using the Flickr API on Android (Using Eclipse and 4.4 SDK). The project is successfully compiled but the app crashes as soon as it launches on Nexus 7 (2012) again running 4.4.

Included in my build path are the following jars: retrofit-1.6.1 okhttp-2.0.0 gson-2.2.4 picasso-2.3.2

Here are the relevant files:

GalleryApp.java

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import utils.Constants;
import android.app.Application;
import apiService.FlickrService;

public class GalleryApp extends Application {

    private FlickrService mFlickrService;

    public synchronized FlickrService getFlickrService() {

        if (mFlickrService == null) {

            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(FlickrService.FLICKR_API_URL)
                    .setRequestInterceptor(new RequestInterceptor() {
                        @Override
                        public void intercept(RequestFacade requestFacade) {
                            requestFacade.addQueryParam("format", "json");
                            requestFacade.addQueryParam("api_key", FlickrService.FLICKR_API_KEY);

                            requestFacade.addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.ACCEPT_APPLICATION_JSON);
                        }
                    })
                    .build();
            /*
            RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(FlickrService.FLICKR_API_URL)
            //.setConverter(new GsonConverter(GsonUtil.getGsonInstance()))
                    .setClient(new OkClient())
                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .setRequestInterceptor(new RequestInterceptor() {

                        @Override
                        public void intercept(RequestFacade requestFacade) {
                            // Get responses in JSON
                            requestFacade.addQueryParam("format", "json");
                            requestFacade.addQueryParam("api_key", FlickrService.FLICKR_API_KEY);

                            requestFacade.addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.ACCEPT_APPLICATION_JSON);
                        }
                    })
                    .build();
            */
            mFlickrService = restAdapter.create(FlickrService.class);
        }
        return mFlickrService;
    }
}

FlickrService.java

import models.PhotoResponse;
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Query;

public interface FlickrService {

    public static final String FLICKR_API_KEY = "dcf5dc19f9d4e86ead9722a634907e6e";
    public static final String FLICKR_API_URL = "http://api.flickr.com/services/rest";

    public static final String NO_JSON_CALLBACK = "&nojsoncallback=1";

    @GET("/?method=flickr.photos.search&extras=original_format" + NO_JSON_CALLBACK)
    void searchPhotos(@Query("text") String searchText, @Query("per_page") String perPage,
                      @Query("page") String page, Callback<PhotoResponse> cb);
}

Full Error Log

07-10 18:41:06.535: E/dalvikvm(3164): Could not find class 'retrofit.RestAdapter$Builder', referenced from method com.example.retrofitgallery2.GalleryApp.getFlickrService
07-10 18:41:06.535: W/dalvikvm(3164): VFY: unable to resolve new-instance 1138 (Lretrofit/RestAdapter$Builder;) in Lcom/example/retrofitgallery2/GalleryApp;
07-10 18:41:06.535: D/dalvikvm(3164): VFY: replacing opcode 0x22 at 0x0005
07-10 18:41:06.535: D/dalvikvm(3164): DexOpt: unable to opt direct call 0x2243 at 0x07 in Lcom/example/retrofitgallery2/GalleryApp;.getFlickrService
07-10 18:41:06.535: D/dalvikvm(3164): DexOpt: unable to opt direct call 0x224a at 0x12 in Lcom/example/retrofitgallery2/GalleryApp;.getFlickrService
07-10 18:41:06.535: I/dalvikvm(3164): Failed resolving Lcom/example/retrofitgallery2/GalleryApp$1; interface 1137 'Lretrofit/RequestInterceptor;'
07-10 18:41:06.535: W/dalvikvm(3164): Link of class 'Lcom/example/retrofitgallery2/GalleryApp$1;' failed
07-10 18:41:06.535: D/dalvikvm(3164): DexOpt: unable to opt direct call 0x20f3 at 0x1b in Lcom/example/retrofitgallery2/GalleryApp;.getFlickrService
07-10 18:41:06.535: I/dalvikvm(3164): Failed resolving Lviewer/MainActivity$1; interface 1135 'Lretrofit/Callback;'
07-10 18:41:06.535: W/dalvikvm(3164): Link of class 'Lviewer/MainActivity$1;' failed
07-10 18:41:06.535: E/dalvikvm(3164): Could not find class 'viewer.MainActivity$1', referenced from method viewer.MainActivity.onCreate
07-10 18:41:06.535: W/dalvikvm(3164): VFY: unable to resolve new-instance 1149 (Lviewer/MainActivity$1;) in Lviewer/MainActivity;
07-10 18:41:06.535: D/dalvikvm(3164): VFY: replacing opcode 0x22 at 0x0027
07-10 18:41:06.535: I/dalvikvm(3164): Failed resolving Lviewer/MainActivity$1; interface 1135 'Lretrofit/Callback;'
07-10 18:41:06.535: W/dalvikvm(3164): Link of class 'Lviewer/MainActivity$1;' failed
07-10 18:41:06.535: D/dalvikvm(3164): DexOpt: unable to opt direct call 0x2258 at 0x29 in Lviewer/MainActivity;.onCreate
07-10 18:41:06.535: D/AndroidRuntime(3164): Shutting down VM
07-10 18:41:06.535: W/dalvikvm(3164): threadid=1: thread exiting with uncaught exception (group=0x41854ba8)
07-10 18:41:06.545: E/AndroidRuntime(3164): FATAL EXCEPTION: main
07-10 18:41:06.545: E/AndroidRuntime(3164): Process: com.example.retrofitgallery2, PID: 3164
07-10 18:41:06.545: E/AndroidRuntime(3164): java.lang.NoClassDefFoundError: retrofit.RestAdapter$Builder
07-10 18:41:06.545: E/AndroidRuntime(3164):     at com.example.retrofitgallery2.GalleryApp.getFlickrService(GalleryApp.java:20)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at viewer.APIRestActivity.onCreate(APIRestActivity.java:16)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at viewer.MainActivity.onCreate(MainActivity.java:29)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.app.Activity.performCreate(Activity.java:5231)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.os.Looper.loop(Looper.java:136)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at android.app.ActivityThread.main(ActivityThread.java:5001)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at java.lang.reflect.Method.invoke(Method.java:515)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-10 18:41:06.545: E/AndroidRuntime(3164):     at dalvik.system.NativeStart.main(Native Method)

I had the same error on Android 4.4 and below. It was something linked with the multiDexEnabling.

Try adding " multiDexEnable true " inside build.gradle :

defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 16
        versionName "1.5"
        multiDexEnabled true
    }

If that doesn't help just add this line

android:name="android.support.multidex.MultiDexApplication"

inside AndroidManifest.xml as below:

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:name="android.support.multidex.MultiDexApplication">
...

Place your jars inside the libs folder so that you wont get a runtime NoClassDefFoundError exception. I think you might have screwed up in this part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM