Gradle BuildConfigFields Syntax - android

What could be wrong with this syntax? I am getting Gradle DSL method not found:'buildConfigFields()' error.
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.okason.drawingapp"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
def filesAuthorityValue = applicationId + ".files"
//Now you can use ${filesAuthority} in your manifest
manifestPlaceholders = [filesAuthority: filesAuthorityValue]
//Now you can use BuildConfig.FILES_AUTHORITY in our code
buildConfigFields "String", "FILES_AUTHORITY", "\"${filesAuthorityValue}\""
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

you should be using buildConfigField and not buildConfigFields
Change
//Now you can use BuildConfig.FILES_AUTHORITY in our code
buildConfigFields "String", "FILES_AUTHORITY", "\"${filesAuthorityValue}\""
to
//Now you can use BuildConfig.FILES_AUTHORITY in our code
buildConfigField "String", "FILES_AUTHORITY", "\"${filesAuthorityValue}\""
are you sure that buildConfigFields even exits ?

Related

Could not resolve dependency for retrofit

I still got error even after changing retrofit 2.3.0 to 2.6.2 I am stucking with this for a long time please help me to resolve this
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.blog"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:convertor-gson:2.6.2'
Your external libraries should be inside dependencies.
It should something like below
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.blog"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies{
// other external libraries
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:convertor-gson:2.6.2'
}

Programmatically get manifestPlaceholders

I want to read manifestPlaceholders' values in my code.
I found some code but they always return null.
My gradle is:
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.some.test"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
manifestPlaceholders = [app_id: "xxx123"]
}
debug {
manifestPlaceholders = [app_id: "xxx123"]
}
}
}
I found this code in Onesignal SDK to read values from gradle but it doesn't work:
try {
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
appId = bundle.getString("app_id");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
With this code, the bundle should contain manifestPlaceholders values, but it is always null.
what is the problem?
There is no way to get programmatically from your class file (java/kotlin). As –
CommonsWare suggested in comment you rather use buildConfigField or resValue to inject data into the app from Gradle.

flavour : override package name

I had a project with this package name : applicationId 'com.safa.auditzanjan'
Now I have created a flavour into other same project and I have added those project into this project. this new package name's project is:applicationId "com.safa.auditkhoramabad".
how could I set older package Name to this flavour?Thats mean I want to do override com.safa.auditkhoramabad for my new flavour to com.safa.auditzanjan?
***EDIT***********
this is my Independent gradle config project:
defaultConfig {
applicationId 'com.safa.auditzanjan'
minSdkVersion 15
targetSdkVersion 25
multiDexEnabled true
versionCode 56
versionName '1.8.56'
}
dexOptions {
jumboMode = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-debug'
}
}
productFlavors {
main {
signingConfig signingConfigs.SigningConfig1
}
ts {
signingConfig signingConfigs.SigningConfig1
applicationIdSuffix ".ts"
versionNameSuffix "-ts"
}
}
this is flavour gradle config:
zanjan {
applicationId 'com.safa.auditzanjan'
signingConfig signingConfigs.SigningConfig1
applicationIdSuffix ".zanjan"
versionNameSuffix "-zanjan"
versionCode 56
versionName '1.8.56'
resValue( "string", "app_name", "ممیزی زنجان")
}
You should be able to do something like following
productFlavors {
flavor1 {
applicationId "com.safa.auditzanjan"
}
flavor2 {
applicationId "com.safa.auditkhoramabad"
}

How do I include `applicationIdSuffix` in some generated resource value?

Imagine this setup:
android {
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
productFlavors {
foo {
applicationId defaultConfig.applicationId + '.foo'
}
}
}
How can I set up a dynamic string value such as
resValue "string", "package_name", applicationId
so that it includes the applicationIdSuffix for debug builds?
If I add this to defaultConfig, its my defaultConfig's applicationId that is set. If I add this to the flavor configuration, it is missing the applicationIdSuffix (this is null at this level).
Any hints?
The cool part about applicationIdSuffix is that you can use it in flavors as well as in build types. Check this out:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "dev.bmax.suffixtext"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix '.debug'
}
}
productFlavors {
prod {
applicationIdSuffix '.prod'
}
mock {
applicationIdSuffix '.mock'
}
}
}
Now, when I build my 'prodDebug' variant the final application ID is 'dev.bmax.suffixtext.prod.debug', which is what you want if I understand your question correctly.
EDIT
You can access the variant's name in a Gradle task like this:
applicationVariants.all { variant ->
// Use 'variant.name'
}
Gradle Android Plugin version 0.14.3 added support of the variant specific BuildConfigField/resValue:
applicationVariants.all { variant ->
variant.resValue "string", "name", "value"
}

Handheld multi flavor app with single wear project

My project was working fine when I had just a simple handheld app with one wear app.
Now, I've introduced three flavors to the handheld app and kept the same single flavor wear app.
The problem is that release builds are not being pushed to the wearable.
My project looks like this:
smartphone's build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 8
versionName "3.1.0"
applicationId "br.com.test"
}
signingConfigs {
...
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig = android.signingConfigs.release
}
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-debug"
zipAlignEnabled true
}
}
productFlavors {
generic {
applicationId "br.com.generic"
}
abc {
applicationId "br.com.teste.abc"
}
company {
applicationId "br.com.test.company"
}
}
}
dependencies {
genericWearApp project(path:':wear', configuration: 'genericRelease')
abcWearApp project(path:':wear', configuration: 'abcRelease')
companyWearApp project(path:':wear', configuration: 'companyRelease')
compile project(':common')
}
and wear's build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
publishNonDefault true
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 8
versionName "3.1.0"
applicationId "br.com.test"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-debug"
zipAlignEnabled true
}
}
productFlavors {
generic {
applicationId "br.com.generic"
}
abc {
applicationId "br.com.teste.abc"
}
company {
applicationId "br.com.test.company"
}
}
}
dependencies {
compile project(':common')
}
Why does the wearable does not gets the app? Any tips?
I was only a matter of tricky details... This repo offers a really nice example and was enough to help me solve this:
https://github.com/vngrs/PomoPomoAndroid/

Categories

Resources