Gradle extend assembleRelease - android

on gradle 2.1.3 I could do:
assembleRelease
{
doFirst()
{
//some code
}
}
But when I updated to gradle 2.2.0 I get an error:
Error:(12, 1) A problem occurred evaluating project ':MyProj'.
> Could not find method assembleRelease() for arguments [build_6dlppzyvvovwra7h55acb4kp$_run_closure1#543a3981] on project ':MyProj' of type org.gradle.api.Project.
Can you please help me with that?

It seems to be a common issue for version update to 2.2.0. You can find some similar questions on SO, for example here. But they all lead to a common workaround - rewrite your task this way:
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease') {
task.doFirst {
//some code
}
}
}
Not sure, but it seems, that assembleRelease is not available at the moment you try to point it in your script since 2.2.0 version.

Related

How to cache dependency of a detached gradle configuration on CI?

I'm trying to generate a full cache of dependencies for all project/scripts configurations before assemble step. I'd like to use --offline gradle flag for all jobs except the caching one.
So android plugin provides androidDependencies task which resolves all resolvable dependencies of a project
Previously we used a custom task which does the same:
task resolveDependencies {
description "Resolves all projects dependencies from the repository."
group "Build Server"
doLast {
rootProject.allprojects { project ->
project.buildscript.configurations.forEach { configuration ->
if (configuration.canBeResolved) {
try {
configuration.resolve()
} catch (ignored) {
}
}
}
project.configurations.forEach { configuration ->
if (configuration.canBeResolved) {
try {
configuration.resolve()
} catch (ignored) {
}
}
}
}
}
}
Unluckily some gradle scripts which are activated during assemble step use detachedConfigurations. These configurations are not visible and solved neither with androidDependencies task nor with the custom one
For example, android gradle build plugin throws the error during assemble step:
Execution failed for task ':app:mergeDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.Aapt2CompileRunnable
> Could not isolate value com.android.build.gradle.internal.res.Aapt2CompileRunnable$Params_Decorated#3d617643 of type Aapt2CompileRunnable.Params
> Could not resolve all files for configuration ':app:detachedConfiguration1'.
> Could not resolve com.android.tools.build:aapt2:7.1.3-7984345.
Required by:
project :app
> No cached version of com.android.tools.build:aapt2:7.1.3-7984345 available for offline mode.
Is it possible to solve all detached configurations before the assemble task?
For now the only solution i see is to cache dependencies as well after the assemble task and forget about using --offline
Please also check related topic on circleCi forum

Could not get unknown property 'manifestOutputDirectory'

I'm trying to "make project" with Android Studio, and I'm getting this error:
Execution failed for task ':myApp:processGoogleDebugManifest'.
Could not get unknown property 'manifestOutputDirectory' for task ':myApp:processGoogleDebugManifest' of type com.android.build.gradle.tasks.ProcessMultiApkApplicationManifest.
Any help please?
EDIT: This error occurred after I updated to gradle v6.5 and plugin v4.1.0. If I revert to gradle v6.1.1 and plugin v4.0.0 the error disappears.
I encountered this same issue today, in my case it was caused by an outdated version of Huawei's AG Connect plugin. I was using com.huawei.agconnect:agcp:1.2.1.301, but when I updated it to com.huawei.agconnect:agcp:1.4.1.300 the issue was fixed.
See Latest Huawei's AG Connect plugin here: https://developer.huawei.com/latest/plugin/agconnect ...Just scroll, you'll find it there haha!
But if Huawei's plugin is not the problem you are having, you can debug the issue by running gradle with --stacktrace option to see where the issue originates from. In Android Studio you can add command line options for gradle in Settings/Build, Execution, Deployment/Compiler/Command-line options.
This solved my same problem:
In the project level build.gradle, replace this:
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
with this:
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
Reference: https://github.com/Tencent/tinker/issues/1471#issuecomment-710777366
If you're using bugsnag, replace the following line
classpath 'com.bugsnag:bugsnag-android-gradle-plugin:4.+'
with:
classpath 'com.bugsnag:bugsnag-android-gradle-plugin:5.+'
For further detail, see this issue: Fails with AGP 4.1.0-alpha04 and this comment.
I write it here because this solution saved my day :
We can fix this by simply replacing references to
manifestOutputDirectory
by
multiApkManifestOutputDirectory
enter code here
in your gradle tasks
For example :
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
output.processManifest.doLast { task ->
def outputDir = multiApkManifestOutputDirectory.asFile.get()
String manifestMergerpath = "$outputDir/AndroidManifest.xml"
writeManifest(manifestMergerpath, placeholders)
}
}
}
android.applicationVariants.all {
outputs.all {
processManifestProvider.configure {
val multiApkManifestOutputDirectory = (this as ProcessMultiApkApplicationManifest)
.multiApkManifestOutputDirectory // 👈
doLast {
multiApkManifestOutputDirectory.get()
.asFile
.walkTopDown()
.forEach {
}
}
}
}
}

Android Build failure : Could not get unknown property 'assembleDebug' for project

