首页 > 解决方案 > 在 Android Lollipop 上运行 Chrome 自定义选项卡 - 仅从 Android N 开始支持静态接口方法

问题描述

我正在使用 chrome 自定义标签制作应用程序。当我尝试在 Lollipop 上运行应用程序时,出现错误:

错误:仅支持从 Android N (--min-api 24) 开始的静态接口方法:androidx.browser.trusted.TrustedWebActivityDisplayMode androidx.browser.trusted.TrustedWebActivityDisplayMode.fromBundle(android.os.Bundle)

我该如何解决?

我的 gradle 应用程序模块:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"


    defaultConfig {
        applicationId "com.myapp.news"
        minSdkVersion 21
        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 JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    dynamicFeatures = [":features", ":customlist"]


}

dependencies {

    //### Base
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    api 'androidx.appcompat:appcompat:1.1.0'
    api 'androidx.core:core-ktx:1.2.0'
    api 'androidx.legacy:legacy-support-v4:1.0.0'
    api "androidx.annotation:annotation:1.1.0"

    // Install: Dynamic Module
    api("com.google.android.play:core:1.7.2")

    // lifecycle & ViewModel
    api 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    api 'androidx.lifecycle:lifecycle-extensions:2.2.0'

    // Navigation
    api "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha05"
    api "androidx.navigation:navigation-ui-ktx:2.3.0-alpha05"
    api "androidx.navigation:navigation-dynamic-features-fragment:2.3.0-alpha05"


    // Preference
    api "androidx.preference:preference-ktx:1.1.1"

    // Database
    api 'androidx.room:room-runtime:2.2.5'
    kapt "androidx.room:room-compiler:2.2.5"
    // optional - Kotlin Extensions and Coroutines support for Room
    api "androidx.room:room-ktx:2.2.5"

    //### Layouts & Design
    api 'com.google.android.material:material:1.1.0'
    api 'androidx.constraintlayout:constraintlayout:1.1.3'
    // Recyclerview
    api 'androidx.recyclerview:recyclerview:1.1.0'
    api 'androidx.recyclerview:recyclerview-selection:1.0.0'
    // Drawer
    api 'com.mikepenz:materialdrawer:8.0.0-rc01'
    api "com.mikepenz:materialdrawer-nav:8.0.0-rc01"


    // Anko Commons
    implementation "org.jetbrains.anko:anko-commons:0.10.8"

    // Custom Tab Chrome
    implementation "androidx.browser:browser:1.2.0"

    //### Tests
    implementation 'androidx.preference:preference:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


}

但这无济于事。

我在 Navigator(导航组件)中使用 chrome 选项卡。


@Navigator.Name("chrome")
class ChromeCustomTabsNavigator(private val context: Context) : Navigator<ChromeCustomTabsNavigator.Destination>() {

    override fun createDestination()= Destination(this)

    override fun navigate(destination: Destination, args: Bundle?, navOptions: NavOptions?, navigatorExtras: Extras?): NavDestination? {

        val url = args?.getString("url") ?: throw IllegalStateException("Destination ${destination.id} does not have an url.")

        if (url.isInvalidWebUrl()) {
            throw IllegalArgumentException("Url($url) is a invalid web URL.")
        }

        val customTabChrome = CustomTabsIntent.Builder()

        val intent = customTabChrome.build()
        try {
            intent.launchUrl(context, Uri.parse(url))
        } catch (e: ActivityNotFoundException) {
            throw e
        }
        return null // Do not add to the back stack, managed by Chrome Custom Tabs
    }

    override fun popBackStack() = true // Managed by Chrome Custom Tabs

    @NavDestination.ClassType(Activity::class)
    class Destination(navigator: Navigator<out NavDestination>) : NavDestination(navigator)

    private fun String.isInvalidWebUrl(): Boolean {
        return Patterns.WEB_URL.matcher(this).matches().not()
    }

}

感谢帮助 :)

标签: androidandroid-jetpackchrome-custom-tabs

解决方案


推荐阅读