การตั้งค่าไลบรารีการสนับสนุน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หมายเหตุ: การเปิดตัว Android 9.0 (API ระดับ 28) มาพร้อมกับ
ไลบรารีการสนับสนุนเวอร์ชันใหม่ที่ชื่อ AndroidX
ซึ่งเป็นส่วนหนึ่งของ Jetpack
ไลบรารี AndroidX
มีไลบรารีการสนับสนุนที่มีอยู่และยังมีคอมโพเนนต์ Jetpack ล่าสุดด้วย
คุณสามารถใช้ไลบรารีการสนับสนุนต่อไปได้
อาร์ติแฟกต์ย้อนหลัง (เวอร์ชัน 27 และก่อนหน้า และแพ็กเกจเป็น android.support.*
) จะยังคงพร้อมให้บริการใน Google Maven
อย่างไรก็ตาม การพัฒนาไลบรารีใหม่ทั้งหมดจะเกิดขึ้นในไลบรารี AndroidX
เราขอแนะนำให้ใช้ไลบรารี AndroidX ในโปรเจ็กต์ใหม่ทั้งหมด นอกจากนี้ คุณควรพิจารณาย้ายข้อมูลโปรเจ็กต์ที่มีอยู่ไปยัง AndroidX ด้วย
วิธีตั้งค่าไลบรารีการสนับสนุน Android ในโปรเจ็กต์การพัฒนาจะขึ้นอยู่กับฟีเจอร์ที่คุณต้องการใช้และช่วงเวอร์ชันแพลตฟอร์ม Android ที่คุณต้องการรองรับด้วยแอปพลิเคชัน
เอกสารนี้จะแนะนำขั้นตอนการดาวน์โหลดแพ็กเกจ Support Library และการเพิ่มไลบรารี
ลงในสภาพแวดล้อมการพัฒนา
ตอนนี้ไลบรารีการสนับสนุนพร้อมให้บริการผ่านที่เก็บ Maven ของ Google แล้ว
เราไม่รองรับการดาวน์โหลดไลบรารีผ่าน SDK
Manager อีกต่อไป และจะนำฟังก์ชันดังกล่าวออกในเร็วๆ นี้
การเลือกไลบรารีการสนับสนุน
ก่อนเพิ่มไลบรารีการสนับสนุนลงในแอปพลิเคชัน ให้ตัดสินใจว่าต้องการรวมฟีเจอร์ใด
และต้องการรองรับ Android เวอร์ชันต่ำสุดใด ดูข้อมูลเพิ่มเติมเกี่ยวกับฟีเจอร์
ที่ไลบรารีต่างๆ มีให้ได้ที่
ฟีเจอร์ของ Support Library
การเพิ่มไลบรารีการสนับสนุน
หากต้องการใช้ Support Library คุณต้องแก้ไขการอ้างอิง classpath ของโปรเจ็กต์แอปพลิเคชันภายในสภาพแวดล้อมการพัฒนา คุณต้องทำตามขั้นตอนนี้สำหรับ
Support Library แต่ละรายการที่ต้องการใช้
วิธีเพิ่มไลบรารีการสนับสนุนลงในโปรเจ็กต์แอปพลิเคชัน
- รวมที่เก็บ Maven ของ Google ไว้ในไฟล์
settings.gradle
ของโปรเจ็กต์
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
// If you're using a version of Gradle lower than 4.1, you must
// instead use:
//
// maven {
// url 'https://maven.google.com'
// }
}
}
- สำหรับแต่ละโมดูลที่คุณต้องการใช้ Support Library ให้เพิ่มไลบรารีในบล็อก
dependencies
ของไฟล์ build.gradle
ของโมดูล ตัวอย่างเช่น หากต้องการเพิ่มไลบรารี core-utils v4 ให้เพิ่มข้อมูลต่อไปนี้
dependencies {
...
implementation "com.android.support:support-core-utils:28.0.0"
}
ข้อควรระวัง: การใช้การขึ้นต่อกันแบบไดนามิก (เช่น
palette-v7:23.0.+
) อาจทำให้เกิดการอัปเดตเวอร์ชันที่ไม่คาดคิดและ
ความเข้ากันไม่ได้ของการถดถอย เราขอแนะนำให้คุณระบุเวอร์ชันไลบรารีอย่างชัดเจน (เช่น palette-v7:28.0.0
)
การใช้ API ของไลบรารีการสนับสนุน
คลาสใน Support Library ที่รองรับ API ของเฟรมเวิร์กที่มีอยู่มักจะมี
ชื่อเดียวกับคลาสของเฟรมเวิร์ก แต่จะอยู่ในแพ็กเกจคลาส android.support
หรือมีคำต่อท้าย *Compat
ข้อควรระวัง: เมื่อใช้คลาสจาก Support Library โปรดตรวจสอบว่าคุณได้นำเข้า
คลาสจากแพ็กเกจที่เหมาะสม ตัวอย่างเช่น เมื่อใช้คลาส ActionBar
android.support.v7.app.ActionBar
เมื่อใช้คลังการสนับสนุน
android.app.ActionBar
เมื่อพัฒนาสำหรับ API ระดับ 11 ขึ้นไปเท่านั้น
หมายเหตุ: หลังจากรวม Support Library ไว้ในโปรเจ็กต์แอปพลิเคชันแล้ว เราขอ
แนะนำเป็นอย่างยิ่งให้คุณลดขนาด ทำให้สับสน และเพิ่มประสิทธิภาพ
แอปสำหรับการเผยแพร่ นอกเหนือจากการปกป้องซอร์สโค้ดด้วยการปรับให้ยากต่อการอ่าน (Obfuscation) แล้ว การย่อ
ยังนำคลาสที่ไม่ได้ใช้ออกจากไลบรารีที่คุณรวมไว้ในแอปพลิเคชันด้วย ซึ่งจะช่วยให้
ขนาดการดาวน์โหลดของแอปพลิเคชันมีขนาดเล็กที่สุดเท่าที่จะเป็นไปได้
คำแนะนำเพิ่มเติมสำหรับการใช้ฟีเจอร์ Support Library บางอย่างมีอยู่ในชั้นเรียนการฝึกอบรม
คำแนะนำ
และตัวอย่างของนักพัฒนาแอป Android ดูข้อมูลเพิ่มเติมเกี่ยวกับคลาสและเมธอดของ Support Library แต่ละรายการได้ที่android.support
แพ็กเกจในเอกสารอ้างอิง API
การเปลี่ยนแปลงการประกาศไฟล์ Manifest
หากคุณเพิ่มความเข้ากันได้แบบย้อนหลังของแอปพลิเคชันที่มีอยู่กับ Android API เวอร์ชันก่อนหน้าโดยใช้ Support Library โปรดตรวจสอบว่าได้อัปเดตไฟล์ Manifest ของแอปพลิเคชันแล้ว โดยคุณควรอัปเดตองค์ประกอบ android:minSdkVersion
ของแท็ก
<uses-sdk>
ในไฟล์ Manifest เป็นหมายเลขเวอร์ชันใหม่ที่ต่ำกว่า ดังที่แสดงด้านล่าง
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
การตั้งค่าไฟล์ Manifest จะบอก Google Play ว่าแอปพลิเคชันของคุณสามารถติดตั้งในอุปกรณ์ที่ใช้ Android 4.0 (API ระดับ 14) ขึ้นไปได้
หากคุณใช้ไฟล์บิลด์ Gradle การตั้งค่า minSdkVersion
ในไฟล์บิลด์
จะลบล้างการตั้งค่า Manifest
plugins {
id 'com.android.application'
}
android {
...
defaultConfig {
minSdkVersion 16
...
}
...
}
ในกรณีนี้ การตั้งค่าไฟล์บิลด์จะบอก Google Play ว่าสามารถติดตั้งตัวแปรบิลด์เริ่มต้นของแอปพลิเคชันในอุปกรณ์ที่ใช้ Android 4.1 (API ระดับ 16) ขึ้นไปได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับตัวแปรบิลด์ได้ที่ภาพรวมระบบบิลด์
หมายเหตุ: หากคุณรวมไลบรารีสนับสนุนหลายรายการ SDK เวอร์ชันขั้นต่ำต้องเป็นเวอร์ชันสูงสุดที่ไลบรารีที่ระบุต้องการ เช่น หากแอปมีทั้งไลบรารีการรองรับ Preference v14 และไลบรารี Leanback v17 เวอร์ชัน SDK ขั้นต่ำต้องเป็น 17 ขึ้นไป
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-21 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-08-21 UTC"],[],[],null,["# Support Library Setup\n\n**Note:** With the release of Android 9.0 (API level 28) there is\na new version of the support library called\n[AndroidX](/jetpack/androidx) which is part of [Jetpack](/jetpack).\nThe AndroidX library\ncontains the existing support library and also includes the latest Jetpack components.\n\n\u003cbr /\u003e\n\n\nYou can continue to use the support library.\nHistorical artifacts (those versioned 27 and earlier, and packaged as `android.support.*`) will\nremain available on Google Maven. However, all new library development\nwill occur in the [AndroidX](/jetpack/androidx) library.\n\n\u003cbr /\u003e\n\n\nWe recommend using the AndroidX libraries in all new projects. You should also consider\n[migrating](/jetpack/androidx/migrate) existing projects to AndroidX as well.\n\nHow you setup the Android Support Libraries in your development project depends on what features\nyou want to use and what range of Android platform versions you want to support with your\napplication.\n\nThis document guides you through downloading the Support Library package and adding libraries\nto your development environment.\n\nThe support libraries are now available through Google's Maven\nrepository. We no longer support downloading the libraries through the SDK\nManager, and that functionality will be removed soon..\n\nChoosing Support Libraries\n--------------------------\n\nBefore adding a Support Library to your application, decide what features you want to include\nand the lowest Android versions you want to support. For more information on the features\nprovided by the different libraries, see\n[Support Library Features](/tools/support-library/features).\n\nAdding Support Libraries\n------------------------\n\nIn order to use a Support Library, you must modify your application's project's\nclasspath dependencies within your development environment. You must perform this procedure for\neach Support Library you want to use.\n\nTo add a Support Library to your application project:\n\n1. Include Google's Maven repository in your project's `settings.gradle` file. \n\n ```groovy\n dependencyResolutionManagement {\n repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)\n repositories {\n google()\n\n // If you're using a version of Gradle lower than 4.1, you must\n // instead use:\n //\n // maven {\n // url 'https://maven.google.com'\n // }\n }\n }\n ```\n2. For each module in which you want to use a Support Library, add the library in the `dependencies` block of the module's `build.gradle` file. For example, to add the v4 core-utils library, add the following: \n\n ```groovy\n dependencies {\n ...\n implementation \"com.android.support:support-core-utils:28.0.0\"\n }\n ```\n\n\n**Caution:** Using dynamic dependencies (for example,\n`palette-v7:23.0.+`) can cause unexpected version updates and\nregression incompatibilities. We recommend that you explicitly specify a\nlibrary version (for example, `palette-v7:28.0.0`).\n\nUsing Support Library APIs\n--------------------------\n\nSupport Library classes that provide support for existing framework APIs typically have the\nsame name as framework class but are located in the `android.support` class packages,\nor have a `*Compat` suffix. \n**Caution:** When using classes from the Support Library, be certain you import\nthe class from the appropriate package. For example, when applying the `ActionBar`\nclass:\n\n- `android.support.v7.app.ActionBar` when using the Support Library.\n- `android.app.ActionBar` when developing only for API level 11 or higher.\n\n\n**Note:** After including the Support Library in your application project, we\nstrongly recommend that you [shrink, obfuscate, and optimize\nyour app](/studio/build/shrink-code) for release. In addition to protecting your source code with obfuscation, shrinking\nremoves unused classes from any libraries you include in your application, which keeps the\ndownload size of your application as small as possible.\n\nFurther guidance for using some Support Library features is provided in the Android developer\n[training classes](/training),\n[guides](/guide/components)\nand samples. For more information about the individual Support Library classes and methods, see\nthe [android.support](/reference/android/support/v4/app/package-summary) packages in the API reference.\n\n### Manifest Declaration Changes\n\nIf you are increasing the backward compatibility of your existing application to an earlier\nversion of the Android API with the Support Library, make sure to update your application's\nmanifest. Specifically, you should update the `android:minSdkVersion`\nelement of the [`\u003cuses-sdk\u003e`](/guide/topics/manifest/uses-sdk-element) tag in the manifest to the new, lower version number, as\nshown below: \n\n```xml\n \u003cuses-sdk\n android:minSdkVersion=\"14\"\n android:targetSdkVersion=\"23\" /\u003e\n```\n\nThe manifest setting tells Google Play that your application can be installed on devices with Android\n4.0 (API level 14) and higher.\n\nIf you are using Gradle build files, the `minSdkVersion` setting in the build file\noverrides the manifest settings. \n\n```groovy\nplugins {\n id 'com.android.application'\n}\n\nandroid {\n ...\n\n defaultConfig {\n minSdkVersion 16\n ...\n }\n ...\n}\n```\n\nIn this case, the build file setting tells Google Play that the default build variant of your\napplication can be installed on devices with Android 4.1 (API level 16) and higher. For more\ninformation about build variants, see\n[Build System Overview](/studio/build).\n\n\n**Note:** If you are including several support libraries, the\nminimum SDK version must be the *highest* version required by any of\nthe specified libraries. For example, if your app includes both the [v14 Preference Support library](/topic/libraries/support-library/features#v14-preference) and the\n[v17 Leanback library](/topic/libraries/support-library/features#v17-leanback), your minimum\nSDK version must be 17 or higher."]]