android - 在 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()
}
}
感谢帮助 :)
解决方案
推荐阅读
- php - 使用选择下拉菜单过滤搜索
- c++ - 如何制作 boost.Test 日志/打印标准类型
- excel - 通过 Excel 打开共享 Access 时出现问题
- python - ValueError : 必须以字符 '-' 开头
- node.js - 如何使第二个容器在第一个容器已经在 docker-compose 中启动之前不运行?
- java - 无法通过链接到达 Spring
- vb.net - 在表单中的控件方法之间迭代
- javascript - 使用 puppeteer,我如何等待特定的 xhr 请求完成
- corda - 如何在 Corda 的流程中更改流程的发起者?
- ios - 将滚动识别更改为更严格的垂直?