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)
}
Related
Using Gradle 7.0.2 with the new Android Gradle Plugin I try to start a copy task which copies a file in the build folder.
The script looks something like this
androidComponents {
onVariants(selector().withBuildType("debug"), {
def copyTestTask = tasks.register("copyTest", Copy) {
from(file('../../local/test.txt'))
into(buildDir)
logger.lifecycle("DEST: $buildDir")
}
// build.dependsOn copyTestTask
// tasks.getByPath(':app:processDebugResources') {
// it.dependsOn(copyTestTask)
// }
})
}
The comments are my first tries to add to copy task in my assembleDebug/assembleRelease build. They do not call my copy task.
My questions:
What is the correct way to set the dependency to my task?
Which build task should depend on my copy job?
We are going to debug our Android app by adding some properties in app.properties and pre-process it in the Kotlin codes.
We wrote build.gradle like:
task dailytest {
doLast {
File testProperty = new File('assets/app.properties')
testProperty.append("\ndaily_test=true")
testProperty.append("\nfps_sample_interval_ms=")
testProperty.append(fps_sample_interval_ms)
testProperty.append("\ndrop_stack_sample_interval_ms=")
testProperty.append(drop_stack_sample_interval_ms)
testProperty.append("\nmin_drop_count_to_log=")
testProperty.append(min_drop_count_to_log)
}
}
And we compile it in command line using:
./gradlew dailytest -Pfps_sample_interval_ms="100" -Pdrop_stack_sample_interval_ms="100" -Pmin_drop_count_to_log="1" :connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.myapp.sub.MainActivityTest#test_click_shelf
I was wondering if maybe we could run this test case in Android Studio by adding some confirmations?
We did try to add ::dailytest in the Before launch part, but we couldn't manage to find a way to add the custom parameters.
You can have an afterEvaluate block in your build.gradle to run your task when you build your app, something like this:
afterEvaluate {
lint.dependsOn dailytest
}
I am trying to integrate code coverage in gradle Kotlin as suggested in https://techblog.tbauctions.com/kotlin-azure-and-code-coverage/ but it's not working, below are the project files.
build.gradle.kts
plugins {
jacoco
}
val test by tasks.getting(Test::class) {
useJUnitPlatform { }
}
tasks.withType(JacocoReport::class.java).all {
reports {
xml.isEnabled = true
xml.destination = File("$buildDir/reports/jacoco/report.xml")
}
}
tasks.withType<Test> {
jacoco {
toolVersion = "0.8.3"
reportsDir = file("$buildDir/reports/jacoco")
}
finalizedBy("jacocoTestReport")
}
Error - Task with name 'test' not found in project ':app'
Also tried https://docs.gradle.org/current/userguide/jacoco_plugin.html
How to generate and publish Code Coverage in gradle kotlin using Azure DevOps?
According to the error message:
Error - Task with name 'test' not found in project ':app'
It seems the task called test is not provided in the project. When using a JVM language plugin like Java in Gradle, a task called test is automatically provided. This task runs all tests under src/test by default.
The Java plugin adds a number of tasks to your project, including the test task:
The Java Plugin:
So, make sure you have add plugin Java in Gradle.
You could check this document for some more details.
Hope this helps.
I have an android library gradle project. And I need to copy some files to assets folder for robolectric unit tests.
To do it I've defined a copy task:
task copyDebugAssets(type: Copy) {
from "${projectDir}/somewhere"
into "${buildDir}/intermediates/bundles/debug/assets"
}
but I can't add this task as a dependency for processDebugResources task:
processDebugResources.dependsOn copyDebugAssets
because of this error:
Could not get unknown property 'processDebugResources' for object of type com.android.build.gradle.LibraryExtension.
Now I have to manually execute this task before unit test:
./gradlew clean copyDebugAssets test
How can I solve it?
The android plugin adds several tasks dynamically. Your .dependsOn line doesn't work because at the time gradle is trying to process this line, processDebugResources task yet available. You should tell gradle to add the dependency as soon as the upstream task is available:
tasks.whenTaskAdded { task ->
if (task.name == 'processDebugResources') {
task.dependsOn copyDebugAssets
}
}
Why copy? Configure where the assets should be pulled from:
android {
// other cool stuff here
sourceSets {
androidTest {
assets.srcDirs = ['../testAssets']
}
}
}
(replacing ../testAssets with a path to where the assets should come from)
I have used this successfully with androidTest for instrumentation testing. AFAIK, it should work for test or any other source set.
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
}