3

I am seeing this crash whenever I am trying to initialize aws s3 client. I tried looking for solutions but didn't found any.

BasicAWSCredentials awsCreds = new BasicAWSCredentials(amazonAccessId, amazonSecretKey);
Amazon S3 s3Client = AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
            .withRegion(Regions.AP_SOUTH_1)
            .build();

Gradle file

dependencies {
      implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.641')
      implementation 'com.amazonaws:aws-java-sdk-s3:1.11.641'
}

This throws an error on .build() line saying Failed resolution of: Ljavax/management/MBeanServerFactory;

--------- beginning of crash
2019-09-30 17:48:02.148 20191-20191/com.tactopus.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.tactopus.app, PID: 20191
    java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/management/MBeanServerFactory;
        at com.amazonaws.jmx.MBeans.getMBeanServer(MBeans.java:111)
        at com.amazonaws.jmx.MBeans.registerMBean(MBeans.java:50)
        at com.amazonaws.jmx.SdkMBeanRegistrySupport.registerMetricAdminMBean(SdkMBeanRegistrySupport.java:27)
        at com.amazonaws.metrics.AwsSdkMetrics.registerMetricAdminMBean(AwsSdkMetrics.java:398)
        at com.amazonaws.metrics.AwsSdkMetrics.<clinit>(AwsSdkMetrics.java:359)
        at com.amazonaws.metrics.AwsSdkMetrics.addAll(AwsSdkMetrics.java:662)
        at com.amazonaws.services.s3.AmazonS3Client.<clinit>(AmazonS3Client.java:406)
        at com.amazonaws.services.s3.AmazonS3Builder$1.apply(AmazonS3Builder.java:35)
        at com.amazonaws.services.s3.AmazonS3Builder$1.apply(AmazonS3Builder.java:32)
        at com.amazonaws.services.s3.AmazonS3ClientBuilder.build(AmazonS3ClientBuilder.java:64)
        at com.amazonaws.services.s3.AmazonS3ClientBuilder.build(AmazonS3ClientBuilder.java:28)
        at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
        at com.tactopus.app.AWS.S3DownloadAndUpdate.init(S3DownloadAndUpdate.java:98)
        at com.tactopus.app.AWS.S3DownloadAndUpdate.<init>(S3DownloadAndUpdate.java:64)
        at com.tactopus.app.presenter.BookDescriptionActivityPresenter.onClick(BookDescriptionActivityPresenter.java:46)
        at com.tactopus.app.view.BookDescriptionActivity$3.onClick(BookDescriptionActivity.java:166)
        at android.view.View.performClick(View.java:7352)
        at android.widget.TextView.performClick(TextView.java:14177)
        at android.view.View.performClickInternal(View.java:7318)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27800)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.management.MBeanServerFactory" on path: DexPathList[[zip file "/data/app/com.tactopus.app-IcaP3SsuBAnlY85tNBJ8fw==/base.apk"],nativeLibraryDirectories=[/data/app/com.tactopus.app-IcaP3SsuBAnlY85tNBJ8fw==/lib/arm64, /system/lib64, /system/vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
Ayush Malviya
  • 991
  • 1
  • 7
  • 20

1 Answers1

2

I had this problem today, it looks like MBeanServerFactory isn't a part of AWS's Java SDK.

Use the following dependencies instead

implementation 'com.amazonaws:aws-android-sdk-core:2.6.0'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.2.0'
implementation 'com.amazonaws:aws-android-sdk-s3:2.6.0'

You can use CognitoCachingCredentialsProvider to initialize your AmazonS3 client. Go to your Amazon Cognito Console, then create an Identity Pool. Make sure you have given the correct permissions (you can check by going to your IAM Dashboard, then under the 'Roles' tab, check for your Identity Pool that you just created and add the AmazonS3FullAccess policy).

After you have created the Identity Pool, you will get sample code that looks like this

CognitoCachingCredentialsProvider credentialsProvider;
credentialsProvider = new CognitoCachingCredentialsProvider(
    getApplicationContext(),
    "ap-southeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Identity Pool ID
    Regions.AP_SOUTHEAST_1 // Region
);

Use this credentialsProvider object to initialize your AmazonS3 client

AmazonS3 s3Client = new AmazonS3Client(credentialsProvider);
Adwait Bhope
  • 43
  • 2
  • 6
  • Yes with this dependency it is working. The reason I am using Java SDK is that it has a transfer manager which helps in downloading the whole directory at once. – Ayush Malviya Oct 03 '19 at 06:50
  • Great answer, that was very helpful for me! I was using the Java SDK and I had this problem additionally to a very dex count toll, using this library instead solves both problems. Thank you. – androidseb Oct 12 '20 at 20:40
  • @AyushMalviya how did you solve the issue and be able to use the TransferManager on android? I have the same problem here – leo7r Feb 09 '23 at 14:56