I want to launch two different android apps from one neutral project but the installed app will be only neutral one, and in that there will be two different buttons to launch(open in that app only) resp. project.
you need use flavores in gradle.
apply plugin: 'com.android.application'
android {
defaultConfig {
//...
}
buildTypes {
//...
}
productFlavors {
app1 {
// config for app 1
}
app2 {
// config for app 2
}
}
}
dependencies {
//...
}
see this link for learn about flavors
Related
What is the best way to apply a gradle plugin to only a specific build variant. For example we might have
usStage
usDebug
usProduction
but a plugin has a lot of build overhead for developers (several minutes), and has no need to run on usStage and usProduction. Normally for libraries you would just use something like releaseImplementation, but I am not aware of anything like this existing at the gradle plugin level.
So right now we have somethig like this at the top of our app/build.gradle.kts in Android
plugins {
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
id("com.heapanalytics.android")
id("com.gladed.androidgitversion") version "0.4.14"
id("jacoco-reports")
}
where id("com.heapanalytics.android") is the culprit of slow build times on debug, where it is not even needed.
Is there a way that within our build variants, to only apply this for usProduction?
I have tried something like this:
plugins {
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
id("com.gladed.androidgitversion") version "0.4.14"
id("jacoco-reports")
}
android {
defaultConfig {
...
signingConfigs {
create(release) {
...
}
getByName(debug) {
...
}
}
buildTypes {
getByName(debug) {
...
}
getByName(release) {
...
apply(plugin = "com.heapanalytics.android")
...
}
But it just applies the plugin to every variant still, I believe because that block configures all variants even when you are only building one.
if (project.hasProperty(BuildProperties.Metrics.ENABLE_HEAP)) {
apply(plugin = "com.heapanalytics.android")
}
Tried something like this too, which may work, but was not really sure it is the best way. Feels hacky.
You can do this in many ways but for the most recent solution I know and use do:
android {
productFlavors {
flavourName{
apply plugin: 'com...'
}
}
}
For more solutions please reference to here.
I want to release my application in three stores. I have the rate section and update dialog in my application (which has a button to go to the store for update application). to manage these features I decide to make three flavors and three sources set to have different implementations of rate and update sections in different kotlin file.
this is my Gradle file:
android {
flavorDimensions("appStores")
productFlavors{
create("p1Flavor"){
dimension="appStores"
sourceSets {
create("p1SourceSet"){
java.srcDirs("src/main/java","src/firstStoreDir")
}
}
}
create("p2Flavor"){
dimension="appStores"
sourceSets {
create("p2SourceSet"){
java.srcDirs("src/main/java","src/secondStoreDir")
}
}
}
create("p3Flavor"){
dimension="appStores"
sourceSets {
create("p3SourceSet"){
java.srcDirs("src/main/java","src/thirdStoreDir")
}
}
}
}
}
I want to add a directory that contains two files (updateStore.kt and rate.kt) to src for every flavor. I mean I have three directories with three different names which every directory contains updateStore.kt and rate.kt with different implementations.
when I build my project I get this error:
The SourceSet 'p1SourceSet' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?
What should I do?
I realized that using source set is not appropriate for this situation and i decide to use buildConfigField.
android {
flavorDimensions("appStores")
productFlavors{
create("p1Flavor"){
dimension="appStores"
buildConfigField("String","APP_STORE_NAME","\"p1store\"")
}
create("p2Flavor"){
buildConfigField("String","APP_STORE_NAME","\"p2store\"")
dimension="appStores"
}
create("p3Flavor"){
buildConfigField("String","APP_STORE_NAME","\"p3store\"")
dimension="appStores"
}
}
}
and every where that i need to understand which flavor is selected, i use these codes :
when(BuildConfig.APP_STORE_NAME){
"p1store" -> {
//TODO
}
"p2store" -> {
//TODO
}
"p3store" ->{
//TODO
}
}
I got a project configured with multiple variants and flavors:
buildTypes {
debug {
}
release {
}
}
flavorDimensions "default"
productFlavors {
mock {
}
alpha {
}
beta {
}
prod {
}
}
Whenever I open the project from another one (so starting Android Studio), it selects the mockDebug variant by default. Often I end up build this one first, then realizing I'm on the wrong variant.
Is there a way to tell Android Studio to defaults to one variant, let's say betaDebug?
Technicals: Android Studio 3.1.4, Gradle wrapper 4.4, Android Gradle 3.1.4.
With Android Studio 3.5+ you can set default falvors:
android {
flavorDimensions "stage", "target"
productFlavors {
develop {
getIsDefault().set(true) // that does the magic
dimension "stage"
...
When using KTS it lookes like this:
android {
flavorDimensions("stage", "target")
productFlavors {
create("develop") {
isDefault = true
dimension("stage")
...
Change the order in which you define them in productFlavors. The IDE always loads the first flavor it finds there as the default.
What actually worked for me is enabling "Android Studio Preferences -> Experimental -> Only sync the active variant". It keeps the selected build variant when reopening AS or when re-syncing, basically solving the original problem.
AS/AGP v4.1.
android {
buildTypes {
...
}
productFlavors {
demo {}
full{}
}
}
sources:
src/demo/java
src/full/java
Inside android studio, with android view, only the sources of one product flavor of currently selected build variant can be seen. Is there a way to see all source codes of all product flavors with android view?
I have put the product Flavors into another file called other.gradle and it looks like this:
project.ext.myflavors = {
mock {
applicationId "com.mysite.mock"
}
}
and i am able to successfully access the closure in my build.gradle file like this:
myflavors()
but i get an error that mock method is not defined.
Error:Gradle DSL method not found: 'mock()'
Is there no way to just define code from another file and import the code itself into the build file ? Or how can i import the flavors from another file ?
The build flavors could be defined in a separate file (build_flavors.gradle) like this:
android {
productFlavors {
flavorA {
// ...
}
flavorB {
// ...
}
}
}
and then imported into build.gradle:
apply plugin: 'com.android.application'
apply from: './build_flavors.gradle'
android {
// the rest of your android configuration
}
It is perfectly fine to access productFlavors multiple times. So adding some or all flavors in a script included by your build script will work.
Create a Gradle script that contains the logic that decides which flavor(s) should be added:
if (someCondition()) {
android {
productFlavors {
one {
applicationId = 'com.example.oneapp'
}
}
}
} else {
android {
productFlavors {
two {
applicationId = 'com.example.twoapp'
}
}
}
}
Now include this script from your build script. Reusing the scripts is easy if you put it under (a subfolder of) the root project. For example:
apply from: rootProject.file('build-scripts/flavor-picker.gradle')
Note that your IDE may not notice changes to the flavor selection script or gradle.properties, so if you make changes to them you will probably have to manually reimport the Gradle files to see the correct set of available tasks.