รวมประกาศโอเพนซอร์ส

SDK ของบริการ Google Play บางครั้งจะมีหรือต้องใช้ไลบรารีโอเพนซอร์ส คุณในฐานะนักพัฒนาแอปมีหน้าที่รับผิดชอบในการแสดงประกาศสำหรับไลบรารีโอเพนซอร์สที่แอปใช้ให้เหมาะสม เพื่อให้เป็นไปตามข้อกำหนดของใบอนุญาตของไลบรารีโอเพนซอร์ส

บริการ Google Play มีชุดเครื่องมือที่ออกแบบมาเพื่อช่วยให้นักพัฒนาแอปแสดงประกาศซอฟต์แวร์โอเพนซอร์ส (OSS) ของไลบรารีที่ใช้ในแอปได้ง่ายขึ้น oss-licenses-plugin และ SDK ของบริการ Google Play oss-licenses จะรวบรวมข้อกำหนดของใบอนุญาตจากไลบรารีที่รวมไว้ตามที่ประกาศไว้ใน ไฟล์ POM และสร้าง กิจกรรม ที่ใช้แสดงข้อกำหนดเหล่านี้ได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับ วิธีที่เครื่องมือค้นหาและจัดแพ็กเกจข้อมูลใบอนุญาต

เพิ่มปลั๊กอิน Gradle

ใน pluginManagement ของการตั้งค่าโปรเจ็กต์ ให้ทำดังนี้

  1. รวมที่เก็บ Maven ของ Google .
  2. แก้ไขปลั๊กอิน 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.12.0")
      }
    }
  }
}

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.12.0")
      }
    }
  }
}

ในไฟล์บิลด์ระดับแอป ให้ใช้ปลั๊กอินโดยเพิ่มบรรทัดต่อไปนี้ภายใต้การประกาศปลั๊กอิน 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.5.1")

Groovy DSL

build.gradle

implementation 'com.google.android.gms:play-services-oss-licenses:17.5.1'

แสดงข้อมูลใบอนุญาต

เมื่อแอปบิลด์ ปลั๊กอิน Gradle จะประมวลผลใบอนุญาตและเพิ่มใบอนุญาตลงในทรัพยากรของแอป หากต้องการแสดงใบอนุญาตอย่างง่ายดาย คุณสามารถเปิดกิจกรรมที่ไลบรารี play-services-oss-licenses จัดเตรียมไว้ให้ในจุดที่เหมาะสมในแอป ดังที่แสดงในข้อมูลโค้ดต่อไปนี้

Kotlin

import com.google.android.gms.oss.licenses.v2.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.v2.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));

ใช้ธีมกับกิจกรรม

คุณสามารถใช้ธีมกับ OssLicensesMenuActivity ได้หลายวิธี โดยขึ้นอยู่กับว่าแอปใช้ธีม Material 3 หรือธีมที่อิงตาม XML

แอปที่ใช้ Material 3

สำหรับแอปที่ใช้ Material 3 คุณสามารถเรียก setTheme() เมธอดเพื่อใช้ธีมที่กำหนดเองกับกิจกรรม ดังที่แสดงในข้อมูลโค้ด ต่อไปนี้:

Kotlin

import com.google.android.gms.oss.licenses.v2.OssLicensesMenuActivity
...

OssLicensesMenuActivity.setTheme(lightColorScheme, darkColorScheme, typography)
startActivity(Intent(this, OssLicensesMenuActivity::class.java))

Java

import com.google.android.gms.oss.licenses.v2.OssLicensesMenuActivity;
...

OssLicensesMenuActivity.setTheme(lightColorScheme, darkColorScheme, typography);
startActivity(new Intent(this, OssLicensesMenuActivity.class));

แอปที่ใช้ธีม XML

สำหรับแอปที่ใช้ธีม XML คุณสามารถใช้ธีมกับกิจกรรมในไฟล์ Manifest เพื่อให้ตรงกับธีมที่ใช้ในกิจกรรมอื่นๆ ของแอป หากต้องการทำเช่นนั้น ให้รวมกิจกรรมใบอนุญาตโอเพนซอร์สไว้ในองค์ประกอบ <activity> ภายในไฟล์ Manifest ของแอป ดังที่แสดงในข้อมูลโค้ดต่อไปนี้

<application android:theme="@style/AppTheme" ...>
    <activity
        android:name="com.google.android.gms.oss.licenses.v2.OssLicensesMenuActivity"
        tools:replace="android:theme"
        android:theme="@style/AppTheme" />
</application>

วิธีกำหนดรายการใบอนุญาต

ในเวลาคอมไพล์ ปลั๊กอิน Gradle จะสแกนทรัพยากร Dependency ของ POM ของโปรเจ็กต์แอป เมื่อมี Maven POM สำหรับทรัพยากร Dependency โดยตรงของแอป ปลั๊กอินจะ ประมวลผลองค์ประกอบ <licenses> แต่ละรายการ และฝังลิงก์และชื่อของใบอนุญาตแต่ละรายการลงในชิ้นงาน Android ที่ รวมอยู่ในแอป