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

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

Related

Cannot get property '?' on null object error when flutter build apk

In my case I run flutter build apk -t lib/main_dev.dart --release --flavor=dev command to build a release APK, I have setup build flavors in my project. But after I run that command it gives "A problem occurred evaluating project ':app'.> Cannot get property '?' on null object" error. Here is my app level build.gradle file,
android {
compileSdkVersion 31
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
lintOptions {
checkReleaseBuilds false │
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.getin.app***"
minSdkVersion 21
targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
flavorDimensions "app"
productFlavors {
dev {
dimension "app"
versionNameSuffix "-dev"
resValue "string", "app_name", "GetIn Dev"
resValue "string", "facebook_app_id", "5982**********"
resValue "string", "facebook_client_token", "d8f78c******************"
resValue "string", "fb_login_protocol_scheme", "fb*****************"
}
prod {
dimension "app"
applicationId "com.getin.app"
resValue "string", "app_name", "GetIn"
resValue "string", "facebook_app_id", "5982**********"
resValue "string", "facebook_client_token", "d8f78c******************"
resValue "string", "fb_login_protocol_scheme", "fb*****************"
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.firebase:firebase-crashlytics'
}
Here is the given error,
As the image, it shows error is on line number 42, in line number 42 it has this,

How to build several apks at once with custom gradle task?

I'm struggling to find a way for build several apk at once with gradle.
I'd like to have a custom gradle task which considers only variants with enviroment = "production" and all the brands but nonPublishedBrand and buildtype = "release" (see code below).
For each of those variants i need to:
generate the signed apk
upload prodguard mappings to bugsnag with the relative task uploadBugsnag${variant.name}-releaseMapping
rename apk into <brand>-<version>.apk and move it to a common folder $buildDir/myApks
I only found a way to make assemble tasks also run my custom tasks but that's not ideal because i don't want to upload mappings every time a production release variant is built, but only when it's meant i.e. launching a specific gradle task.
Is it possible to achieve that with gradle? Can you please point me in the right direction?
See my build.gradle android section for reference:
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 15
targetSdkVersion 27
versionCode 1000000
versionName "1.0.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile file("keystore/keystore")
storePassword '*******'
keyAlias '*******'
keyPassword '*******'
}
}
buildTypes {
debug {
applicationIdSuffix ".debug"
versionNameSuffix ".debug"
manifestPlaceholders = [buildTypePrefix: "D_"]
}
release {
debuggable false
signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
manifestPlaceholders = [buildTypePrefix: ""]
}
}
flavorDimensions "environment", "brand"
productFlavors {
//ENVIRONMENTS
staging {
dimension "environment"
applicationIdSuffix ".staging"
versionNameSuffix ".staging"
buildConfigField("String", "BASE_URL", "\"http://baseurl-staging.com\"")
manifestPlaceholders = [environmentPrefix: "S_"]
}
production {
dimension "environment"
buildConfigField("String", "BASE_URL"l, "\"http://baseurl-prod.com\"")
manifestPlaceholders = [environmentPrefix: ""]
}
//BRANDS
nonPublishedBrand {
dimension "brand"
applicationId "${packageBaseName}.nonpublishedbrand"
manifestPlaceholders = [appName: "Non published brand"]
ext {
facebook_app_id = [
staging: "0000000",
prod : "11111111"
]
}
}
brand1 {
dimension "brand"
applicationId "${packageBaseName}.brand1"
manifestPlaceholders = [appName: "Brand 1"]
ext {
facebook_app_id = [
staging: "22222222",
prod : "33333333"
]
}
}
brand2 {
dimension "brand"
applicationId "${packageBaseName}.brand2"
manifestPlaceholders = [appName: "Brand 2"]
ext {
facebook_app_id = [
staging: "44444444",
prod : "555555555"
]
}
}
}
productFlavors {
applicationVariants.all { variant ->
def isDebug = false
if (variant.buildType.name == "debug") {
isDebug = true
}
def isStaging = false
def flavors = variant.productFlavors
def environment = flavors[0]
if (environment.name == "staging") {
isStaging = true
}
def facebookAppId = ""
if (isStaging){
facebookAppId = flavors[1].facebook_app_id.staging
}else{
facebookAppId = flavors[1].facebook_app_id.prod
}
variant.buildConfigField "String", "FACEBOOK_APP_ID", "\"${facebookAppId}\""
}
}
dataBinding {
enabled = true
}
bugsnag {
autoUpload false
}
}
circle.ci is an efficient way to generate builds with each commit. Documentation : https://circleci.com/docs/2.0/
Your circle.yml file would have something like this :
override:
- ./gradlew clean :mobile:assemblePre -PdisablePreDex -Pandroid.threadPoolSize=1 -Dorg.gradle.parallel=false -Dorg.gradle.jvmargs="-Xms2048m -Xmx4608m"
- cp -r ~ build/outputs/apk/build/pre/*.apk $CIRCLE_ARTIFACTS
- ./gradlew clean :mobile:assembleRelease -PdisablePreDex -Pandroid.threadPoolSize=1 -Dorg.gradle.parallel=false -Dorg.gradle.jvmargs="-Xms2048m -Xmx4608m"
- cp -r ~build/outputs/apk/build/release/*.apk $CIRCLE_ARTIFACTS
assemblePre and assembleRelease are tasks executed in above case. You can try to write custom tasks here.

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

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/

How do I configure a variant to run both prod & debug app on same Android?

I added the following to my build.gradle:
def applicationId() { "com.rithmio.coach" }
def applicationName() { "Coach Rithmio" }
defaultConfig {
applicationId applicationId()
minSdkVersion 19
targetSdkVersion 22
versionCode gitVersion()
versionName "${versionMajor()}.${versionMinor()}.${versionPatch()}"
}
buildTypes {
debug {
applicationIdSuffix ".debug"
resValue "string", "app_name", "Coach (Debug)"
final CONTENT_AUTHORITY = "${applicationId()}${applicationIdSuffix}.content"
resValue "string", "content_authority", CONTENT_AUTHORITY
final ACCOUNT_TYPE = "${applicationId()}${applicationIdSuffix}.account"
resValue "string", "account_type", ACCOUNT_TYPE
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
resValue "string", "app_name", "${applicationName()}"
final CONTENT_AUTHORITY = "${applicationId()}.content"
resValue "string", "content_authority", CONTENT_AUTHORITY
// If you're plugging into the account auth system
final ACCOUNT_TYPE = "${applicationId()}.account"
resValue "string", "account_type", ACCOUNT_TYPE
}
}
But this breaks my application in debug mode, all of the sudden the root classes are not where they are supposed to be.
It's looking for a class com.rithmio.coach.debug.mobile.* but it should be looking for com.rithmio.coach.mobile.*
How would i save modify this to look in the correct package?
Caused by: java.lang.ClassNotFoundException:
find class "com.rithmio.coach.debug.mobile.ScrollAwareFabBehavior"
on path: DexPathList[[zip file "/data/app/com.rithmio.coach.debug-1/base.apk"],
nativeLibraryDirectories=[/data/app/com.rithmio.coach.debug-1/lib/arm,
/vendor/lib, /system/lib]]
I basically want to change the applicationid without changing the package name.
How define product flavors in the build file :-
To define two product flavors, edit the build file for the app module to add the following configuration:-
...
android {
...
defaultConfig { ... }
signingConfigs { ... }
buildTypes { ... }
productFlavors {
demo {
applicationId "com.buildsystemexample.app.demo"
versionName "1.0-demo"
}
full {
applicationId "com.buildsystemexample.app.full"
versionName "1.0-full"
}
}
}
...
The product flavor definitions support the same properties as the defaultConfig element. The base configuration for all flavors is specified in defaultConfig, and each flavor overrides any default values. The build file above uses the applicationId property to assign a different package name to each flavor: since each flavor definition creates a different app, they each need a distinct package name.
More detailed answer here :-
https://developer.android.com/tools/building/configuring-gradle.html

Categories

Resources