首页 > 解决方案 > The plugin flutter_plugin_android_lifecycle could not be built

问题描述

The task - :compileReleaseJavaWithJavac failed saying: The plugin flutter_plugin_android_lifecycle couldn't be built due to the errors:

error: package androidx.lifecycle does not exist
import androidx.lifecycle.Lifecycle;

and

error: cannot find symbol
  public static Lifecycle getActivityLifecycle(
                ^
  symbol:   class Lifecycle
  location: class FlutterLifecycleAdapter

Here is the FlutterLifecycleAdapter.java file:

package io.flutter.embedding.engine.plugins.lifecycle;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;

/** Provides a static method for extracting lifecycle objects from Flutter plugin bindings. */
public class FlutterLifecycleAdapter {
  private static final String TAG = "FlutterLifecycleAdapter";

  /**
   * Returns the lifecycle object for the activity a plugin is bound to.
   *
   * <p>Returns null if the Flutter engine version does not include the lifecycle extraction code.
   * (this probably means the Flutter engine version is too old).
   */
  @NonNull
  public static Lifecycle getActivityLifecycle(
      @NonNull ActivityPluginBinding activityPluginBinding) {
    HiddenLifecycleReference reference =
        (HiddenLifecycleReference) activityPluginBinding.getLifecycle();
    return reference.getLifecycle();
  }
}

This is the first time I'm using sharedPreferences and Flutter_secure_storage and the first time I've encountered this problem. I believe error came up after I updated Android Studio. I haven't touched anything in this file. Thanks for your help in advance!

标签: javaandroidflutterandroid-lifecycleactivity-lifecycle

解决方案


请按照以下步骤操作:

gradle-wrapper.properties添加以下代码:

distributionUrl=https://services.gradle.org/distributions/gradle-5.4.1-all.zip

在此处输入图像描述

gradle.properites请检查以下行是否已添加:

android.useAndroidX=true
android.enableJetifier=true

在此处输入图像描述

android/build.gradle文件中添加以下代码:

classpath 'com.android.tools.build:gradle:3.5.4'

在此处输入图像描述

并在android/build.gradle检查这个版本:

compileSdkVersion 29

在此处输入图像描述

请参考此链接了解更多信息: 点击这里


推荐阅读