Google Ads API 物料清單 (BOM) 會管理依附元件版本,避免與其他架構使用的 Guava 和 GAX 等程式庫發生依附元件衝突。BOM 可確保您使用的這些依附元件版本,與 Google Ads 用戶端程式庫測試的版本完全相同。
建議您採用這種方式使用用戶端程式庫構件。
Maven 設定 {#maven-configuration}
Maven 依附元件為:
<!-- Import the Bill of Materials (BOM) to ensure you're using compatible
versions of all google-ads libraries. -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.api-ads</groupId>
<artifactId>google-ads-bom</artifactId>
<version>41.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Add the google-ads dependency, without a version. The version is
managed by the BOM. -->
<dependency>
<groupId>com.google.api-ads</groupId>
<artifactId>google-ads</artifactId>
</dependency>
Gradle 設定
Gradle 依附元件為:
// Import the Bill of Materials (BOM).
implementation platform('com.google.api-ads:google-ads-bom:41.1.0')
// Add the google-ads dependency, without a version.
implementation 'com.google.api-ads:google-ads'
宣告 BOM 涵蓋的依附元件
Google Ads API BOM 包含多個常見程式庫的版本管理功能,例如 Guava、Protobuf、GAX 和 gRPC。為避免潛在的依附元件衝突,宣告 BOM 涵蓋的依附元件時,不得指定版本。BOM 會自動管理這些程式庫的版本,確保相容性。
舉例來說,如要在 Maven 中宣告 Guava 依附元件,請使用下列程式碼:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<!-- NO VERSION SPECIFIED -->
</dependency>
在 Gradle 中:
implementation 'com.google.guava:guava' // NO VERSION SPECIFIED
省略版本可讓 BOM 管理版本,有助於避免因依附元件版本不相容而導致的問題。依附元件衝突的常見指標包括 NoSuchMethodError 或 ClassNotFoundException,通常只要確保所有 BOM 管理的依附元件都未指定版本,即可解決這類問題。