บางครั้ง SDK ของบริการ Google Play จะมีหรือขึ้นอยู่กับไลบรารีโอเพนซอร์ส คุณในฐานะนักพัฒนาแอปมีหน้าที่รับผิดชอบในการแสดงประกาศสำหรับไลบรารีโอเพนซอร์สที่แอปของคุณใช้ให้เหมาะสม เพื่อให้เป็นไปตามข้อกำหนดของใบอนุญาตสำหรับไลบรารีโอเพนซอร์ส
บริการ Google Play มีชุดเครื่องมือที่ออกแบบมาเพื่อช่วยให้นักพัฒนาแอปมีวิธีที่ง่ายขึ้นในการแสดงประกาศซอฟต์แวร์โอเพนซอร์ส (OSS) ของไลบรารีที่ใช้ในแอป oss-licenses-plugin
และ SDK บริการของ Google Play
oss-licenses จะรวบรวมข้อกำหนดของใบอนุญาตจากไลบรารีที่รวมไว้ตามที่ประกาศไว้ใน
ไฟล์ POM และสร้างกิจกรรม
ที่ใช้แสดงข้อกำหนดเหล่านี้ได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับ
วิธีที่เครื่องมือค้นหาและจัดแพ็กเกจข้อมูลใบอนุญาต
เพิ่มปลั๊กอิน Gradle
ในpluginManagement
ของการตั้งค่าโปรเจ็กต์ ให้ทำดังนี้
- รวมที่เก็บ Maven ของ Google
- แก้ไขปลั๊กอิน
oss-licenses
ในPluginManagement
ข้อมูลโค้ดต่อไปนี้แสดงขั้นตอนเหล่านี้
Kotlin DSL
settings.gradle.kts
pluginManagement { repositories { ... google() } resolutionStrategy { eachPlugin { if (requested.id.id == "com.google.android.gms.oss-licenses-plugin") { useModule("com.google.android.gms:oss-licenses-plugin:0.10.6") } } } }
Groovy DSL
build.gradle
pluginManagement { repositories { ... google() } resolutionStrategy { eachPlugin { if (requested.id.id == "com.google.android.gms.oss-licenses-plugin") { useModule("com.google.android.gms:oss-licenses-plugin:0.10.6") } } } }
ในไฟล์บิลด์ระดับแอป ให้ใช้ปลั๊กอินโดยเพิ่มบรรทัดต่อไปนี้
ภายใต้การประกาศที่มีอยู่ของปลั๊กอิน com.android.application
ที่
ด้านบนของไฟล์
Kotlin DSL
app/build.gradle.kts
plugins { id("com.android.application") id("com.google.android.gms.oss-licenses-plugin") }
Groovy DSL
app/build.gradle
plugins { id 'com.android.application' id 'com.google.android.gms.oss-licenses-plugin' }
คุณสามารถดูโค้ด ของปลั๊กอินนี้ได้ใน GitHub
เพิ่มไลบรารี play-services-oss-licenses
ลงในแอป
ในส่วน dependencies
ของไฟล์บิลด์ระดับแอป ให้เพิ่มทรัพยากร Dependency ในไลบรารี
play-services-oss-licenses
ดังนี้
Kotlin DSL
build.gradle.kts
implementation("com.google.android.gms:play-services-oss-licenses:17.2.1")
Groovy DSL
build.gradle
implementation 'com.google.android.gms:play-services-oss-licenses:17.2.1'
แสดงข้อมูลใบอนุญาต
เมื่อแอปสร้างขึ้น ปลั๊กอิน Gradle จะประมวลผลใบอนุญาตและเพิ่มลงในทรัพยากรของแอป
หากต้องการแสดงใบอนุญาตอย่างง่ายดาย คุณสามารถเปิดใช้งานกิจกรรม
ที่จัดทำโดยไลบรารี play-services-oss-licenses
ในจุดที่เหมาะสม
ในแอปได้ ดังที่แสดงในข้อมูลโค้ดต่อไปนี้
Kotlin
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity ... // When the user selects an option to see the licenses: startActivity(Intent(this, OssLicensesMenuActivity::class.java))
Java
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity; ... // When the user selects an option to see the licenses: startActivity(new Intent(this, OssLicensesMenuActivity.class));
เมื่อเปิดใช้กิจกรรม ระบบจะแสดงรายการไลบรารีโอเพนซอร์สที่ คอมไพล์ลงในแอป รวมถึงไลบรารีที่แอปใช้ ดังที่แสดงใน รูปที่ 1 ผู้ใช้สามารถแตะชื่อคลังเพื่อดูข้อมูลใบอนุญาตเพิ่มเติม สำหรับคลังนั้น
รูปที่ 1 กิจกรรมในเมนูใบอนุญาตจะแสดงรายการที่เลือกได้ของไลบรารีโอเพนซอร์ส ที่แอปใช้
ตั้งชื่อกิจกรรม
โดยค่าเริ่มต้น กิจกรรมที่แสดงจะมีชื่อว่า "ใบอนุญาตโอเพนซอร์ส" คุณปรับแต่งชื่อกิจกรรมได้โดยเรียกใช้
setActivityTitle()
ดังที่แสดงในข้อมูลโค้ดต่อไปนี้
Kotlin
OssLicensesMenuActivity.setActivityTitle(getString(R.string.custom_license_title))
Java
OssLicensesMenuActivity.setActivityTitle(getString(R.string.custom_license_title));
ใช้ธีมกับกิจกรรม
คุณสามารถใช้ธีมกับกิจกรรมเพื่อให้ตรงกับธีมที่ใช้ในกิจกรรมอื่นๆ ของแอป
โดยให้รวมกิจกรรมใบอนุญาตโอเพนซอร์สไว้ในองค์ประกอบ
<activity>
ภายในไฟล์ Manifest ของแอป ดังที่แสดงในข้อมูลโค้ดต่อไปนี้
<application android:theme="@style/AppTheme" ...> <activity android:name="com.google.android.gms.oss.licenses.OssLicensesMenuActivity" android:theme="@style/AppTheme" /> <activity android:name="com.google.android.gms.oss.licenses.OssLicensesActivity" android:theme="@style/AppTheme" /> </application>
วิธีกำหนดรายการใบอนุญาต
ในเวลาคอมไพล์ ปลั๊กอิน Gradle จะสแกนทรัพยากร Dependency POM ของโปรเจ็กต์
แอป เมื่อมี POM ของ Maven สำหรับการอ้างอิงโดยตรงของแอป ปลั๊กอินจะ
ประมวลผลองค์ประกอบ <licenses>
แต่ละรายการ
และฝังลิงก์และชื่อของใบอนุญาตแต่ละรายการในเนื้อหา Android ที่รวมอยู่ในแอป