Deploying multiple build variants at a time - Android studio gradle - android-studio

I recently discovered this awesome feature about gradle productFlavors. I currently have 3 variants (staging, sandbox and production) and I can deploy one of the variants at a time using build variant panel.
Is there a way I can deploy all variants at a time?

Yes,
In Android Studio, open the "Gradle Tasks" tab, which is usually on the right. You will see many tasks that start with 'assemble', double click on one of those.
For example, double clicking on 'assembleRelease' will create all your release apks.
From the docs:
Building and Tasks
We previously saw that each Build Type creates its own assemble
task, but that Build Variants are a combination of Build Type and
Product Flavor.
When Product Flavors are used, more assemble-type tasks are created.
These are:
1) assemble[Variant Name]
2) assemble[Build Type Name]
3) assemble[Product Flavor Name]
1) allows directly building a single variant. For instance
assembleFlavor1Debug.
2) allows building all APKs for a given Build Type. For instance
assembleDebug will build both Flavor1Debug and Flavor2Debug variants.
3) allows building all APKs for a given flavor. For instance
assembleFlavor1 will build both Flavor1Debug and Flavor1Release
variants.
The task assemble will build all possible variants.

If you know the names of the gradle tasks that install your variants you can run this from the root of your project in the terminal:
./gradlew install{VariantName1, VariantName2, VariantName3}Debug
This assumes you have a module build.gradle file with variants set up according to the guide. So something along these lines:
apply plugin: 'com.android.application'
android {
...
flavorDimensions "myFlavorDimension"
productFlavors {
VariantName1 {
...
}
VariantName2 {
...
}
VariantName3 {
...
}
}
...
}
dependencies {
...
}
You can find these gradle task names either in Android Studio in the Gradle Tab (right side of GUI) under you moduleName->Tasks->install
Or you can find them in the terminal with:
./gradlew tasks | grep install
I'm sure there is some Regex that could grab only the ones of interest programmatically as well, but I'm not a regex buff. If you want to leave a comment with something that would work, I'd be happy to edit and add later.

Related

What is the use of com.diffplug.spotless plugin in Android Studio?

I am working with Jetpack Compose and I came across "com.diffplug.spotless" plugin in the app's build.gradle file of many examples, but I am not sure if I need it in my project. Can anyone explain the purpose of using it in the Jetpack compose projects?
''' apply plugin: "com.diffplug.spotless" '''
Spotless: Keep your code spotless with Gradle
While working, Many a times you will get formatting issues at the stage of every commit like removing empty lines, cutting white spaces correct, indentation and other minor formatting mistakes.
Using tool/plugin called “Spotless” will reduce time in addressing code review comments.
Spotless provides support for a variety of languages.
Spotless consists of a list of format and each format has:-
1.a target (the files to format), which you set with target.
2.a list of FormatterStep, which are just String -> String functions, such as replace, replaceRegex, trimTrailingWhitespace, custom, prettier, eclipseWtp, licenseHeader etc.
To start integration with Gradle:-
1.Add the following dependency to your build.gradle file
classpath(“com.diffplug.spotless:spotless-plugin-gradle:$spotlessVersion”)
2.Apply the following plugin
apply plugin: ‘com.diffplug.gradle.spotless’
3.Applying spotless to your gradle file in Android Java source
spotless {
java {
// ...
target '**/*.java'
// ...
}
}
Note:- Be sure to add target '**/*.java' otherwise spotless will not detect Java code
inside Android modules.
For more detail you can refer this link : 1

Updated android studio and got fail with Gstreamer build

