Cordova:java.lang.IllegalStateException:未指定compileSdkVersion。

9

我将我的cordova环境更新到了Cordova Android 7,并在使用命令cordova build android --device --verbose时遇到了以下错误。

Command finished with error code 0: /usr/libexec/java_home
ANDROID_HOME=/Users/kano/Library/Android/sdk 
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home studio Subproject Path: CordovaLib Subproject Path: app Running command: /Users/kano/git_repositories/ncdc/KuiManagementSystem/app/platforms/android/gradlew cdvBuildDebug -b /Users/kano/git_repositories/ncdc/KuiManagementSystem/app/platforms/android/build.gradle
-Dorg.gradle.daemon=true -Dorg.gradle.jvmargs=-Xmx2048m -Pandroid.useDeprecatedNdk=true 
publishNonDefault is deprecated and has no effect anymore. 
All variants are now published. Failed to notify ProjectEvaluationListener.afterEvaluate(), 
but primary configuration failure takes precedence. 
java.lang.IllegalStateException: compileSdkVersion is not specified.
at com.google.common.base.Preconditions.checkState(Preconditions.java:456)
at com.android.build.gradle.BasePlugin.createAndroidTasks(BasePlugin.java:590)
at com.android.build.gradle.BasePlugin.lambda$null$3(BasePlugin.java:555)
at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:81)
at com.android.build.gradle.BasePlugin.lambda$createTasks$4(BasePlugin.java:551)
at org.gradle.internal.event.BroadcastDispatch$ActionInvocationHandler.dispatch(BroadcastDispatch.java:91)

我添加了以下参数,但结果相同。
$ cordova build android --device --verbose -- --gradleArg=-PcdvCompileSdkVersion=26

有人知道解决方案或变通方法吗?


你找到解决方案了吗? 我遇到了同样的问题,现在唯一的解决方法是将 Cordova Android 版本降级至 6.4.0。 - Toan Tran
2
你好,Toan Tran。 我创建了一个新的项目目录并将源代码复制到其中。 此后,我没有遇到这个问题。当我在新环境中添加Android平台时,出现了许多插件错误。我需要更新一些插件版本。 - rsogo
3个回答

4
通过运行以下命令
ionic cordova build android --prod --no-build

我突然遇到了同样的问题:

java.lang.IllegalStateException: compileSdkVersion is not specified.

也许您的问题不同,但您应该能够像我现在所描述的那样追踪您的问题。
如果您继续阅读,将会有更多信息:
FAILURE: Build failed with an exception.

* Where: Script '/***/platforms/android/CordovaLib/cordova.gradle' line: 132

以下是函数:

def doExtractIntFromManifest(name) {
    def manifestFile = file(android.sourceSets.main.manifest.srcFile)
    def pattern = Pattern.compile(name + "=\"(\\d+)\"")
    def matcher = pattern.matcher(manifestFile.getText())
    matcher.find()
    println('Crashing name: ' + name) // <-- I added this line
    return new BigInteger(matcher.group(1))
}

我添加了一个println语句,想要查看哪一个会导致崩溃。执行上述命令来构建Android应用程序的输出如下:

Crashing name: versionCode

好的,我在我的config.xml中设置了以下内容:android-versionCode="0.0.1"

那么怎么回事?RegExp不再匹配该模式。

var a = 'android-versionCode="1"';
var a1 = 'android-versionCode="1.0.0"';
var b = new RegExp('versionCode' + "=\"(\\d+)\"");

console.log('With Version as 1:', b.exec(a));
console.log('With Version as 1.0.0:', b.exec(a1));

实际上,cordova手册页面指出它应该是以下方式:

https://cordova.apache.org/docs/de/latest/config_ref/

<widget id="io.cordova.hellocordova"
  version="0.0.1"
  android-versionCode="7"
  ios-CFBundleVersion="3.3.3">

versionCode = PATCH + MINOR * 100 + MAJOR * 10000
CFBundleVersion = "MAJOR.MINOR.PATCH"

希望这篇文章能够帮助到一些人,因为目前这个问题已经被浏览了3000多次。


3

我在使用时遇到了与你相同的错误

cordova run android

我通过降级我项目中的 Android 平台来解决了这个问题

cordova platform remove android

那么

cordova platform add android@6

对我来说,6号效果很好。


1
在我的情况下,错误来自于我的插件,我通过在我的plugin.xml文件中添加以下行来修复它:
    <platform name="android">
        <preference name="android-compileSdkVersion" value="30" />
         ...

它又停止工作了。不知道为什么 Cordova 对我来说有点间歇性。 - MIWMIB
cordova platform add android 报错,但是构建仍在继续。将插件的 Gradle 文件更新为 dependencies { implementation "androidx.biometric:biometric:1.1.0" } 解决了我的问题。 - MIWMIB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接