Monaca プロジェクトに Cordova プラグインを複数組み込んだ場合、それぞれの Cordova プラグインの設定内容によって競合が発生し、ビルドが失敗する場合があります。
Android の場合、build.gradle
の設定をカスタマイズすることで、Cordova プラグインの設定競合を回避できる場合があります。
Monaca で利用している Cordova では、build-extras.gradle
ファイルを使用することで、build.gradle をカスタマイズする機能を提供しています。
Monaca で build-extras.gradle を使用する場合は、Cordova プラグインから使用します。
build-extras.gradle については、Configuring Gradle を参照してください。
Gradle プロパティー
Monaca でAndroid アプリ設定
からアプリのバージョンコードを直接指定した場合は、config.xml にある widget タグ内のandroid-versionCode
属性に設定されます。
バージョンコードを指定する場合は、Android アプリ設定から行うことができるのですが、Cordova が提供しているcdvVersionCode
プロパティーを使用することでも変更することができます。
この cdvVersionCode プロパティーの値は、android-versionCode の値よりも優先されます。
Cordova が提供している Gradle プロパティーについては、Setting Gradle Properties を参照してください。
postBuildExtras プロパティー
postBuildExtras プロパティーを使用することで、さまざまな Gradle の設定を行うことができます。
設定方法は、下記のように postBuildExtras プロパティーに、Gradle の設定を追加します。
ext.postBuildExtras = {
allprojects {
repositories {
maven {
url "https://maven.google.com"
}
jcenter()
}
}
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
configurations.all {
resolutionStrategy {
force 'com.android.support:support-v4:26.+'
}
}
}
このように設定することで、ビルド時に設定内容が反映されます。
Cordova プラグインで build-extras.gradle を使用する
Cordova プラグインで build-extras.gradle を使用する場合は、Cordova プラグインの設定ファイルplugin.xml
から、下記のように指定します。
<platform name="android">
<framework src="build-extras.gradle" custom="true" type="gradleReference"/>
</platform>
framework タグの src の値として、build-extras.gradle のパスを指定します。
cordova-plugin-build-extras プラグインのサンプル
build-extras.gradle を Cordova プラグインとして使用するために必要な最低限のファイルは下記になります。
- plugin.xml
- package.json
- build-extras.gradle
これらのファイルを含むフォルダーを Zip ファイルにすることで、Monaca プロジェクトに追加できるようになります。
cordova-plugin-build-extras プラグインのサンプル内容は、以下になります。
build-extras.gradle の設定内容を変更して試してみてください。
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-build-extras" version="1.0.0">
<name>Cordova Plugin Build Extras</name>
<author>Monaca (https://ja.monaca.io/)</author>
<description>This plugin customizes the build.gradle.</description>
<license>Asial Corporation</license>
<engines>
<engine name="cordova-android" version=">=4.0.0" />
</engines>
<platform name="android">
<framework src="build-extras.gradle" custom="true" type="gradleReference"/>
</platform>
</plugin>
{
"name": "cordova-plugin-build-extras",
"version": "1.0.0",
"description": "This plugin customizes the build.gradle.",
"author": "Monaca Team",
"license": "COPYRIGHT ASIAL CORPORATION"
}
ext.cdvVersionCode = 30000
ext.cdvMinSdkVersion = 19
ext.postBuildExtras = {
allprojects {
repositories {
maven {
url "https://maven.google.com"
}
jcenter()
}
}
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
configurations.all {
resolutionStrategy {
force 'com.android.support:support-v4:26.+'
}
}
}
おわりに
build-extras.gradle は、Cordova プラグインから利用できるため、Monaca プロジェクトに追加した複数のサードパーティー製 Cordova プラグインに設定されている場合があります。
対象のプロジェクトに build-extras.gradle が複数設定されている場合、基本的には最後に設定された値が反映されますが、状況によっては正常に値が反映されない場合があります。
作成した build-extras.gradle の設定が反映されない場合は、一番最後に対象の Cordova プラグインを追加して試してみてください。