-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(all): Add ability to use OkHttp4 with Amplify v2.x (#2970)
- Loading branch information
1 parent
97ddb8b
commit fe0c468
Showing
17 changed files
with
159 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
aws-core/src/main/java/com/amplifyframework/util/AmplifyHttp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amplifyframework.util | ||
|
||
import aws.smithy.kotlin.runtime.http.config.HttpClientConfig | ||
import aws.smithy.kotlin.runtime.http.engine.okhttp4.OkHttp4Engine | ||
import com.amplifyframework.annotations.InternalAmplifyApi | ||
import com.amplifyframework.core.Amplify | ||
|
||
internal object AmplifyHttp { | ||
|
||
enum class Version { | ||
OkHttp4, | ||
OkHttp5 | ||
} | ||
|
||
private val logger = Amplify.Logging.logger("HttpEngine") | ||
|
||
val availableVersion: Version by lazy { | ||
// Check to see if OkHttp4 Engine is available on the runtime classpath. If it is then the customer has | ||
// explicitly added it, so we can use that. Otherwise, use the OkHttp5 engine. | ||
try { | ||
Class.forName("aws.smithy.kotlin.runtime.http.engine.okhttp4.OkHttp4Engine") | ||
logger.info("Using OkHttp4 Engine") | ||
Version.OkHttp4 | ||
} catch (e: ClassNotFoundException) { | ||
logger.info("Using default OkHttp5 Engine") | ||
Version.OkHttp5 | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This function is used to determine, at runtime, whether we should use the OkHttp4Engine instead of the | ||
* default OkHttp5Engine with Kotlin SDK. This allows customers that cannot use OkHttp5 (which is currently an alpha | ||
* release) to use OkHttp4 throughout Amplify by adding a dependency on aws.smithy.kotlin:http-client-engine-okhttp4 | ||
* to their runtime classpath. | ||
* This must be called when instantiating any Client instance from the Kotlin SDK. | ||
*/ | ||
@InternalAmplifyApi | ||
fun HttpClientConfig.Builder.setHttpEngine() { | ||
// The default engine is OkHttp5. If we should use OkHttp4 instead then override it here. | ||
if (AmplifyHttp.availableVersion == AmplifyHttp.Version.OkHttp4) { | ||
this.httpClient = OkHttp4Engine() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Using OkHttp4 in Amplify Android | ||
|
||
Amplify Android v2 uses OkHttp5 by default, but starting with release `2.26.0` it will switch all clients to use OkHttp4 if the `OkHttp4Engine` | ||
is available on the runtime classpath. Please use these steps to switch to using OkHttp4. | ||
|
||
## 1. Upgrade Amplify if necessary | ||
|
||
You must be using at least Amplify `2.26.0` to use OkHttp4. | ||
|
||
## 2. Add the required dependency | ||
|
||
Add the dependency on the `OkHttp4Engine` library to your application's `build.gradle.kts` | ||
|
||
```kotlin | ||
dependencies { | ||
implementation("aws.smithy.kotlin:http-client-engine-okhttp4:1.3.32") // Version must align with Smithy dependency in Amplify | ||
} | ||
``` | ||
|
||
To determine the correct version for the above dependency check in Amplify's [libs.versions.toml](../gradle/libs.versions.toml) file. | ||
Ensure that you are viewing the file version for the Amplify version you are using, and then check the version entry for `aws-smithy`. | ||
Remember to keep these versions in sync when you update Amplify. | ||
|
||
## 3. Force the OkHttp version | ||
|
||
Add the following snippet in your application's `build.gradle.kts` file: | ||
|
||
```kotlin | ||
configurations.configureEach { | ||
// Force replace OkHttp5 with OkHttp4 | ||
resolutionStrategy { | ||
force("com.squareup.okhttp3:okhttp:4.12.0") // Or whicher OkHttp version you want | ||
} | ||
// Exclude other OkHttp5 dependencies | ||
exclude(group = "com.squareup.okhttp3", module = "okhttp-coroutines") | ||
} | ||
``` | ||
|
||
## 4. Add Proguard/R8 rules | ||
|
||
If you are using obfuscation/minification you may encounter compilation errors in affected builds. Check | ||
`build/outputs/mapping/<variant>/missing_rules.txt` for any rules that are needed. The following | ||
rules may need to be added to `proguard-rules.pro`: | ||
|
||
``` | ||
-dontwarn com.google.errorprone.annotations.Immutable | ||
-dontwarn okhttp3.ConnectionListener$Companion | ||
-dontwarn okhttp3.ConnectionListener | ||
-dontwarn okhttp3.coroutines.ExecuteAsyncKt | ||
``` | ||
|
||
## Troubleshooting | ||
|
||
Please refer to the [AWS SDK for Kotlin document](https://github.com/smithy-lang/smithy-kotlin/tree/main/runtime/protocol/http-client-engines/http-client-engine-okhttp4) on this topic or [Open an Issue](https://github.com/aws-amplify/amplify-android/issues/new/choose) if you run into any problems. |
Oops, something went wrong.