2

问题:

我正在尝试重新创建此 codelab 教程项目https://developer.android.com/codelabs/android-proto-datastore,但 Android Studio 无法导入androidx.datastore.dataStore

脚步:

  1. 使用空的 Activity 创建新的 Kotlin 项目

  2. 修改gradle文件

  3. 切换到 Android Studio 的项目视图

  4. 在 app/src/main 中创建一个名为proto的文件夹

  5. 在 app/src/main/ proto中创建和修改文件user_prefs.proto

  6. 构建 -> 清理项目 -> 重建项目

  7. 创建一个名为UserPreferencesSerializer的序列化程序类

  8. 尝试将以下代码添加到空 MainActivity.kt

    private const val DATA_STORE_FILE_NAME = "user_prefs.pb"

    private val Context.userPreferencesStore: DataStore by dataStore(fileName = DATA_STORE_FILE_NAME, serializer = UserPreferencesSerializer)

在这一步之后,Android Studio 会标记dataStore并显示警告“ Unresolved reference: dataStore ”我也无法导入 androidx.datastore.dataStore,但我在我的 gradle 文件中找不到丢失的导入。请问,有人能告诉我如何解决这个问题吗?

代码:

构建.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id "com.google.protobuf" version "0.8.12"
}



android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 28
        targetSdkVersion 30
        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 JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    
    implementation  "androidx.datastore:datastore-core:1.0.0-alpha08"
    implementation  "com.google.protobuf:protobuf-javalite:3.11.0"

}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.10.0"
    }

    // Generates the java Protobuf-lite code for the Protobufs in this project. See
    // https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
    // for more information.
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option 'lite'
                }
            }
        }
    }
}

user_prefs.proto

syntax = "proto3";

option java_package = "com.example.test";
option java_multiple_files = true;

message UserPreferences {
  // filter for showing / hiding completed tasks
  bool show_completed = 1;
}

UserPreferencesSerializer

package com.example.test

import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
import com.google.protobuf.InvalidProtocolBufferException
import java.io.InputStream
import java.io.OutputStream

object UserPreferencesSerializer : Serializer<UserPreferences> {
    override val defaultValue: UserPreferences = UserPreferences.getDefaultInstance()
    override suspend fun readFrom(input: InputStream): UserPreferences {
        try {
            return UserPreferences.parseFrom(input)
        } catch (exception: InvalidProtocolBufferException) {
            throw CorruptionException("Cannot read proto.", exception)
        }
    }

    override suspend fun writeTo(t: UserPreferences, output: OutputStream) = t.writeTo(output)
}

MainActivity.kt

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.datastore.core.DataStore


private const val DATA_STORE_FILE_NAME = "user_prefs.pb"
private val Context.userPreferencesStore: DataStore<UserPreferences> by dataStore(
    fileName = DATA_STORE_FILE_NAME,
    serializer = UserPreferencesSerializer
)


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}
4

2 回答 2

0

dataStore委托是androidx.datastore:datastore图书馆的一部分。

将依赖项添加到您的模块build.gradle文件中。替换$dataStoreVersion为您使用的数据存储版本,例如1.0.0

implementation  "androidx.datastore:datastore:$dataStoreVersion"

您可以在 Google 的 Maven 存储库中找到可用的版本。

添加此依赖项后,您可以by dataStore通过将以下导入添加到您的类来使用:

import androidx.datastore.dataStore
于 2021-11-10T14:33:51.897 回答
0

我在做项目时也遇到了这个问题。

我发现我正在使用datastore-core并且需要datastore-preferences. 所以我改变了我的依赖声明:

implementation 'androidx.datastore:datastore-core:1.0.0-alpha08'

至:

implementation 'androidx.datastore:datastore-preferences:1.0.0'

alpha08可能在发布和发布之间发生了重大变化1.0.0

于 2021-09-06T22:51:56.830 回答