Programmatically get manifestPlaceholders - android

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.

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'
}

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/

Gradle BuildConfigFields Syntax

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 ?

Gradle Warning: missing groovy return statement

I'm having the following warning in my gradle build file
Not all execution paths return a value
This inspection reports on missing groovy return statement at the end of methods returning
and this is the code in that file
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "ac.company.srikar.quickhelpindia"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
}
Can anyone tell what is the issue here and how to get rid of this warning.
With Android Studio 2.2 I had to add a return void before the final bracket in the android section.
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.example.app"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
minifyEnabled false
shrinkResources false
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
standard {
applicationId "com.example.app.standard"
}
free {
applicationId "com.example.app.free"
}
}
// `return void` removes the lint error: `Not all execution paths return a value`.
return void
}
I have been getting this same warning and I think it is incorrect. I have read through the gradle documentation and it does not appear that a return type is needed. However, the warnings bug me and the only way I could get rid of it was to add return true.
buildTypes {
android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
return true
}
}
}
I doubt this is the "correct" solution; however, it does remove the warnings and it doesn't cause any issues.
I fixed this by adding the recommended suppression string from inspection:
//noinspection GroovyMissingReturnStatement
android {
compileSdkVersion 25
buildToolsVersion "23.0.3"
...
I got rid of this warning when I specified both, minifyEnabled and shrinkResources.
buildTypes {
debug {
minifyEnabled false
shrinkResources false
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
It seems that this is an issue fixed in Android Studio 2.3:
https://code.google.com/p/android/issues/detail?id=223575

Categories

Resources