Android BUG 之 Invoke-customs are only supported starting with Android O (--min-api 26)

foreword

When downloading and importing the project on Github, the compilation passed but the code exploded, as follows, it is obvious that there is a problem with the Lambda expression

This error is because you have not correctly configured the Java version and minSdkVersion in the build.gradle file when using Lambda expressions or method references. For Lambda expressions and method references, Java 8 compatibility needs to be set in gradle.

 

1. What is a Lambda expression?

Lambda expression is essentially an anonymous method. It has neither method name nor access modifier and return value type. Using it to write code is more concise and easier to understand. It makes it easier for developers to write functional interfaces. In Android, Lambda expressions can help us write listeners, callback functions, etc. more concisely, improving code readability and development efficiency. The following is a brief explanation of how Lambda expressions are used in Android:

1. Use Lambda expressions instead of interface implementation classes

Traditional interface implementation classes may make the code lengthy, and Lambda expressions can help us implement listeners and callback functions more concisely. For example, when setting the button's click event in Android, the traditional way of writing is as follows:

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 点击事件处理逻辑
    }
});

Using Lambda expressions can simplify the code to the following:

Button button = findViewById(R.id.button);
button.setOnClickListener((View v) -> {
    // 点击事件处理逻辑
});

2 Traversing collections using Lambda expressions

When traversing the collection, the traditional way of writing may make the code verbose. Using Lambda expressions can help us iterate over collections more concisely. For example, when traversing a List in Android, the traditional writing method is as follows:

List<String> list = new ArrayList<>();
for (String item : list) {
    // item处理逻辑
}

Using Lambda expressions can simplify the code to the following:

List<String> list = new ArrayList<>();
list.forEach(item -> {
    // item处理逻辑
});

Two, solve

1. Add the following configuration to the build.gradle file under App:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        minSdkVersion 26 // 设置你的minSdkVersion
        // ...
    }
    // ...
}

2. If you are using Kotlin, you need to add the following configuration to the build.gradle file:

androidExtensions {
    experimental = true
}

3. If you are using Android Studio 3.0 or higher, you can use the following methods:

  • Open the build.gradle file and select "Open for Editing in Android Studio" in the upper right corner.
  • In the opened file, click on the "File" menu and select "Project Structure".
  • Select "app" in the left menu, then select the "Properties" tab.
  • On this page, select the "Use Latest Platform" option.

Summarize

Of these methods, the first method is the most commonly used and is suitable for most situations. Note that Lambda expressions can only be used in Java 8 and above. If your project uses a lower Java version, you need to upgrade the Java version first. .

Guess you like

Origin blog.csdn.net/X_sunmmer/article/details/131000531