我安装了反应本机导航V7.25.4, 现在当我在 android studio 中同步项目时出现错误
A problem occurred configuring project ':react-native-navigation'.
> It is too late to set sourceCompatibility
It has already been read to configure this project.
Consider either moving this call to be during evaluation,
or using the variant API.
我在react-native-navigation的build.gradle上也有两个错误
Cannot resolve symbol 'json'
Cannot resolve symbol 'JsonSlurper'
这是react-native-navigation的build.gradle:
import groovy.json.JsonSlurper
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
def safeExtGetFallbackLowerBound(prop, fallback) {
Math.max(safeExtGet(prop,fallback),fallback)
}
def DEFAULT_COMPILE_SDK_VERSION = 30
def DEFAULT_MIN_SDK_VERSION = 21
def DEFAULT_TARGET_SDK_VERSION = 30
def DEFAULT_KOTLIN_VERSION = "1.5.31"
def DEFAULT_KOTLIN_STDLIB = 'kotlin-stdlib-jdk8'
def kotlinVersion = safeExtGet("RNNKotlinVersion", DEFAULT_KOTLIN_VERSION)
def kotlinStdlib = safeExtGet('RNNKotlinStdlib',DEFAULT_KOTLIN_STDLIB )
def kotlinCoroutinesCore = safeExtGet('RNNKotlinCoroutinesCore', '1.5.2')
android {
compileSdkVersion safeExtGetFallbackLowerBound('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
defaultConfig {
minSdkVersion safeExtGetFallbackLowerBound('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
targetSdkVersion safeExtGetFallbackLowerBound('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
}
buildTypes {
release {
minifyEnabled false
}
debug {
minifyEnabled false
}
}
lintOptions {
abortOnError false
}
testOptions {
unitTests.includeAndroidResources = true
unitTests.all { t ->
maxHeapSize = "4g"
reports {
html.enabled true
}
testLogging {
events "PASSED", "SKIPPED", "FAILED"
exceptionFormat TestExceptionFormat.FULL
showExceptions true
showCauses true
showStackTraces true
}
afterSuite { desc, result ->
if (!desc.parent) { // will match the outermost suite
def output = " ${result.resultType} (${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped) "
def repeatLength = output.length()
println '\n\n' + ('-' * repeatLength) + '\n' + output + '\n' + ('-' * repeatLength) + '\n'
println "see report at file://${t.reports.html.destination}/index.html"
}
}
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
flavorDimensions "RNN.reactNativeVersion"
productFlavors {
reactNative51 {
dimension "RNN.reactNativeVersion"
buildConfigField("int", "REACT_NATVE_VERSION_MINOR", "51")
}
reactNative55 {
dimension "RNN.reactNativeVersion"
buildConfigField("int", "REACT_NATVE_VERSION_MINOR", "55")
}
reactNative56 {
dimension "RNN.reactNativeVersion"
buildConfigField("int", "REACT_NATVE_VERSION_MINOR", "56")
}
reactNative57 {
dimension "RNN.reactNativeVersion"
buildConfigField("int", "REACT_NATVE_VERSION_MINOR", "57")
}
reactNative57_5 {
dimension "RNN.reactNativeVersion"
buildConfigField("int", "REACT_NATVE_VERSION_MINOR", "57")
}
reactNative60 {
dimension "RNN.reactNativeVersion"
buildConfigField("int", "REACT_NATVE_VERSION_MINOR", "60")
}
reactNative62 {
dimension "RNN.reactNativeVersion"
buildConfigField("int", "REACT_NATVE_VERSION_MINOR", "62")
}
reactNative63 {
dimension "RNN.reactNativeVersion"
buildConfigField("int", "REACT_NATVE_VERSION_MINOR", "63")
}
}
def flavor = resolveFlavor()
variantFilter { variant ->
def names = variant.flavors*.name
if (!names.contains(flavor)) {
setIgnore(true)
}
}
}
String resolveFlavor() {
List reactNativeVersionComponents = reactNativeVersionComponents(findReactNativePackageJson())
Integer reactNativeMinorComponent = reactNativeVersionComponents[1].toInteger()
Integer reactNativePatchComponent = reactNativeVersionComponents[2].toInteger()
if (reactNativeMinorComponent >= 63) {
return "reactNative63"
} else if (reactNativeMinorComponent >= 62) {
return "reactNative62"
} else if (reactNativeMinorComponent >= 60) {
return "reactNative60"
} else if (reactNativeMinorComponent >= 57 && reactNativePatchComponent >= 5) {
return "reactNative57_5"
} else if (reactNativeMinorComponent >= 57) {
return "reactNative57"
} else if (reactNativeMinorComponent >= 56) {
return "reactNative56"
} else if (reactNativeMinorComponent >= 55) {
return "reactNative55"
} else if (reactNativeMinorComponent >= 51) {
return "reactNative51"
}
}
Object findReactNativePackageJson() {
def searchPath = 'node_modules/react-native/package.json'
def projectDir = project.projectDir.toString() + '/'
def rnPackageJsonFile = new File(projectDir + searchPath)
while (!rnPackageJsonFile.exists()) {
searchPath = '../' + searchPath
rnPackageJsonFile = new File(projectDir + searchPath)
}
return rnPackageJsonFile
}
List reactNativeVersionComponents(rnPackageJsonFile) {
def packageSlurper = new JsonSlurper()
def reactNativePackageJson = packageSlurper.parseText(rnPackageJsonFile.text)
def reactNativeVersion = reactNativePackageJson.version
return reactNativeVersion.tokenize('-')[0].tokenize('.')
}
allprojects { p ->
p.afterEvaluate {
p.android.compileOptions.sourceCompatibility JavaVersion.VERSION_1_8
p.android.compileOptions.targetCompatibility JavaVersion.VERSION_1_8
}
p.repositories {
maven { url "https://jitpack.io" }
}
}
dependencies {
implementation 'org.codehaus.groovy:groovy-json:3.0.9' // add manual
implementation "androidx.core:core-ktx:1.6.0"
implementation "org.jetbrains.kotlin:$kotlinStdlib:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutinesCore"
implementation "androidx.constraintlayout:constraintlayout:2.0.4"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'
implementation 'com.github.wix-playground:ahbottomnavigation:3.3.0'
// implementation project(':AHBottomNavigation')
implementation 'com.github.wix-playground:reflow-animator:1.0.6'
implementation 'com.github.clans:fab:1.6.4'
//noinspection GradleDynamicVersion
implementation 'com.facebook.react:react-native:+'
if("Playground".toLowerCase() == rootProject.name.toLowerCase()){
// tests only for our playground
testImplementation 'junit:junit:4.13.2'
testImplementation "org.robolectric:robolectric:4.7.2"
testImplementation 'org.assertj:assertj-core:3.11.1'
testImplementation 'org.mockito:mockito-core:4.0.0'
testImplementation 'com.squareup.assertj:assertj-android:1.2.0'
testImplementation 'org.mockito:mockito-inline:3.4.0'
testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlinVersion"
}
}
我的android studio版本:2021.1.1 patch2。
类路径“org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31”
classpath('com.android.tools.build:gradle:7.1.2')
这与设置 sourceCompatibility :react-native-navigation 是同一个问题。
这是由于 7.26 之前版本的 React Native Navigation 的
app/build.gradle
配置问题导致的。
解决方案是升级或手动修复损坏的配置(https://stackoverflow.com/a/77923298/4821759)