I need to create Jar and copy to lib folder, which is done in following task :
task copyJarToLib(type: Copy, dependsOn: 'createJar') {
from "build/libs/lib1.jar"
from "build/libs/lib2.jar"
into "../App/libs/"
}
I have to execute this after apk generation. So, I am calling following instruction at the end of the module-app build.gradle :
assembleDebug.finalizedBy(copyJarToLib)
Issue is observed after upgrading the gradle plugin to 3.1.0 and gradle to 4.4.
Same implementation is working fine with gradle 2.3.
If you want to execute something at the end of build, you can do it as follows:
gradle.buildFinished {
copy {
from "build/libs/lib1.jar"
from "build/libs/lib2.jar"
into "../App/libs/"
}
}
If you want to execute task before apk is built the you can:
afterEvaluate {
project.tasks.findByName('preDebugBuild').dependsOn(':<module>:copyJarToLib')
}
base on Android Studio 2020.3.1, you can use follow codes
afterEvaluate {
project.tasks.findByName('preDebugBuild').dependsOn(copyJarToLib)
}

Why can't I use gradle task connectedDebugAndroidTest in my build script?

I can refer to connectedCheck task (which came from android plugin) from my build script:
connectedCheck.finalizedBy AndroidShowTestResults
But trying to use connectedDebugAndroidTest (which came from android plugin too)
connectedDebugAndroidTest.finalizedBy AndroidShowTestResults
gives me
Error:(48, 0) Could not find property 'connectedDebugAndroidTest' on project ':app'.
And if I try
task connectedDebugAndroidTest << {print '123'}
it curses me with
Error:Cannot add task ':app:connectedDebugAndroidTest' as a task with that name already exists.
I don't undestand why I cannot refer to connectedDebugAndroidTest?
Available gradle tasks are shown below:
The android plugin defers the addition of several tasks especially those that have buildType or flavor names in them till a very late stage of the configuration phase. Which in turn means if you try to refer to these yet-to-be-added-tasks by name, you're likely to see a "does not exist" type error messages. If you want to add dependencies around deferred-created tasks, you should wait until configuration is complete:
gradle.projectsEvaluated {
connectedDebugAndroidTest.finalizedBy AndroidShowTestResults
}
Alternatively, you can add a listener to task-graph events, so you can do stuff as soon as a certain task is added to task-graph:
tasks.whenTaskAdded { task ->
if (task.name == 'connectedDebugAndroidTest') {
task.finalizedBy AndroidShowTestResults
}
}
Try
task connectedTest(dependsOn: ["connectedDebugAndroidTest"]){
}
connectedTest.finalizedBy "AndroidShowTestResults"
I think you should try to open test and rebuild.

Gradle: How to run custom task after an Android Library is built?

I have an Android Library, it's generating a debug.aar and a release.aar, I need to copy the release.aar to another folder as a reference to other part of the project.
What I've done now is in this Android Library build.gradle I defined a task:
task copyAARToCommonLibs(type: Copy) {
from('../build/outputs/aar') {
include '*-release.arr'
}
into '../SomeSampleApps/libs'
}
I'm trying to run this task after the arr is generated, which I assume is assembleRelease stage, so I tried do this in this build.gradle
assembleRelease.doLast{
copyAARToCommonLibs
}
I build the overall project using
gradle build
But this task is running at the very beginning of the whole process.
I also tried this:
applicationVariants.all { variant ->
variant.assemble.doLast {
copyAARToCommonLibs
}
}
inside android{} property(I guess that's what it's called?)
Running gradle build, got this error: Could not find property 'applicationVariants'
I then came across this snippet:
tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn copyAARToCommonLibs }
But it seems this makes the task to run after compiling, I don't know exactly how to modify this to run after assemble.
Could someone please correct me where I did wrong and how can I get this copy task work after the .arr file is generated?
It seems that finalizedBy might be helpful.
assembleRelease.finalizedBy(copyAARToCommonLibs)
Mind the fact that in the following way you won't define a dependency:
assembleRelease.doLast {
copyAARToCommonLibs
}
actually.. it does exactly nothing. You need to execute the task:
assembleRelease.doLast {
copyAARToCommonLibs.execute()
}
but running task in the following way is discouraged and very bad practice.
You can also try:
assembleRelease.doLast {
copy {
from('../build/outputs/aar') {
include '*-release.aar'
}
into '../AscendonSDKSamples/libs'
}
}
I went with finalizedBy() but had to include it within an afterEvaluate...
afterEvaluate {
if (gradle.startParameter.taskNames.contains(":app:assembleFatReleaseInternal")) {
play.enabled = true
play.commit = true
play.track = "internal"
play.releaseStatus = "completed"
play.releaseName = versionName
generateFatReleaseInternalBuildConfig.dependsOn set_build_date
assembleFatReleaseInternal.finalizedBy(uploadCrashlyticsSymbolFileFatReleaseInternal)
uploadCrashlyticsSymbolFileFatReleaseInternal.finalizedBy(publishFatReleaseInternal)
}
}
This worked well for automating the upload of native symbols to Fabric / Crashlytics and other things such as automated play store publishing.
Because android studio add task by dynamic,so assembleRelease will not be recognized.
Just add hook after task added event happens.
tasks.whenTaskAdded {
theTask ->
if (theTask.name.contains('externalNativeBuild')) {
theTask.doLast{
println "[*] begin to copy file."
}
}
// println theTask.name
}

Categories

Resources