Android 的 AWS Amplify - 无法解析符号 'AmplifyModelProvider'

AWS Amplify for Android - Cannot resolve symbol 'AmplifyModelProvider'

大家好,

我正在实施 AWS Amplify DataStore for android following the docs, and basically i get this error when i try to initialize the data store plugin on amplify according to this part of the doc:

无法解析符号'AmplifyModelProvider'

您可以在下面找到我的 gradle 文件和我的应用程序 class 中的代码。

我不是 android 专家,但我认为这是因为缺少依赖项或者我在 ModelProvider 的初始化或分配过程中做错了什么。但我无法在互联网上找到有关此问题或任何可能解决方案的任何信息。

在此先感谢您的帮助。

重现步骤:

  1. 新建android项目

  2. 安装 Amplify CLI,使用 npm,npm install -g @aws-amplify/cli

  3. 配置放大,amplify configure

  4. 在新的android项目根目录,运行扩充初始化,amplify init

  5. 在新的android项目根文件夹,运行扩充添加授权,amplify add auth,添加授权

  6. 创建 android 应用程序 class

  7. 将android应用程序class添加到AndroidManifest.xml

  8. 尝试添加数据存储插件以在应用程序的onCreate方法上进行放大class

我已经尝试了以下解决方案:

  1. 清理项目

  2. 重建项目

  3. 运行 制作项目

  4. 关闭再打开Android工作室

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bakeano.htejobs">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MyApplication.java

package com.bakeano.htejobs;

import android.app.Application;
import android.util.Log;

import com.amazonaws.mobile.client.AWSMobileClient;
import com.amazonaws.mobile.client.Callback;
import com.amazonaws.mobile.client.UserStateDetails;
import com.amplifyframework.api.aws.AWSApiPlugin;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.core.model.ModelProvider;
import com.amplifyframework.datastore.AWSDataStorePlugin;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // AWSMobileClient initialization
        AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
            @Override
            public void onResult(UserStateDetails result) {
                try {
                    ModelProvider modelProvider = AmplifyModelProvider.getInstance(); // Error on this line !!!
                    Amplify.addPlugin(AWSDataStorePlugin.forModels(modelProvider));
                    Amplify.addPlugin(new AWSApiPlugin());
                    Amplify.configure(getApplicationContext());
                } catch (Exception e) {
                    Log.e("bakeanoMessage", "Amplify adding plugins Exception: " + e.getMessage(), e);
                }
            }

            @Override
            public void onError(Exception e) {
                Log.e("bakeanoMessage", "AWSMobileClient init Exception: " + e.getMessage(), e);
            }
        });
    }
}

模块Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.bakeano.htejobs"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    // androidx constraint layout
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'

    // aws amplify framework core
    implementation 'com.amplifyframework:core:0.10.0'

    // AWSMobileClient
    implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.16.11'

    // aws amplify for the drop-in ui
    implementation 'com.amazonaws:aws-android-sdk-auth-userpools:2.16.11'
    implementation 'com.amazonaws:aws-android-sdk-auth-ui:2.16.11'

    // aws amplify api
    implementation 'com.amplifyframework:aws-api:0.10.0'

    // aws amplify data store
    implementation 'com.amplifyframework:aws-datastore:0.10.0'

}

项目Gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
        mavenCentral()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'

        // amplify tools gradle plugin
        classpath 'com.amplifyframework:amplify-tools-gradle-plugin:0.2.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

// applying the amplify tools plugin
apply plugin: 'com.amplifyframework.amplifytools'

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Jameson Williams提供的解决方案:

projects github issue platform - issue #370

AmplifyModelProvider 是一个由 amplify codegen

创建的自动生成的文件

amplify codegen models

手动生成后需要导入:

import com.amplifyframework.datastore.generated.model.AmplifyModelProvider;

谢谢!!