AWS SDK NoClassDefFoundError: com.amazonaws.servic

2020-02-15 05:09发布

问题:

I've been trying to upload a video I took to a bucket of S3 services using the AWS SDK.

this is the code that I took from the Amazon example for S3 uploading. The only things I'm doing differently is:

  1. not creating a new bucket but reusing the one that is already there.
  2. using a predetermined Uri for the file where the image is.

    public void uploadToS3(View v) {
        AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(
            Constants.ACCESS_KEY_ID, Constants.SECRET_KEY));
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(fileUri, filePathColumn,
            null, null, null);
        cursor.moveToFirst();
    
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
    
        // Put the image data into S3.
        try {
            //s3Client.createBucket(Constants.getPictureBucket());
    
            PutObjectRequest por = new PutObjectRequest(
                    Constants.getPictureBucket(), Constants.PICTURE_NAME,
                    new java.io.File(filePath)); // Content type is determined
                                                 // by file extension.
            s3Client.putObject(por);
        } catch (Exception exception) {
            // displayAlert( "Upload Failure", exception.getMessage() );
        }
    }
    

the problem is, once I try and create the AmazonS3Client, the app crashes with a noClassFound exception.

here is my Logcat:

05-17 10:42:02.555: E/AndroidRuntime(20310): FATAL EXCEPTION: main
05-17 10:42:02.555: E/AndroidRuntime(20310): java.lang.IllegalStateException: Could not execute method of the activity
05-17 10:42:02.555: E/AndroidRuntime(20310):    at android.view.View$1.onClick(View.java:2154)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at android.view.View.performClick(View.java:2538)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at android.view.View$PerformClick.run(View.java:9152)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at android.os.Handler.handleCallback(Handler.java:587)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at android.os.Looper.loop(Looper.java:123)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at java.lang.reflect.Method.invokeNative(Native Method)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at java.lang.reflect.Method.invoke(Method.java:507)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at dalvik.system.NativeStart.main(Native Method)
05-17 10:42:02.555: E/AndroidRuntime(20310): Caused by: java.lang.reflect.InvocationTargetException
05-17 10:42:02.555: E/AndroidRuntime(20310):    at java.lang.reflect.Method.invokeNative(Native Method)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at java.lang.reflect.Method.invoke(Method.java:507)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at android.view.View$1.onClick(View.java:2149)
05-17 10:42:02.555: E/AndroidRuntime(20310):    ... 11 more
05-17 10:42:02.555: E/AndroidRuntime(20310): Caused by: java.lang.NoClassDefFoundError: com.amazonaws.services.s3.AmazonS3Client
05-17 10:42:02.555: E/AndroidRuntime(20310):    at com.thepoosh.aws.s3upload.android.CameraToAmazonaws3Activity.uploadToS3(CameraToAmazonaws3Activity.java:63)
05-17 10:42:02.555: E/AndroidRuntime(20310):    ... 14 more
05-17 10:42:02.555: E/AndroidRuntime(20310): Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.s3.AmazonS3Client in loader dalvik.system.PathClassLoader[/data/app/com.thepoosh.aws.s3upload.android-1.apk]
05-17 10:42:02.555: E/AndroidRuntime(20310):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-17 10:42:02.555: E/AndroidRuntime(20310):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-17 10:42:02.555: E/AndroidRuntime(20310):    ... 15 more

回答1:

Your code gives NoClassDefFoundError because you didnt add the sdk's library to your project. Here is how you can do it

  • Right click on your project name -> New -> Folder and name it "libs" (without "s)
  • You should add "aws-android-sdk-X.X.X-debug.jar" in this library folder
  • Right click on your project name -> Properties -> select Java Build Path from left -> Click Add Jars -> navigate to your projects libs which we created.
  • select "aws-android-sdk-X.X.X-debug.jar"
  • Click Ok and you are done.

Hope this helps