久々の投稿になる。
今回は地図サービスのMapboxをAndroid StudioのGradleで設定する際に、公式ドキュメントどおりにならなくてキレかけたという投稿だ。
環境
- Android Studio Girrafe
- Gradle 8.0
公式によると
Mapboxの公式はバージョン違いによる設定を2つ紹介してくれている
Android Studio less than Arctic Fox (2020.3.1) and Gradle less than v6.0
project-level build.gradle
allprojects {
repositories {
maven {
url 'https://api.mapbox.com/downloads/v2/releases/maven'
authentication {
basic(BasicAuthentication)
}
credentials {
// Do not change the username below.
// This should always be `mapbox` (not your username).
username = "mapbox"
// Use the secret token you stored in gradle.properties as the password
password = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: ""
}
}
}
}
Android Studio Arctic Fox (2020.3.1) or later and Gradle v6.0 or later
自分の場合はこちらの設定。
settings.gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
url 'https://api.mapbox.com/downloads/v2/releases/maven'
authentication {
basic(BasicAuthentication)
}
credentials {
// Do not change the username below.
// This should always be `mapbox` (not your username).
username = "mapbox"
// Use the secret token you stored in gradle.properties as the password
password = MAPBOX_DOWNLOADS_TOKEN
}
}
}
}同じように記述するも「url」と「basic」でエラー発生。
全然できねーじゃねーかよ!!!!!
調べ始めて1時間くらいでなんとか現状を打破する記事を発見。
しかし、これだけではcredentials内のpasswordで再びエラー。
なんでだめなんだよ!!!!
しばらく調べて、ヒントになる記事を発見。
gradle.propertiesに以下のように記述。
systemProp.MAPBOX_DOWNLOADS_TOKEN=[自分のトークン]
それを以下のように呼び出す。
settings.gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
url = uri("https://api.mapbox.com/downloads/v2/releases/maven")
authentication {
create<BasicAuthentication>("basic")
}
credentials {
// Do not change the username below.
// This should always be `mapbox` (not your username).
username = "mapbox"
// Use the secret token you stored in gradle.properties as the password
password = System.getProperty("MAPBOX_DOWNLOADS_TOKEN")
}
}
}
}
追記
上記の方法だとトークンをgitに公開することになる。
それだとまずいのでlocal.propertiesにトークンを記述して読み出す。
もちろんlocal.propertiesは.gitignoreで排除すること。
settings.gradle
maven {
url = uri("https://api.mapbox.com/downloads/v2/releases/maven")
authentication {
create<BasicAuthentication>("basic")
}
credentials {
// Do not change the username below.
// This should always be `mapbox` (not your username).
username = "mapbox"
val prop = Properties().apply {
load(FileInputStream(File(rootProject.projectDir, "local.properties")))
}
val key = prop.getProperty("MAPBOX_DOWNLOADS_TOKEN")
// Use the secret token you stored in gradle.properties as the password
password = key
}
}local.propertiesには以下のように記述。
MAPBOX_DOWNLOADS_TOKEN=[自分のトークン]
これでうまくいきました。
Mapboxよ、全然ユーザに優しくないのな。