I'm trying to use AWS CloudWatch Log in an Android app. I have the following config for the aws-client:
val basicAWSCredentials = BasicAWSCredentials(
“Xxxxxx”,
“Yyyyyy”
)
val awsLogsClientBuilder = AWSLogsClientBuilder.standard()
awsLogsClientBuilder.region = Regions.EU_WEST_2.name
awsLogsClientBuilder.credentials = AWSStaticCredentialsProvider(basicAWSCredentials)
awsClient = awsLogsClientBuilder.build()
In the build.gradle I have
implementation ("com.amazonaws:aws-java-sdk-logs:1.11.367") {
exclude module: 'joda-time'
}
The app is crashing at awsLogsClientBuilder.build()
with the following exception:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.xxxxxx.xxxxx, PID: 28703 java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar!classes2.dex)...
I have tried to use the android specific library as well:
androidSdkLogsVersion = "2.6.24"
implementation "com.amazonaws:aws-android-sdk-logs:$androidSdkLogsVersion"
and/or
implementation "com.amazonaws:aws-android-sdk-mobile-client:$androidSdkLogsVersion"
but if I use it along with the java sdk I get an error at compile time:
Program type already present: com.amazonaws.ResponseMetadata
If I do not import the java-sdk then I get an error at compile time because the class AWSLogsClientBuilder
is not found anymore.
How should I create the logger on Android?
This is what I did:
In the build.gradle
androidSdkLogsVersion = "2.6.24"
implementation "com.amazonaws:aws-android-sdk-logs:$androidSdkLogsVersion"
and to configure the logger:
val basicAWSCredentials = BasicAWSCredentials("xxxxx","yyyy")
val awsClient = AmazonCloudWatchLogsClient(basicAWSCredentials)
val regions: Regions = Regions.EU_WEST_1
awsClient.setRegion(Region.getRegion(regions.getName()))
I suggest to use the AWS Java SDK V2 . It will allow you to use an alternate HTTP runtime, and avoid some of the mess with the Apache client, when working on Android.
GitHub Issue #1180 in the AWS Java SDK V2 repo addresses this topic.
In your module-level build.gradle
, add dependencies:
dependencies {
implementation 'software.amazon.awssdk:cloudwatch:2.13.49'
implementation 'software.amazon.awssdk:url-connection-client:2.13.49'
}
Now, initialize the CloudWatch client:
val cloudwatch = CloudWatchClient.builder()
.httpClient(UrlConnectionHttpClient.create())
.region(Region.US_EAST_1)
.credentialsProvider(yourCredentialsHere())
.build()
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.