Why are my Android gradle variant customisations not working? - android

1- Why is the following:
android.buildVariants.each { variant ->
... my code
}
giving me the following error?
Could not find property 'buildVariants' on com.android.build.gradle.AppExtension_Decorated#1bf6bde6.
2- Why is the following silently not executing "... my code"?
android.applicationVariants.each { variant ->
... my code
}

The Android Build System now uses "applicationVariants" instead of "buildVariants". However, "android.applicationVariants.each" will also not work, see below.
Since the Android Build System 0.5.5 release you must use "android.applicationVariants.all" instead of "android.applicationVariants.each", as applicationVariants will remain empty with a call to each.

Related

After upgrading to Android studio 3 (stable version) I get this error

Gradle sync failed:
Manifest Tasks does not support the manifestOutputFile property any more, please use the manifestOutputDirectory instead.
For more information, please check
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html
I opened the link and applied it but still can not solve my problem ..
it was said to add this code in build.gradle module app ..
applicationVariants.all { variant ->
variant.outputs.all { output ->
output.processManifest.doLast {
// Stores the path to the maifest.
String manifestPath = "$manifestOutputDirectory/AndroidManifest.xml"
// Stores the contents of the manifest.
def manifestContent = file(manifestPath).getText()
// Changes the version code in the stored text.
manifestContent = manifestContent.replace('android:versionCode="1"',
String.format('android:versionCode="%s"', generatedCode))
// Overwrites the manifest with the new text.
file(manifestPath).write(manifestContent)
}
}
}
I may apply it in wrong way because no resources that cover this topic.
I also try this answer .. Error:Manifest Tasks does not support the manifestOutputFile property any more, please use the manifestOutputDirectory instead. (Android Studio 3.0)
for all my trials the error still exist ..

Grade Plugin 3-alpha1 outputFile causes error

I'm trying to update a project to Android Studio 3.
The following snippet is no longer accepted in a build.gradle file.
applicationVariants.all { variant ->
variant.outputs.each { out ->
def oFile =out.outputFile // This line causes failure
//...
}
}
The error is a simple "Not Valid" yet the intellisense suggests it is as it autocompletes fine.
Checking the idea.log shows the following exception:
Caused by: java.lang.RuntimeException: Not valid.
at com.android.ide.common.build.ApkData.getMainOutputFile(ApkData.java:136)
at com.android.build.gradle.internal.api.BaseVariantOutputImpl.getOutputFile(BaseVariantOutputImpl.java:60)
at com.android.build.gradle.internal.api.ApkVariantOutputImpl_Decorated.getOutputFile(Unknown Source)
at org.gradle.internal.metaobject.BeanDynamicObject$MetaClassAdapter.getProperty(BeanDynamicObject.java:228)
at org.gradle.internal.metaobject.BeanDynamicObject.tryGetProperty(BeanDynamicObject.java:171)
at org.gradle.internal.metaobject.CompositeDynamicObject.tryGetProperty(CompositeDynamicObject.java:55)
at org.gradle.internal.metaobject.AbstractDynamicObject.getProperty(AbstractDynamicObject.java:59)
at com.android.build.gradle.internal.api.ApkVariantOutputImpl_Decorated.getProperty(Unknown Source)
I can find no documentation on Gradle 4. Would this be a bug or a function that is deprecated perhaps?
Also filed at: https://issuetracker.google.com/issues/38408231
Update: Fix for APK renaming:
Use all iterators instead of each:
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${variant.name}-${variant.versionName}.apk"
}
}
Previous answer, still valid: It's a known problem with the new plugin:
This build error occurs because variant-specific tasks are no longer created during the configuration stage. This results in the plugin not knowing all of its outputs up front, but it also means faster configuration times. As an alternative, we will introduce new APIs to provide similar functionality.
We need to wait for an alternative way of doing that, according to the Migration Guide.
If your failing plugin support explicitly setting file path, it might be a work around.
I had problems with Fabrics crashlyticsUploadDistributionRelease task, giving me the same stack trace as above. I fixed it by explicitly setting the output file path property in app/build.gradle:
ext.betaDistributionApkFilePath = "app/build/outputs/apk/release/app-release.apk"

Gradle Skip Test

I am looking for a way to skip tests from one of the projects in a multi-build project. I don't want to use gradle build -x test because then it will skip test for all sub - projects.
Root
Sub P1
build.gradle
Sub P2
build.gradle
Sub P3
build.gradle
build.gradle
settings.gradle
I want to skip tests only for "Sub P3"
Can i configure my project(Sub P3) build file to skip tests?
Due to official user guide, there are 3 ways to skip some task in gradle.
The first 2, are: using predicate and exception throwing. Predicates are resolved during the configuration phase and may not pass to your requirements. StopExecutionExeptionthrowing could be added to the doFirst of every test and be throwed according to some condition. Bit that seems to be not very clear, because you have to modify both root script to set the condition and subroject sripts test tasks.
And the 3rd one is - disabling the tasks. Every task, has a enabled property (default is true), which is preventing task execution, if it was set to false. Only thing you have to do is to set this property for test task in your subproject. This can be done in sub projects buil script root, as:
test.enabled = false
This will work, if you didn't specify custom test tasks, if you did, you can disable all test by task type, as:
project(':subProject').tasks.withType(Test){
enabled = false
}
Previews 2 configurations must be included to the build script of the subproject, but since you have a root project, you can configure subprojecst from it's build script, via project() providing subproject's name:
project(':subProject').tasks.withType(Test){
enabled = false
}
For android there is no test task available in gradle
To skip unit tests in android you have to do this
android {
.
.
.
testOptions {
unitTests.all {
enabled false
}
}
}

collectMultidex task looking in wrong place for manifest.xml

When trying to enable multi-dexing for an application over 65K, following the guidelines specified by the android developer page here
While in my application build.gradle I specify:
android.applicationVariants.all{
variant -> variant.outputs.each {
output ->
output.processResources.manifestFile = file('AndroidManifest.xml')
output.processManifest.enabled=false
}
}
Upon running build I receive this error,
Error: A problem was found with the configuration of task ':myapp:collectDebugMultiDexComponents'.
File 'C:\Android Projects\myappapp\build\intermediates\manifests\full\debug\AndroidManifest.xml' specified for property 'manifest' does not exist.
Open to any suggestions or advice to properly direct multi dex task to actual AndroidManifest.xml path. When removing the library that causes the application to cross the 64k limit and removing multidexing the application builds fine.

Gradle android: Where should I put my custom task?

I want to add a custom task to my Gradle build file.
Should this go inside the android configuration closure or not?
I tried both and they seem to work fine and I couldn't find any explicit mention in the docs.
the task definition should usually not go into the android blog and on root level as zeventh suggests. though it sometimes useful to do this in the android block. especially when you create these tasks dynamically.
In the following example I create one task for each build variant:
android{
applicationVariants.all{ variant ->
def variantTask = task("${variant.name}CustomTask", type:CustomTask){
...
}
check.dependsOn variantTask
}
}
You should just put the task at the root level of the file, outside the android closure.
task yourTask << {
println "Hello world"
}

Categories

Resources