运行Android应用程序会产生java.lang.NoClassDefFoundError:retrofit.RestAdapter $ Builder

时间:2014-07-10 13:14:37

标签: java android retrofit

我正在使用Android上的Flickr API制作照片库应用程序(使用Eclipse和4.4 SDK)。该项目已成功编译,但应用程序一旦在Nexus 7(2012)上再次运行4.4,即会崩溃。

我的构建路径中包含以下jar:   改装的1.6.1   okhttp-2.0.0   GSON-2.2.4   毕加索-2.3.2

以下是相关文件:

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);
}

完整错误日志

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)

2 个答案:

答案 0 :(得分:1)

我在Android 4.4及更低版本上遇到了同样的错误。它与multiDexEnabling相关联。

尝试添加&#34; multiDexEnable true&#34;在build.gradle内:

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

如果这样做无助于添加此行

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

AndroidManifest.xml内,如下所示:

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

答案 1 :(得分:-1)

将您的jar放在libs文件夹中,这样就不会出现运行时NoClassDefFoundError异常。我想你可能已经搞砸了这一部分。