cordova-android@12.0.1でビルドエラー

Android API31のアプリをAPI33に対応して書き出ししようとしてハマったメモです。

結論としては、config.xmlのnameをHelloCordovaから1文字でも変更するとビルドエラーになります。

何言っているか分からないと思いますので、順を追って説明します。

環境

PowerShell
PS> cordova -v
12.0.0 (cordova-lib@12.0.1)

PS> gradle -v

------------------------------------------------------------
Gradle 7.4.2
------------------------------------------------------------

Build time:   2022-03-31 15:25:29 UTC
Revision:     540473b8118064efcc264694cbcaa4b677f61041

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          19.0.2 (Oracle Corporation 19.0.2+7-44)
OS:           Windows 11 10.0 amd64

PS> javac -version
javac 19.0.2

ビルドエラーの再現

PowerShell
PS> cordova create test1019
Creating a new cordova project.

PS> cd test1019

PS> cordova platform add android
Using cordova-fetch for cordova-android
Adding android project...
Creating Cordova project for the Android platform:
        Path: platforms\android
        Package: io.cordova.hellocordova
        Name: HelloCordova
        Activity: MainActivity
        Android Target SDK: android-33
        Android Compile SDK: 33
Subproject Path: CordovaLib
Subproject Path: app
Android project created with cordova-android@12.0.1

PS> cordova build android
Checking Java JDK and Android SDK versions
ANDROID_HOME=C:\Users\tsunet\AppData\Local\Android\Sdk (recommended setting)
ANDROID_SDK_ROOT=undefined (DEPRECATED)
Using Android SDK: C:\Users\tsunet\AppData\Local\Android\Sdk

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
Subproject Path: CordovaLib
Subproject Path: app

> Task :CordovaLib:compileDebugRenderscript
The RenderScript APIs are deprecated. They will be removed in Android Gradle plugin 9.0. See the following link for a guide to migrate from RenderScript: https://developer.android.com/guide/topics/renderscript/migrate

> Task :CordovaLib:compileDebugJavaWithJavac
ノート:一部の入力ファイルは推奨されないAPIを使用またはオーバーライドしています。
ノート:詳細は、-Xlint:deprecationオプションを指定して再コンパイルしてください。

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 13s
50 actionable tasks: 50 executed
Built the following apk(s):
        E:\cordovatest\test1019\platforms\android\app\build\outputs\apk\debug\app-debug.apk

テンプレートから何も変更せずに、cordova platform add androidcordova build androidを実行すれば、当たり前ですがビルド成功します。

ここで、config.xmlを変更してみます。デフォルトから変更した後のXMLが以下です。

XML
<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>Hello Cordova</name>
    <description>Sample Apache Cordova App</description>
    <author email="dev@cordova.apache.org" href="https://cordova.apache.org">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
</widget>

<name>HelloCordova</name><name>Hello Cordova</name>に変更しました。

HelloとCordovaに半角スペースを1ついれました。

PowerShell
PS> cordova platform rm android
Removing android from cordova.platforms array in package.json

PS> cordova platform add android
Using cordova-fetch for cordova-android
Adding android project...
Creating Cordova project for the Android platform:
        Path: platforms\android
        Package: io.cordova.hellocordova
        Name: Hello Cordova
        Activity: MainActivity
        Android Target SDK: android-33
        Android Compile SDK: 33
Subproject Path: CordovaLib
Subproject Path: app
Android project created with cordova-android@12.0.1

PS> cordova build android
Checking Java JDK and Android SDK versions
ANDROID_HOME=C:\Users\tsunet\AppData\Local\Android\Sdk (recommended setting)
ANDROID_SDK_ROOT=undefined (DEPRECATED)
Using Android SDK: C:\Users\tsunet\AppData\Local\Android\Sdk

FAILURE: Build failed with an exception.

* Where:
Script 'E:\cordovatest\test1019\platforms\android\cdv-gradle-name.gradle'

* What went wrong:
Could not compile script 'E:\cordovatest\test1019\platforms\android\cdv-gradle-name.gradle'.
> startup failed:
  General error during conversion: Unsupported class file major version 63

(中略)

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 1s
Command failed with exit code 1: E:\gradle-7.4.2\bin\gradle.BAT -p E:\cordovatest\test1019\platforms\android wrapper -b E:\cordovatest\test1019\platforms\android\wrapper.gradle

これだけで、ビルド失敗。意味不明です。

ちなみに、元のテキストに戻せば再びビルド成功するので、XML保存時の問題ではありません。
<name>HelloCordova2</name>でも失敗しました。

もしかしたら、cordovaではなくgradleのバージョン依存の問題かもしれません。

いろいろ試す限り、config.xmlの他の個所を変更しても問題ありませんでした。