There is custom build type mock and it initialised with debug:
buildTypes {
release {
}
debug {
}
mock {
initWith debug
}
}
There are also debug resources under /debug/res/values/file.xml.
Without copying /debug/res/values/file.xml into /mock/res/values/file.xml project doesn't compile as compiler can't find mock resources.
Is there way to let gradle know that debug resources should be reused for mock build type?
You can put file.xml in the main/res/values folder to share it across all build types
Related
I'm trying to define a dependency that will exist on my beta flavor builds, on both debug and release, but never on my production builds. I was following some examples that I saw online and the syntax made sense, but it seems to NOT be working for me. Here's what I have in a nutshell:
productFlavors {
beta {
}
prod {
}
}
dependencies {
betaDebugImplementation XYZ // this doesn't work.
}
The error I get is this one: > Could not find method betaDebugImplementation() .
Did I miss some step?
I know we can edit build types in Android Studio:
I know we can edit each build type setting in gradle:
android {
buildTypes {
release {
minifyEnabled true
}
}
I know we can detect build types in code. How do I detect if I am in release or debug mode?
But where actually are the build types defined? Let say I want to commit it to git. What should I do to keep build types of the project consistent?
Where actually are the build types defined?
Basically, BuildConfig is the auto-generated class that resides under path :
app/build/generated/source/buildConfig/yourBuildType/yourPackageName/BuildConfig.java.
This class holds variables provided by buildTypes {} block from app level build.gradle file. So, on every clean & rebuild of project, Gradle auto generates BuildConfig class that can be used in further Android development environment.
I.e. BuildConfig.DEBUG is the default variable that we can use in our application code to determine it's buildType.
We can provide our own fields through buildType from build.gradle file like following:
android {
. . .
buildTypes {
debug {
buildConfigField "String", "SOME_VARIABLE", '"This string value is from build config class"'
}
}
. . .
}
I have a gradle script which needs to be imported as a dependency like this:
compile project(':subproject', { ext.app = 'myApp'; ext.serverUrl = 'https://example.com'; ext.system = 'LIVE'})
This is working fine, if I set the variables directly in the dependency statement.
As I have a different system for debug and for release I tried to move these properties to the buildTypes:
...
debug {
debuggable true
serverUrl = 'https://example.com'
system = 'TEST'
}
prerelease {
debuggable true
serverUrl = 'https://example.com'
system = 'STAGING'
}
release {
serverUrl = 'https://example.com'
system = 'LIVE'
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
...
dependencies {
compile project(':subproject', { ext.app = appName; ext.serverUrl = serverUrl; ext.system = system })
}
So, when I build assembleDebug it should use TEST and with assemblePrerelease it should use STAGING. However it is always using the release build type variables to compile the dependency.
The library already contains publishNonDefault true
What's wrong with this gradle script?
I answer my own question.
Gradle does not parse the gradle file as expected. The closures will be evaluated in a single step when the tasks are being generated. This means that there is no concept of having a variable which will only be used when the specific task is being executed. The file is being read once which causes the variables to override the previous values of another flavor or buildType. This is also the reason why changing the order of the flavors results in different values.
The correct solution would be to define a custom task which is run right after the file has been generated. That task will generate a set of tasks for each variant of the app which themselves contain the configuration what to do.
This SO article helped me alot: How to get current buildType in Android Gradle configuration
Can I configure fabric.io / Crashlytics to not put any code/data into one of my build flavours?
It seems that even if I disable it like so :
productFlavors {
internal {
ext.enableCrashlytics = true
}
external {
ext.enableCrashlytics = false
}
}
... fabric still adds some data into the build. If I look at classes.dex, and search for the word "fabric", I find stuff that's not there if I remove fabric from the build altogether.
( FYI: I use a wrapper class in the build flavor source directories (which is called from the Application's onCreate) to not have any code dependency on fabric in the "external" build. ==> There's no reference from my code to anything fabric in the "external" flavor. )
I am using Android Studio and Gradle to build Android applications. I would like to have different strings within the Java code based on which type of build it is (debug vs. release). What is the best way to do this?
For example - I want to have different URLs if I am in debug or release. Also, I want to specify different GUIDs and other keys / strings.
The obvious hacky way to do this is to do a search and replace of a string in AndroidManifest.xml or worse yet, in a Java file. This approach seems error prone and hacky to me - is there a better way to do this?
There are many ways you can do this, although I usually do
android {
buildTypes {
release {
buildConfigField("String", "URL", "your_url_on_release")
}
debug {
buildConfigField("String", "URL", "your_url_on_debug")
}
}
}
You then can access them on your java code by using:
BuildConfig.URL
You can test this using Android Studio Build Variants, by changing your application variant to debug or release ( e.g. http://prntscr.com/8waxkw)
You have many solutions to do this, here's a simple case:
buildTypes {
debug { ... }
release { ... }
}
productFlavors {
staging { ... }
production { ... }
}
build types are for build management proguarding, debugging, signing, etc.
productFlavors are for all app internal configuration.
If you want to add resources related to the flavours you can create and add to src/(flavor_name)/res/values/ folder your urls.xml config file.
With this, in android studio, you'll directly see, all the builds variants in the corresponding window and the right urls.xml file associated to the current context and leave the gradle config clean.
Of course, this method works also for any resource you would need in your app.
For more detail, you can read this : http://developer.android.com/tools/building/configuring-gradle.html#workBuildVariants
I would do it with product flavors as explained in this post.