Solving `version conflict either by updating the version of the google-services plugin`

Posted on
ionic fixing

แก้ปัญหา Exception Execution failed for task ‘:processDebugGoogleServices’.

> Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 9.0.0.

ท้าวความไปว่าใน ionic project ที่ทำอยู่ใช้ ionic native ซึ่งต้องใช้ Google Play Services คือ Google Maps, Firebase Messaging (FCM)

เมื่อใส่ทั้งตัวเข้าไป พอสั่ง ionic build android แล้วมันจะพัง เจอ exception แบบนี้

* What went wrong:
Execution failed for task ':processDebugGoogleServices'.
> Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 9.0.0.

สาเหตุ

เกิดจากนักพัฒนา plugin แต่ละตัวก็เป็นคนละคนกัน software ของ Google ก็พัฒนาไปเรื่อยๆเช่นกัน พอจับ plugin 2 ตัวมาใช้งานใน project เดียวกัน แต่เรียกใช้งาน software ของ Google version ไม่ตรงกัน อันนึงเก่า อันนึงใหม่ เกิดความเข้ากันไม่ได้ขึ้นมา

ทางแก้ปัญหา*

หมายเหตุ ผู้เขียนทดลองกับ Ionic version 3.5.2, @ionic-native/google-maps 3.10.3, >cordova-plugin-googlemaps 1.4.0, @ionic-native/fcm 4.1.0, cordova-plugin-fcm 2.1.2 แล้ว แก้ไขได้ build Android project ได้

ไล่อ่าน ionic forum, github issues ทดลองหลายกระบวนท่า ท่าต่อไปนี้ได้ผล แก้ไขให้ build ได้แน่นอน

ผมใช้ plugin ตัวนี้ cordova-plugin-fcm มันใช้ google play version เก่ากว่า plugin ตัวอื่นเค้า

1 ให้ทำการ remove platform android ออกจาก project เราก่อน

cordova platform rm android

2 เปิดไฟล์ build.gradle ที่อยู่ภายใต้ android folder platforms/android/build.gradle หาด้วย keyword play-services- เมื่อเจอแล้วเราจะได้เลข version จดเอาไว้ก่อน ซึ่งก็คือ 9.8.0

compile "com.google.android.gms:play-services-maps:9.8.0"

3 แก้ไข code ใน plugin cordova-plugin-fcm ไฟล์จะอยู่ที่ path plugins/cordova-plugin-fcm/plugin.xml

...
  <framework src="com.google.firebase:firebase-core:+" />
  <framework src="com.google.firebase:firebase-messaging:+" />
...

แก้ให้ + เป็นเลข version ที่จดเอาไว้จากขั้นตอนก่อนหน้านี้

...
  <framework src="com.google.firebase:firebase-core:9.8.0" />
  <framework src="com.google.firebase:firebase-messaging:9.8.0" />
...

3 แก้ไข code ใน plugins/cordova-plugin-fcm/src/android/FCMPlugin.gradle

buildscript {
	repositories {
            jcenter()
			mavenLocal()
        }
    dependencies {
        classpath 'com.android.tools.build:gradle:+'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}
// apply plugin: 'com.google.gms.google-services'
// class must be used instead of id(string) to be able to apply plugin from non-root gradle file
apply plugin: com.google.gms.googleservices.GoogleServicesPlugin

ให้ลบหรือ comment

classpath 'com.google.gms:google-services:3.0.0' และ

apply plugin: com.google.gms.googleservices.GoogleServicesPlugin

ออก เป็นแบบนี้

buildscript {
	repositories {
            jcenter()
			mavenLocal()
        }
    dependencies {
        classpath 'com.android.tools.build:gradle:+'
        //classpath 'com.google.gms:google-services:3.0.0'
    }
}
// apply plugin: 'com.google.gms.google-services'
// class must be used instead of id(string) to be able to apply plugin from non-root gradle file
// apply plugin: com.google.gms.googleservices.GoogleServicesPlugin

6 ทำการเพิ่ม cordova platform android กลับเข้ามาอีกครั้ง

cordova platform add android

7 แก้ไขไฟล์ build.gradle ที่อยู่ภายใต้ android folder platforms/android/build.gradle

เพิ่ม classpath 'com.google.gms:google-services:3.1.0' เข้าไปใน dependecies

...
apply plugin: 'com.android.application'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    // Switch the Android Gradle plugin version requirement depending on the
    // installed version of Gradle. This dependency is documented at
    // http://tools.android.com/tech-docs/new-build-system/version-compatibility
    // and https://issues.apache.org/jira/browse/CB-8143
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'com.google.gms:google-services:3.1.0'
    }
}
...

ต่อมาในไฟล์เดียวกัน ให้เพิ่ม apply plugin: 'com.google.gms.google-services' ในท้ายๆไฟล์ อาจจะสังเกตว่า ให้อยู่ระหว่างนี้ // SUB-PROJECT DEPENDENCIES END กับ def promptForReleaseKeyPassword()

...
    compile "com.google.android.gms:play-services-maps:9.8.0"
    compile "com.google.android.gms:play-services-location:9.8.0"
    // SUB-PROJECT DEPENDENCIES END
}

def promptForReleaseKeyPassword() {
    if (!cdvReleaseSigningPropertiesFile) {
        return;
    }
...

เพิ่มบรรทัดนี้ลงไป apply plugin: 'com.google.gms.google-services'

...
    compile "com.google.android.gms:play-services-maps:9.8.0"
    compile "com.google.android.gms:play-services-location:9.8.0"
    // SUB-PROJECT DEPENDENCIES END
}

apply plugin: 'com.google.gms.google-services'

def promptForReleaseKeyPassword() {
    if (!cdvReleaseSigningPropertiesFile) {
        return;
    }
...

ทีนี้เราสั่ง ionic build android ได้แล้ว Hooray!

...
BUILD SUCCESSFUL


Total time: 32.424 secs

Built the following apk(s):
    ~/platforms/android/build/outputs/apk/android-debug.apk
...