Updated to Android Studio 3.0.0 with new android gradle plugin.
While buildin project got message:
What went wrong:
Execution failed for task `':app:externalNativeBuildDebug'`.
Expected output file at `gst-build-arm64-v8a/libgstreamer_android.so` for target `gstreamer_android` but there was none
but libgstreamer_android.so library file is already there. For native code I use ndk-build. Does anyone have this issue?
Add to build.gradle file of our android module field targets.
android {
defaultConfig {
externalNativeBuild {
ndkBuild {
targets "name_of_native_module_in_android_mk_file"
}
...
}
Don't add gstreamer_android.
UPDATE: Valery's answer works!
Obsolete answer:
That's not the perfect fix, it's just temporary until I have time to take a deep look into the problem. Downgrade your gradle plugin:
File -> Project Structure
Click at "Project"
At "Gradle version" field put:
3.3
At "Android Plugin Version" field put:
2.3.3
Hit "OK"
Accept the messages, sync the project, etc... Android Studio may prompt a windown asking for update gradle plugin again, just don't accept it for now...
I guess the update on gradle changed the way the builds are made, maybe something on Android.mk will have to change or some other parameter on build.grade...
edit: I found some clue at: https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html
API changes Android plugin 3.0.0 introduces API changes that removes
certain functionalities and may break your existing builds. Later
versions of the plugin may introduce new public APIs that replace
broken functionalities.
Modifying variant outputs at build time may not work Using the Variant
API to manipulate variant outputs is broken with the new plugin. It
still works for simple tasks, such as changing the APK name during
build time, as shown below:...
So, I guess we should keep using the temporary fix (not updated version of gradle)...

Intellij + Gradle: Import classes automatically

I am writing a gradle build file. I want to use the dependency Apache Commons Net v3.3.
Now, whenever I type a statement that uses a class from this dependency, IntelliJ does not recognize that I am missing a import. I was aldo not offered any classes from the previously added dependency. I did indeed click the refresh button on the gradle tab in IntelliJ IDEA, before I added any dangling content to my buildscript.
When I run gradlew now (and it compiles the buildscript), I get a error about a unrecognized Symbol.
Can I do something to make IntelliJ report missing imports to me?
I can add the import by looking up the javadoc online any manually copy-pasting the class name into my buildscript, but I find that rather annoying and I think that such primitive tasks should be done by a IDE.
This is pretty much the exact example:
apply plugin: 'java'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'commons-net:commons-net:3.3'
}
}
task example << {
FTPClient ftp = new FTPClient() //IntelliJ does not complain about missing imports, neither did it have the correct class available in auto-completion
}
Install the gradle plugin (which it seems like you already have).
Go to the gradle tab on the right side of the IDE and click the wrench icon to get to gradle settings.
Check the box that says "Use auto-import"

how to dump all gradle values used for build

we have a multi-project gradle build in android studio. every now and then we have to change something in it, and usually its only 1 or two lines of code, but its never easy knowing where to put those. I find it quite hard to know which properties exist where so I would love to have something like dump-everything where I could see all properties and their children at the point in time, this would make changes much easier
I have found this
def void explainMe(it){
//println "Examining $it.name:"
println "Meta:"
println it.metaClass.metaMethods*.name.sort().unique()
println "Methods:"
println it.metaClass.methods*.name.sort().unique()
println "Depends On:"
//println it.dependsOn.collect({it*.getName()})
println "Properties:"
println it.properties.entrySet()*.toString()
.sort().toString().replaceAll(", ","\n")
}
which is OK, but I would like to call it on top level scope and for all its children recursively, and in best case store output to file to be able to search through it. any idea would be appreciated? alternatively would it be possible to attach debugger to gradle build and inspect /watch variables inside?
thanks
Gradle has very specific support for inspecting certain parts of the build model (gradle tasks, gradle help --task taskName, gradle properties, gradle projects, gradle dependencies, gradle dependencyInsight, etc.), but doesn't currently have a generic feature for deep inspection of arbitrary build model properties and their values. Instead one would typically add some printlns to the build script and/or consult the Gradle Build Language Reference.
To answer your second question, a Gradle build can be debugged in the same way as any other external application. The necessary JVM args (typically provided by the debugger) can be set via the JAVA_OPTS or GRADLE_OPTS environment variable. It's probably best to execute Gradle with --no-daemon when debugging.

Using archivesBaseName in a gradle project has no effect

I'm trying to name the artifact that gets built by gradle. Look at this build.gradle:
archivesBaseName='this_is_ignored'
apply plugin: 'groovy'
archivesBaseName='this_is_also_ignored'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.+'
}
I also tried in gradle.properties:
archivesBaseName=`this_is_ignored_too`
In every case ./gradlew build generates a .jar file based on the folder where the project resides (which as I understand it is project.name, I was just hoping to override that with archivesBaseName).
In other words, I want:
~/gradle-helloworld > ./gradlew build
to generate this_is_ignored.jar, but it's generating gradle-helloworld.jar instead.
Any ideas?
(Turning Peter's comment into a CW answer.)
If you set archivesBaseName before applying the plugin, you'll introduce a dynamic property (which gives a deprecation warning). This dynamic property will then shadow the one introduced by the plugin, which is why the second assignment doesn't have the desired effect either. The solution is to only set the property after applying the plugin.

Resources