目次
-
バージョンカタログとは何ですか?
バージョン カタログは、AGP7.0 以降に開始された依存関係のバージョン管理方法です。
このとき、友人の中には「以前は他のバージョン管理方法があったのに、とても使いやすいのに、なぜこれを使うのですか?」と尋ねる人もいるでしょう。
この質問を念頭に置いて、以前に一般的に使用されていた依存関係管理方法を比較してみましょう。
-
依存関係管理方法の比較
内線 | バージョンカタログ | |
---|---|---|
宣言ドメイン | *.gradle | *.toml / sting.gradle |
変更可能ですか | はい | いいえ |
言葉遣い | 追加機能 | xxx.xxx.xxxの文言を修正 |
チェック | なし | 同期チェック |
内線:
つまり、.gradleファイルごとに宣言できるのですが、この宣言方法が断片化してしまい、後から変数を定義する際に同じ変数を定義すると元の変数が上書きされてしまいます(チーム開発時に発生しやすい問題です)また、比較的カジュアルであり、各開発者の記述スタイルが異なる可能性があるため、依存関係管理よりも混乱が発生し、コンパイル時や同期時の検証プロセスはありません。
バージョン カタログ:
この方法での宣言はすべて、固定ファイル *.toml または sting.gradle に書き込まれます。ext 内で変数名の重複が発生することはありません。これにより、後で宣言された変数が以前に定義された変数と置き換わることはありません。同期検証ができるため、チームの開発に役立ち、各モジュールで依存関係のバージョンの競合が起こりにくい
-
具体的な使い方
バージョン管理タイプ | タイプの説明 | 定義例 | 使用例 app/build.gradle |
図書館 | arr、jar バージョン管理 | library( 'core-ktx', 'androidx.core', 'core-ktx') .version('1.7.0') |
API グループ名.core.ktx |
バンドル | 依存関係の集約管理 | バンドル('androidx', ['core-ktx', 'constraintlayout', 'appcompat'] ) |
APIグループ名.androidx |
バージョン | バージョン番号の一定管理 | バージョン('compileSdk','33') |
API グループ名.バージョン .compileSdk .get() .toInteger() |
プラグイン | Gradleプラグインのバージョン管理 | plugin('hilt-android', 'com.google.dagger.hilt.android' ).version('2.41') |
グループ名.plugins.hilt.android |
sting.gradle の例:
pluginManagement { ... } //1.启用Version Catalogs enableFeaturePreview('VERSION_CATALOGS') dependencyResolutionManagement { ... //2.在这里添加versionCatalogs versionCatalogs{ //3.创建一个分组名称 这里起名androidxLibs create('androidxLibs'){ //4.依赖管理分组定义 //如:api'androidx.core:core-ktx:1.7.0' //参数:(别名(自己看着取),group,artifact,version) library('core-ktx','androidx.core','core-ktx').version('1.7.0') library('appcompat','androidx.appcompat','appcompat').version('1.6.1') //5.依赖聚合管理 即:将多个依赖进行合并,引用时只需要调用聚合管理分组即可 //参数:(引用时的别名,[需要聚合的别名,需要聚合的别名]) bundle('androidx',['core-ktx','appcompat']) } //可以创建多个分组 create('googleLibs'){ library('material','com.google.android.material','material').version('1.9.0') } /** * 6.版本号常量管理 * 在app/build.gradle中 * compileSdk buildsdk.versions.compileSdk.get().toInteger() */ create('buildsdk'){ version('compileSdk','33') version('minSdk','24') version('targetSdk','33') } /* 7.插件管理 * 只需在app/build.gradle中alias(pluginLibs.plugins.hilt.android)即可 * 对于没有发布到gradlePluginPortal的插件,暂时沿用老的模式,即buildScript { dependencies{ classpath 'xxx.xxx.xxx:1.0.0'}} */ create('pluginLibs'){ plugin('application','com.android.application').version('7.4.1') } //8.引用文件(在根目录当中创建libs.version.toml文件,示例代码在下面) //直接将四种类型的管理放到文件当中 create("libs"){ from(files("libs.version.toml")) } } }
ルートディレクトリにlibs.version.tomlを作成します。
ps: 書き方は基本的に決まっています
。
#版本管理 [versions] compileSdk = '33' minSdk = '24' targetSdk = '33' #插件管理 [plugins] android-application = { id = "com.android.application", version = "7.4.1" } android-library = { id = "com.android.library", version = "7.4.1" } #依赖管理 [libraries] core-ktx = { module = "androidx.core:core-ktx", version = "1.7.0" } appcompat = { module = "androidx.appcompat:appcompat", version = "1.6.1" } constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version = "2.1.4" } #依赖聚合 [bundles] androidx = ["core-ktx", "appcompat", "constraintlayout"]
app/bundile.gradles の例:
plugins { //1.引用插件示例 alias(pluginLibs.plugins.application) } android { namespace 'com.techme.jetpack_android_online' //2.版本管理引用示例: compileSdk buildsdk.versions.compileSdk.get().toInteger() defaultConfig { applicationId "com.techme.jetpack_android_online" minSdk buildsdk.versions.minSdk.get().toInteger() targetSdk buildsdk.versions.targetSdk.get().toInteger() versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } dependencies { //3.依赖管理引用示例 implementation googleLibs.material //4.聚合依赖管理示例 implementation androidxLibs.bundles.androidx //5.引用libs.version.toml文件示例 implementation libs.constraintlayout }
-
予防
テスト後、*.toml ファイルの形式を使用して時間依存関係を引用すると、他のファイルと同じではなくなります。
比較 →
ps: libs はグループ名です
依存関係の参照:
libs.constraintlayout
は: グループ名、エイリアスその他 (アグリゲーション、プラグイン、バージョン):
集約: グループ名.bundles.alias
プラグイン: グループ名.plugins.alias
バージョン: グループ名.バージョン.エイリアス
-
述べる
この記事は基本的に俗語での解説であり、個人的な勉強メモとも言えますが、専門用語はあまり多くありませんので、間違っているところがあればご指摘ください。