ไลบรารีการสนับสนุน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หมายเหตุ: การเปิดตัว Android 9.0 (API ระดับ 28) มี
ไลบรารีการสนับสนุนเวอร์ชันใหม่ที่ชื่อ
AndroidX ซึ่งเป็นส่วนหนึ่งของ Jetpack
ไลบรารี AndroidX
มีไลบรารีการสนับสนุนที่มีอยู่และยังมีคอมโพเนนต์ล่าสุดของ Jetpack ด้วย
คุณสามารถใช้ไลบรารีการสนับสนุนต่อไปได้
อาร์ติแฟกต์ในอดีต (เวอร์ชัน 27 และก่อนหน้าและจัดแพ็กเกจเป็น android.support.*
) จะ
จะยังคงอยู่ใน Google Maven อย่างไรก็ตาม การพัฒนาไลบรารีใหม่ทั้งหมด
จะปรากฏในไลบรารี AndroidX
เราขอแนะนำให้ใช้ไลบรารี AndroidX ในโปรเจ็กต์ใหม่ทั้งหมด คุณควรพิจารณา
ย้ายข้อมูลโปรเจ็กต์ที่มีอยู่ไปยัง AndroidX ด้วย
เมื่อพัฒนาแอปที่รองรับ API หลายเวอร์ชัน คุณอาจต้องการ
มาตรฐานในการให้บริการฟีเจอร์ใหม่ๆ ใน Android เวอร์ชันก่อนหน้า หรือ
กลับไปใช้ฟังก์ชันการทำงานที่เทียบเท่ากันอย่างนุ่มนวล แทนการสร้างโค้ด
ในการจัดการแพลตฟอร์มเวอร์ชันก่อนหน้า คุณสามารถใช้ประโยชน์จากไลบรารีเหล่านี้
เพื่อระบุเลเยอร์ความเข้ากันได้ นอกจากนี้ ไลบรารีการสนับสนุน
ให้ชั้นเรียนอำนวยความสะดวกเพิ่มเติมและฟีเจอร์เพิ่มเติมที่ไม่มีใน
API เฟรมเวิร์กมาตรฐานสำหรับการพัฒนาและการสนับสนุนที่ง่ายขึ้น
อุปกรณ์
เดิมทีไลบรารีการสนับสนุนของ Android เป็นไลบรารีไบนารีของแอปเพียงอันเดียว
ได้พัฒนาเป็นชุดไลบรารีสำหรับการพัฒนาแอป อยู่ในเกณฑ์ต่อไปนี้
ไลบรารีที่แนะนำ ถือเป็นส่วนสำคัญจริงๆ ของแอปพลิเคชันนี้
ที่กำลังพัฒนา
เอกสารนี้มีภาพรวมของไลบรารีการสนับสนุนเพื่อช่วยคุณ
เข้าใจคอมโพเนนต์และวิธีใช้งานในแอปอย่างมีประสิทธิภาพ
ข้อควรระวัง: ตั้งแต่ Support Library รุ่น 26.0.0 เป็นต้นไป
(กรกฎาคม 2017) ระดับ API ขั้นต่ำที่รองรับในไลบรารีการสนับสนุนส่วนใหญ่
เพิ่มขึ้นเป็น Android 4.0 (API ระดับ 14) สำหรับแพ็กเกจไลบรารีส่วนใหญ่ สำหรับ
ข้อมูลเพิ่มเติม โปรดดูที่การสนับสนุนเวอร์ชันและแพ็กเกจ
ชื่อในหน้านี้
การใช้งานสำหรับไลบรารีการสนับสนุน
การใช้งานที่แตกต่างสำหรับไลบรารีการสนับสนุน ย้อนกลับ
คลาสความเข้ากันได้สำหรับแพลตฟอร์มเวอร์ชันก่อนหน้าเป็นเพียงหนึ่งใน
ให้พวกเขา ตัวอย่างวิธีทั้งหมดในการใช้ไลบรารีการสนับสนุนมีดังนี้
ในแอปของคุณ
-
ความเข้ากันได้แบบย้อนหลังสำหรับ API รุ่นใหม่ - มี
ไลบรารีการสนับสนุนมีความเข้ากันได้แบบย้อนหลังสำหรับเฟรมเวิร์กที่ใหม่กว่า
คลาสและวิธีการ ตัวอย่างเช่น
Fragment
support Class จะรองรับ Fragment ในอุปกรณ์ที่ใช้เวอร์ชันต่างๆ
รุ่นก่อนหน้า Android 3.0 (API ระดับ 11)
-
ชั้นเรียนตามความสะดวกและตัวช่วย - ไลบรารีการสนับสนุนมี
โดยเฉพาะสำหรับการพัฒนาอินเทอร์เฟซผู้ใช้ สำหรับ
ตัวอย่างคลาส
RecyclerView
จะระบุผู้ใช้
วิดเจ็ตอินเทอร์เฟซสำหรับแสดงและจัดการรายการที่ยาวเกินไป ใช้งานได้
เวอร์ชัน Android ตั้งแต่ API ระดับ 7 ขึ้นไป
-
การแก้ไขข้อบกพร่องและยูทิลิตี - มีฟีเจอร์มากมาย
ซึ่งให้ประโยชน์นอกเหนือจากโค้ดที่คุณใช้ในแอป ซึ่งรวมถึง
support-annotations
ไลบรารีสำหรับการตรวจสอบโค้ดโดยตรงที่ดีขึ้นเกี่ยวกับอินพุตเมธอดและการรองรับ Multidex สำหรับ
การกำหนดค่าและเผยแพร่แอปด้วยเมธอดกว่า 65,536 รายการ
การใช้ API การสนับสนุนเทียบกับเฟรมเวิร์ก API
ไลบรารีการสนับสนุนมีคลาสและเมธอดที่คล้ายกับ API ใน
Android Framework เมื่อพบผลิตภัณฑ์นี้ คุณอาจสงสัยว่าคุณควร
ใช้เวอร์ชันเฟรมเวิร์กของ API หรือไลบรารีการสนับสนุนที่เทียบเท่า ที่นี่
คือแนวทางสำหรับกรณีที่คุณควรใช้ชั้นเรียนไลบรารีการสนับสนุน
ของ Framework API
-
ความเข้ากันได้สำหรับฟีเจอร์หนึ่งๆ - หากคุณต้องการรองรับ
ฟีเจอร์ล่าสุดของแพลตฟอร์มในอุปกรณ์ที่ใช้งานเวอร์ชันก่อนๆ
ให้ใช้คลาสและเมธอดที่เทียบเท่าจากไลบรารีการสนับสนุน
-
ความเข้ากันได้สำหรับฟีเจอร์ไลบรารีที่เกี่ยวข้อง - ซับซ้อนมากขึ้น
คลาสไลบรารีการสนับสนุนอาจขึ้นอยู่กับการสนับสนุนเพิ่มเติมอย่างน้อย 1 รายการ
คลาสไลบรารี ดังนั้นคุณควรใช้คลาสไลบรารีการสนับสนุนสำหรับ
ทรัพยากร Dependency ตัวอย่างเช่น
ViewPager
คลาสการสนับสนุนควรใช้กับชั้นเรียนการสนับสนุน FragmentPagerAdapter
หรือ FragmentStatePagerAdapter
-
ความเข้ากันได้กับอุปกรณ์ทั่วไป - หากคุณไม่มีอุปกรณ์
ฟีเจอร์แพลตฟอร์มที่คุณตั้งใจจะใช้กับแอปของคุณในแบบที่เข้ากันได้แบบย้อนหลัง
คุณยังควรใช้ชั้นเรียนไลบรารีการสนับสนุนในแอปของคุณ
เช่น คุณอาจต้องการใช้
ActivityCompat
แทนคลาส Activity
ของเฟรมเวิร์ก เพื่อใช้ประโยชน์จากฟีเจอร์ใหม่ๆ
ในภายหลัง เช่น การรวมรูปแบบการอนุญาตใหม่ไว้ใน
Android 6.0 (API ระดับ 23)
ชั้นเรียนไลบรารีสนับสนุนที่รองรับการใช้งานแพลตฟอร์มที่เข้ากันได้
คลาส API อาจไม่สามารถให้ฟังก์ชันการทำงานครบชุด
ในรุ่นล่าสุดได้ เนื่องจากข้อจำกัดของอุปกรณ์ที่เป็นโฮสต์
เวอร์ชันแพลตฟอร์ม ในกรณีเหล่านี้ ชั้นเรียน Support Library ได้รับการออกแบบมาเพื่อ
ลดระดับอย่างมีชั้นเชิง และอาจไม่ได้ให้ฟังก์ชันการทำงานหรือข้อมูลทั้งหมดของ
API ของแพลตฟอร์มในปัจจุบัน ด้วยเหตุนี้ คุณควรตรวจสอบข้อมูลอ้างอิง
สำหรับชั้นเรียนและเมธอดของไลบรารีที่คุณใช้
ทดสอบในอุปกรณ์ที่ใช้แพลตฟอร์มเวอร์ชันแรกสุดที่ระบบรองรับ
แอปของคุณ
หมายเหตุ: ไลบรารีการสนับสนุนไม่ได้ให้เนื้อหาที่เทียบเท่า
คลาสและเมธอดสำหรับ API ของเฟรมเวิร์กแต่ละรายการ ในบางกรณี คุณอาจต้อง
รวมการเรียก Method ของเฟรมเวิร์กด้วยการตรวจสอบเวอร์ชัน SDK ที่ชัดเจนและให้
โค้ดสำรองในการจัดการกับวิธีการที่ใช้งานไม่ได้ในอุปกรณ์ สำหรับข้อมูลเพิ่มเติม
ข้อมูลเกี่ยวกับการใช้การตรวจสอบเวอร์ชันในโค้ดของคุณ โปรดดูการสนับสนุนที่แตกต่างกัน
เวอร์ชันของแพลตฟอร์ม
การรองรับเวอร์ชันและชื่อแพ็กเกจ
แพ็กเกจไลบรารีการสนับสนุนบางรายการมีชื่อแพ็กเกจเพื่อระบุ
ระดับ API ขั้นต่ำ
ที่ผู้ใช้สนับสนุนตั้งแต่แรก
โดยใช้รูปแบบ v# เช่น
support-v4 เริ่มจาก Support Library เวอร์ชัน 26.0.0 (เปิดตัวแล้วใน
กรกฎาคม 2017) ระดับ API ขั้นต่ำที่รองรับได้เปลี่ยนเป็น Android 4.0 (API
ระดับ 14) สำหรับแพ็กเกจไลบรารีการสนับสนุนทั้งหมด ด้วยเหตุนี้ เมื่อทำงานร่วมกับ
ไลบรารีการสนับสนุนที่เผยแพร่ล่าสุด คุณไม่ควรคาดหวังว่า
สัญลักษณ์แพ็กเกจ v# จะระบุระดับการสนับสนุน API ขั้นต่ำ
การเปลี่ยนแปลงในรุ่นล่าสุดนี้ยังหมายความว่าแพ็กเกจห้องสมุดที่มีเวอร์ชัน 4
และ v7 ก็เทียบเท่ากันในระดับ API ขั้นต่ำที่รองรับ
ตัวอย่างเช่น ทั้งแพ็กเกจ support-v4 และแพ็กเกจ support-v7 รองรับการใช้งานขั้นต่ำ
API ระดับ 14 สำหรับรุ่นของ Support Library ตั้งแต่ 26.0.0 ขึ้นไป
เวอร์ชันที่เผยแพร่ของไลบรารีการสนับสนุน
เวอร์ชันที่เผยแพร่ของ Support Library เช่น 24.2.0 หรือ 25.0.1 คือ
แตกต่างจากระดับ API ขั้นต่ำที่ไลบรารีใน
รุ่น หมายเลขเวอร์ชันที่เผยแพร่จะระบุเวอร์ชันของแพลตฟอร์ม
API ที่สร้างโดยใช้ API ดังกล่าว ดังนั้น API ล่าสุดอาจเป็น
ในไลบรารีเวอร์ชันนี้
โดยเฉพาะอย่างยิ่งในส่วนแรกของหมายเลขเวอร์ชันที่เผยแพร่ ตัวอย่างเช่น
24 ในเวอร์ชัน 24.2.0 นั้นมักจะสอดคล้องกับเวอร์ชันของ
API ของแพลตฟอร์มที่พร้อมใช้งานเมื่อเปิดตัว ระดับเวอร์ชันการเผยแพร่ของ
ไลบรารีการสนับสนุนระบุว่ามีบางฟีเจอร์ของ API นั้นรวมอยู่ด้วย
แต่คุณไม่ควรคิดว่าจะเข้ากันได้กับ all
ที่เปิดตัวใน API แพลตฟอร์มเวอร์ชันใหม่
ทรัพยากร Dependency ของไลบรารี
ไลบรารีส่วนใหญ่ในชุดไลบรารีการสนับสนุนของ Android มีการขึ้นต่อกันบางส่วน
ไลบรารีอย่างน้อย 1 รายการ ตัวอย่างเช่น ไลบรารีการสนับสนุนเกือบทั้งหมดมี
ขึ้นอยู่กับแพ็กเกจ support-compat
โดยทั่วไป คุณไม่ต้อง
คุณต้องกังวลเกี่ยวกับ
support ไลบรารี เนื่องจากบิลด์ Gradle
เครื่องมือจะจัดการทรัพยากร Dependency ของไลบรารีให้คุณ โดยใส่โดยอัตโนมัติ
ไลบรารีที่เกี่ยวข้อง
หากต้องการดูว่ามีไลบรารีและทรัพยากร Dependency ใดบ้างรวมอยู่ใน
ให้เรียกใช้คำสั่งต่อไปนี้ที่รูทบิลด์ของการพัฒนาแอป
เพื่อรับรายงานทรัพยากร Dependency สำหรับโปรเจ็กต์ รวมถึง
ไลบรารีการสนับสนุนของ Android และไลบรารีอื่นๆ
gradle -q dependencies your-app-project:dependencies
ดูข้อมูลเพิ่มเติมเกี่ยวกับการเพิ่มไลบรารีการสนับสนุนลงในการพัฒนา
โดยใช้ Gradle โปรดดูการสนับสนุน
การตั้งค่าคลัง สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการใช้งาน Gradle โปรดดูที่
กำหนดค่า
บิลด์ของคุณ
โปรดทราบว่าไลบรารีการสนับสนุนของ Android ทั้งหมดขึ้นอยู่กับระดับพื้นฐานบางระดับด้วย
ของแพลตฟอร์มสำหรับรุ่นล่าสุด ซึ่งก็คือ Android 4.0 (API ระดับ 14) หรือ
สูงขึ้น
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2024-08-23 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"]],["อัปเดตล่าสุด 2024-08-23 UTC"],[],[],null,["# Support Library\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\n\nWhen developing apps that support multiple API versions, you may want a\nstandard way to provide newer features on earlier versions of Android or\ngracefully fall back to equivalent functionality. Rather than building code\nto handle earlier versions of the platform, you can leverage these libraries\nto provide that compatibility layer. In addition, the Support Libraries\nprovide additional convenience classes and features not available in the\nstandard Framework API for easier development and support across more\ndevices.\n\n\nOriginally a single binary library for apps, the Android Support Library has\nevolved into a suite of libraries for app development. Many of these\nlibraries are now a strongly recommended, if not essential, part of app\ndevelopment.\n\n\nThis document provides an overview of the support library to help you\nunderstand its components and how to use it effectively in your app.\n\n\n**Caution:** Starting with Support Library release 26.0.0\n(July 2017), the minimum supported API level across most support libraries\nhas increased to Android 4.0 (API level 14) for most library packages. For\nmore information, see [Version Support and Package\nNames](#api-versions) on this page.\n\nUses for the Support Libraries\n------------------------------\n\n\nThere are a few distinct uses for the support libraries. Backward\ncompatibility classes for earlier versions of the platform is just one of\nthem. Here is a more complete list of ways you can use the support libraries\nin your app:\n\n- **Backward Compatibility for newer APIs** - A large amount of the support libraries provide backward compatibility for newer framework classes and methods. For example, the [Fragment](/reference/androidx/fragment/app/Fragment) support class provides support for fragments on devices running versions earlier than Android 3.0 (API level 11).\n- **Convenience and Helper Classes** - The support libraries provides a number of helper classes, particularly for user interface development. For example the [RecyclerView](/reference/androidx/recyclerview/widget/RecyclerView) class provides a user interface widget for displaying and managing very long lists, useable on versions of Android from API level 7 and up.\n- **Debugging and Utilities** - There are a number of features that provide utility beyond code you incorporate into your app, including the [`support-annotations`](/studio/write/annotations) library for improved code lint checks on method inputs and [Multidex support](/studio/build/multidex) for configuring and distributing apps with over 65,536 methods.\n\nUsing Support versus Framework APIs\n-----------------------------------\n\n\nSupport Libraries provide classes and methods that closely resemble APIs in\nthe Android Framework. Upon discovering this, you may wonder if you should\nuse the framework version of the API or the support library equivalent. Here\nare the guidelines for when you should use support library classes in place\nof Framework APIs:\n\n- **Compatibility for a Specific Feature** - If you want to support a recent platform feature on devices that are running earlier versions of the platform, use the equivalent classes and methods from the support library.\n- **Compatibility for Related Library Features** - More sophisticated support library classes may depend on one or more additional support library classes, so you should use support library classes for those dependencies. For example, the [ViewPager](/reference/androidx/viewpager/widget/ViewPager) support class should be used with [FragmentPagerAdapter](/reference/androidx/fragment/app/FragmentPagerAdapter) or the [FragmentStatePagerAdapter](/reference/androidx/fragment/app/FragmentStatePagerAdapter) support classes.\n- **General Device Compatibility** - If you do not have a specific platform feature you intend to use with your app in a backward compatible way, it is still a good idea to use support library classes in your app. For example, you may want to use [ActivityCompat](/reference/androidx/core/app/ActivityCompat) in place of the framework [Activity](/reference/android/app/Activity) class, so you can take advantage of newer features later on, such as incorporating the new permissions model introduced in Android 6.0 (API level 23).\n\n\nSupport Library classes that provide a compatible implementation of platform\nAPI classes may not be able to provide the full set of functionality\navailable in the latest release, due to the limitations of the host device\nplatform version. In these cases, Support Library classes are designed to\ndegrade gracefully, and may not provide the all the functionality or data of\nthe current, platform API. For this reason, you should review the reference\ndocumentation for the library classes and methods you use, and thoroughly\ntest on devices running the earliest version of the platform supported by\nyour app.\n\n\n**Note:** The support libraries do not provide equivalent\nclasses and methods for each framework API. In some cases, you may need to\nwrap a framework method call with an explicit SDK version check and provide\nalternative code to handle methods not available on a device. For more\ninformation on using version checks in your code, see [Supporting Different\nPlatform Versions](/training/basics/supporting-devices/platforms).\n\nVersion Support and Package Names\n---------------------------------\n\n\nSome of the Support Library packages have package names to indicate the\nminimum level of the API they originally supported,\nusing a v# notation, such as the\nsupport-v4 package. Starting with Support Library version 26.0.0 (released in\nJuly 2017), the minimum supported API level has changed to Android 4.0 (API\nlevel 14) for all support library packages. For this reason, when working with\nany recent release of the support library, you should not assume that the\nthe *v#* package notation indicates a minimum API support level.\nThis change in recent releases also means that library packages with the v4\nand v7 are essentially equivalent in the minimum level of API they support.\nFor example, the support-v4 and the support-v7 package both support a minimum\nAPI level of 14, for releases of the Support Library from 26.0.0 and higher.\n\n### Support Library Release Versions\n\n\nThe [release version](/topic/libraries/support-library/revisions) of the Support Library, such as 24.2.0 or 25.0.1, is\ndifferent from the minimum API level supported by any library in that\nrelease.The release version number indicates which version of the platform\nAPI it was built against, and therefore, what the most recent APIs *may be\nincluded* in this version of the libraries.\n\n\nSpecifically, the first section of the release version number, for example\nthe 24 in version 24.2.0, generally corresponds with the version of the\nplatform API available when it was released. The release version level of the\nsupport library indicates it incorporates *some* features of that API\nlevel, but you should not assume it provides compatibility with *all*\nfeatures released in the new platform API version.\n\nLibrary Dependencies\n--------------------\n\n\nMost libraries in the Android Support Library suite have some dependency on\none or more libraries. For example, nearly all support libraries have a\ndependency on the `support-compat` package. In general, you do not\nneed to worry about support library dependencies, because the gradle build\ntool manages library dependencies for you, by automatically including\ndependent libraries.\n\n\nIf you want to see what libraries and library dependencies are included in\nyour app, run the following command at the build root of your app development\nproject to get a report of the dependencies for that project, including\nAndroid Support Libraries and other libraries: \n\n```\ngradle -q dependencies your-app-project:dependencies\n```\n\n\nFor more information about adding support libraries to your development\nproject using Gradle, see [Support\nLibrary Setup](/topic/libraries/support-library/setup). For more information about working with Gradle, see\n[Configure\nYour Build](/studio/build).\n\n\nNote that *all* Android Support Libraries also depend on some base level\nof the platform, for recent releases, that is Android 4.0 (API level 14) or\nhigher."]]