Skip to content

Instantly share code, notes, and snippets.

@dEn13L
Created December 18, 2024 11:12
Show Gist options
  • Select an option

  • Save dEn13L/caeab0ce4e6c7662e398e94a5d6fbffe to your computer and use it in GitHub Desktop.

Select an option

Save dEn13L/caeab0ce4e6c7662e398e94a5d6fbffe to your computer and use it in GitHub Desktop.
def getSecureProperty(String propertyName) throws MissingSecurePropertyException {
def property = project.findProperty(propertyName) ?: System.getenv(propertyName)
if (property != null) {
return property
} else {
throw new MissingSecurePropertyException(propertyName)
}
}
class MissingSecurePropertyException extends RuntimeException {
private final String property
MissingSecurePropertyException(String property) {
super()
this.property = property
}
@Override
String getMessage() {
return "The property \"${property}\" is not found. " +
"Please add this property to ~/.gradle/gradle.properties."
}
}
ext {
getSecureProperty = this.&getSecureProperty
}
@dEn13L
Copy link
Author

dEn13L commented Dec 18, 2024

  1. Нужно найти/создать файл gradle.properties в корне вашего юзера в системе (Users/<username>/.gradle/gradle.properties)
  2. Сохранить в нем ключ systemProp.org.gradle.project.yandexMapKitApiKey=<API_KEY>
  3. Добавить в проект файл secure-properties-provider.gradle
  4. В файле app/build.gradle добавить
def securePropertiesProviderPlugin = file '../secure-properties-provider.gradle' // тут должен быть правильный путь до файла
apply from: securePropertiesProviderPlugin

android {
    defaultConfig {
        buildConfigField "String", "MAPKIT_API_KEY", "\"${getSecureProperty("yandexMapKitApiKey")}\""
    }
}

После этого в коде можно получить ключ как BuildConfig.MAPKIT_API_KEY

@dEn13L
Copy link
Author

dEn13L commented Jan 8, 2025

Также нужно включить фичу buildConfig

android {
    buildFeatures {
        buildConfig = true
    }
}

@dEn13L
Copy link
Author

dEn13L commented Jan 8, 2025

Но можно не создавать отдельный файл

Пример для gradle.kts

android {
    defaultConfig {
        buildConfigField(
            type = "String",
            name = "MAPKIT_API_KEY",
            value = "\"${project.findProperty("yandexMapKitApiKey")}\"",
        )
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment