วันจันทร์ที่ 31 ธันวาคม พ.ศ. 2561

4. การเลือกใช้ Gradle และ Android Gradle plugin



The following table lists which version of Gradle is required for each version of the Android Gradle plugin. For the best performance, you should use the latest possible version of both Gradle and the plugin.
Plugin versionRequired Gradle version
1.0.0 - 1.1.32.2.1 - 2.3
1.2.0 - 1.3.12.2.1 - 2.9
1.5.02.2.1 - 2.13
2.0.0 - 2.1.22.10 - 2.13
2.1.3 - 2.2.32.14.1+
2.3.0+3.3+
3.0.0+4.1+
3.1.0+4.4+

distributionUrl = https\://services.gradle.org/distributions/gradle-4.6-all.zip
...
เค้าบอกว่า คุณต้องใช้  Android Gradle plugin  ให้สอดคล้องกับ Gradle และควรจะเลือกใช้ version ล่าสุด

จากข้อมูลที่ได้จากเว็บไซต์ข้างบน ผู้เขียนเลือกที่จะใช้  Gradle เวอร์ชั่น 4.6 ซึ่ง สามารถใช้ร่วมกับ

Android Gradle plugin เวอร์ชั่น 3.2.1และ   SDK Build Tools 28.0.3 
ซึ่งตัว  SDK Build Tools 28.0.3 ได้โหลดเตรียมไว้แล้วตั้งแต่ตอน   2. ดาวน์โหลดซอฟท์แวร์ ที่เกี่ยวข้อง

Gradle เวอร์ชั่น 4.6  ดาวน์โหลดได้ที่  https://services.gradle.org/distributions/   แต่ว่าเราจะไม่ดาวน์โหลดเอง หร๊อก... แต่เราจะเข้าไปแก้ไข config ในโปรเจ็คที่เราสร้างขึ้น จากนั้น สั่งรัน ตัวโปรเจ็คจะดาวน์โหลดไฟล์ทั้งหมดเอง



จากโฟล์เดอร์ของโปรเจ็ค ที่เราสร้างขึ้น
ไฟล์ที่เราจะเข้าไปแก้ไข ก็มีไฟล์  C:\Users\User\Desktop\AndroidProject\app\build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0<--  Gradle Plugin version
    }
}
apply plugin: 'android'

android {
    compileSdkVersion 'android-21'
    buildToolsVersion '28.0.3'

    buildTypes {
        release {
            runProguard false
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
}


แก้เป็น

buildscript {
    repositories {
        google()
jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'   <--  Gradle Plugin version
    }
}

allprojects {      <-- เพิ่มเข้ามา
    repositories {
        google()
        jcenter()
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 'android-21'
    buildToolsVersion '28.0.3'

    buildTypes {
        release {
            minifyEnabled false
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
}


และไฟล์ C:\Users\User\Desktop\AndroidProject\app\gradle\wrapper.properties

#Wed Apr 10 15:27:10 PDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip  <-- gradle version

แก้เป็น

#Wed Apr 10 15:27:10 PDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-4.6-all.zip <-- gradle version




ถึงตอนนี้ เราก็พร้อมที่จะรันคำสั่งต่อไป  โดยใช้คำสั่ง  gradlew ....

อ่านต่อในบทความตอนต่อไป

ไม่มีความคิดเห็น:

แสดงความคิดเห็น