'Tried to get a plugin but that plugin was not present
I am trying to use AWS Amplify's REST API with my android app. I did exactly as it was written in the docs, but I am still getting this error:
ApiException{message=AWSApiPlugin depends on AWSCognitoAuthPlugin but it is currently missing, cause=java.lang.IllegalStateException: Tried to get a plugin but that plugin was not present. Check if the plugin was added originally or perhaps was already removed., recoverySuggestion=Before configuring Amplify, be sure to add AWSCognitoAuthPlugin same as you added AWSApiPlugin.}
These are the AWS dependencies in my Gradle file:
dependencies {
// ...
implementation 'com.amplifyframework:aws-api:1.6.4'
implementation 'com.amplifyframework:core:1.6.4'
implementation 'com.amazonaws:aws-android-sdk-apigateway-core:2.3.2'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.3.2'
// ...
}
This is my Application class:
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
try {
Amplify.addPlugin(AWSApiPlugin())
Amplify.configure(applicationContext)
Log.i("MyAmplifyApp", "Initialized Amplify.")
} catch (error: AmplifyException){
Log.e("MyAmplifyApp","Could not initialize Amplify.",error)
}
}
}
Solution 1:[1]
This error happens because you need to call Amplify.addPlugin before you make use of it.
It can also be there was an error initializing the plugin, so check the suggestion in the error when catching the exception.
So call whatever plugin you are using, preferrably in your Application class (MyApp.java) code:
public class MyAmplifyApp extends Application {
String tag = "xxx";
@Override
public void onCreate() {
super.onCreate();
//xxx
try {
Amplify.addPlugin(new AWSDataStorePlugin());
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.configure(getApplicationContext());
Log.i(tag, "Initialized Amplify");
} catch (AmplifyException error) {
Log.e(tag, "Could not initialize Amplify", error);
}
}
}
Solution 2:[2]
How are you attempting to authenticate with your API? What is the authType listed in your app/src/main/res/raw/amplifyconfiguration.json?
If it is something involving Cognito, you need to add the Auth category. Please follow the Getting Started guide for Auth.
If you are not trying to use Cognito, update your API to use a different auth type using the CLI:
amplify update api
amplify push
For example, you might select API Key authorization, which essentially makes your API public to anyone who knows the key.
Solution 3:[3]
According to official Doc Add This line to my configuration function solved my problem
Amplify.addPlugin(new AWSApiPlugin());
So it become look like this
try {
Amplify.addPlugin(new AWSDataStorePlugin());
Amplify.addPlugin(new AWSApiPlugin());
Amplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify");
} catch (AmplifyException error) {
Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
}
Solution 4:[4]
I had the same issue and the previous answers pointed me in the right direction. I was missing the line below from my initialize function, as soon as I added this in everything worked as planned.
Amplify.addPlugin(AWSCognitoAuthPlugin())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jameson |
| Solution 3 | |
| Solution 4 | Wai Ha Lee |
