LoginSignup
4
4

More than 5 years have passed since last update.

build.gradle のカスタマイズについて

Posted at

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 の設定を追加します。

build-extras.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から、下記のように指定します。

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 プラグインとして使用するために必要な最低限のファイルは下記になります。

  1. plugin.xml
  2. package.json
  3. build-extras.gradle

これらのファイルを含むフォルダーを Zip ファイルにすることで、Monaca プロジェクトに追加できるようになります。
cordova-plugin-build-extras プラグインのサンプル内容は、以下になります。

build-extras.gradle の設定内容を変更して試してみてください。

plugin.xml
<?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>
package.json
{
  "name": "cordova-plugin-build-extras",
  "version": "1.0.0",
  "description": "This plugin customizes the build.gradle.",
  "author": "Monaca Team",
  "license": "COPYRIGHT ASIAL CORPORATION"
}
build-extras.gradle
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 プラグインを追加して試してみてください。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4