运行Android应用会产生java.lang。NoClassDefFoundError:retrofit.RestAdapter $建设者

时间:2021-05-22 17:27:32

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.

我正在开发一个使用Android上的Flickr API(使用Eclipse和4.4 SDK)的相册应用程序。该项目已成功编译,但一旦在Nexus 7(2012)上再次运行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

在我的构建路径中包含以下jar: retrofit-1.6.1 okhttp-2.0.0 gson-2.2.4 picasso-2.3.2

Here are the relevant files:

以下是相关文件:

GalleryApp.java

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

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)

2 个解决方案

#1


1  

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

我在安卓4.4和下面有同样的错误。它与multidexenable联系在一起。

Try adding "multiDexEnable true" inside build.gradle:

试着在建筑物内添加“multiDexEnable true”。

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:

在AndroidManifest。xml如下:

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

#2


-1  

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.

将jar放到libs文件夹中,这样就不会出现运行时NoClassDefFoundError异常。我想你在这方面可能搞砸了。

#1


1  

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

我在安卓4.4和下面有同样的错误。它与multidexenable联系在一起。

Try adding "multiDexEnable true" inside build.gradle:

试着在建筑物内添加“multiDexEnable true”。

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:

在AndroidManifest。xml如下:

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

#2


-1  

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.

将jar放到libs文件夹中,这样就不会出现运行时NoClassDefFoundError异常。我想你在这方面可能搞砸了。