As of com.android.tools.build:gradle:1.3.0 you can run into Task with name 'testDebug' not found in project ':module'.
As in the earlier stage of the build environment it was not possible to test library modules properly using Robolectric & Java this workaround was created:
afterEvaluate { project ->
android.libraryVariants.each { variant ->
println variant.name
println tasks
tasks.getByName("test${variant.name.capitalize()}") {
dependsOn "assemble${variant.name.capitalize()}"
}
}
}
With version 1.3.0 this is broken.
They have changed the name from testDebug to testDebugUnitTest hence the code above needs to be changed to:
afterEvaluate { project ->
android.libraryVariants.each { variant ->
println variant.name
println tasks
tasks.getByName("test${variant.name.capitalize()}UnitTest") {
dependsOn "assemble${variant.name.capitalize()}"
}
}
}
Related
I updated the following:
//gradle
classpath 'com.android.tools.build:gradle:3.0.0-beta6'
// library dependencies
implementation "com.android.support:appcompat-v7:26.1.0"
implementation "com.google.code.gson:gson:2.7"
implementation "com.google.android.gms:play-services-location:11.2.2"
I am now getting the following exception kinds of exceptions for the gradle task androidJavadocs.
error: package com.google.android.gms.security does not exist
error: package com.google.gson does not exist
error: cannot find symbol class NonNull
Here is the gradle task that used to allow me to package up the javadocs but this no longer suffices:
libraryVariants.all { variant ->
if (variant.name == 'release') {
task docs(type: Javadoc) {
println 'docs task'
source = variant.javaCompiler.source
classpath += files(((Object) android.bootClasspath.join(File.pathSeparator)))
classpath += files(variant.javaCompiler.classpath.files)
}
}
}
I have tried lots of different combinations of gradle tasks and workarounds that I've found searching around but nothing works and I continue to get these errors. I have tried cleaning the project and invalidating the cache. Any ideas?
Adding the following to my upload-archives.gradle file fixed the problem:
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
// this is new
android.libraryVariants.all { variant ->
if (variant.name == 'release') {
owner.classpath += variant.javaCompiler.classpath
}
}
// end of new
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
After updating Android Studio to version 2.2 and the Gradle-plugin to 2.2.0, I get following error:
"Could not get unknown property 'assembleRelease' for project ':app' of type org.gradle.api.Project."
When I change the plugin version back to 2.1.3 the code still works, but that's no long-term option for me.
My code:
apply plugin: 'com.android.application'
dependencies {
...
}
android {
...
}
...
assembleRelease.doLast {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}
Hint:
project.ext.androidVersionCode is a variable defined otherwhere and contains a build number. The code in assembleRelease.doLast shall just move/rename the generated apk file.
Thank you for advices!
tangens
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease') {
task.finalizedBy 'yourRenameTasks'
}
}
You may rewrite your task a bit and try like this:
task renameBuildTask() << {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
dependsOn 'assembleRelease'
}
Also you can check this question to get better understanding.
EDIT
As #tangens said in a comment:
It works when I replace the call gradle assemble by e.g. gradle renameBuildTask. Thank you! The answer contains an error. Correct would be: task renameBuildTask() << { ... }
maybe wrap code in afterEvaluate{} will be work:
afterEvaluate {
assembleRelease.doLast {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}
}
gradle-2.14.1 and android gradle plugin 2.2.0
details:
Could not get unknown property 'assembleDebug' (2.2-beta)
I had the same problem after upgrading Android Studio to 2.2 and Gradle to 2.2.
I have task copyApk that needs to be run at the end of building. For brevity, let me skip what was working before, and post only what is working right now:
tasks.create(name: 'copyApk', type: Copy) {
from 'build/outputs/apk/myapp-official-release.apk'
into '.../mobile'
rename('myapp-official-release.apk', 'myapp.apk')
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease') {
task.dependsOn 'copyApk'
}
}
Gradle console shows copyApk was run near the end after packageOfficialRelease, assembleOfficialRelease, right before the last task assembleRelease. "Official" is a flavor of the app.
I got the workaround from this SO post. I essentially copied the answer here for your convenience. All credits go to the author of that post.
inside buildTypes {} method, I put this code : worked like a charm
task setEnvRelease << {
ant.propertyfile(
file: "src/main/assets/build.properties") {
entry(key: "EO_WS_DEPLOY_ADDR", value: "http://PRODUCTION IP")
}
}
task setEnvDebug << {
ant.propertyfile(
file: "src/main/assets/build.properties") {
entry(key: "EO_WS_DEPLOY_ADDR", value: "http://DEBUG IP TEST")
}
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug') {
task.dependsOn 'setEnvDebug'
} else if (task.name == 'assembleRelease') {
task.dependsOn 'setEnvRelease'
}
}
you can do this:
task copyApk(dependsOn: "assembleRelease") << {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}
I am using 'com.android.tools.build:gradle:2.2.0-alpha6' and Android Studio 2.2 Preview 6. The build runs perfectly fine on Gradle 2.1.0, but to enable instant run it asks me to update Gradle plugin.
On updating Gradle plugin, the build shows "Error:Could not find property 'assembleDebug' on project ':app'". I already tried cleaning .gradle and .idea and reloading the project, but nothing works.
Please help.
find which task is depending on assembleDebug task
changing the following did the trick for me at least:
from:
task findbugs(type: FindBugs, dependsOn: assembleDebug)
to:
task findbugs(type: FindBugs, dependsOn: "assembleDebug")
so just surrounding the task with quotes was enough.
It's from In that case, a workaround is this way:
//assembleDebug.doFirst {
// println '=============assembleDebug============='
//}
//assembleRelease.doFirst {
// println '=============assembleRelease============='
//}
//
// =======>
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug') {
//task.dependsOn 'checkstyle', 'findbugs', 'pmd', 'lint'
println '=============assembleDebug============='
} else if (task.name == 'assembleRelease') {
//task.dependsOn 'checkstyle', 'findbugs', 'pmd', 'lint'
println '=============assembleRelease============='
}
}
If you have no any "assemble" in your project, so check an answer from this post:
Could not get unknown property 'assemble'
I've been building some tasks for a gradle multi-project build and have a need to get the class path for a project. The build script has projects that use the Java plugin and projects that use the Android plugin.
For the Java projects I was able to use the top voted answer in this question to get the class path using configurations.runtime.asPath; however, this is not working for the Android projects because there is no configurations.runtime property.
How can generate a classpath for a gradle project using the Android plugin?
Android projects may build multiple versions of the app. These are called variants. The most basic variants are "debug" and "release" The following code should create the classpath assignment for all the variants in a project. Place this code in the "build.gradle" file for the module.
android.applicationVariants.each { variant ->
variant.javaCompile.classpath += configurations.provided
}
You should be able to refer to a specific variant using the variant name:
debug.javaCompile.classpath
Here is a gradle task that generates the module jar and includes also the test classpath for all variants.
It is including libraries and the android.jar from selected runtime.
I've added two commandline executions for updating some env var inside emacs and for killing any running beanshell (with previous classpath).
task classpath(type: Jar) {
from android.sourceSets.main.java.srcDirs,
android.sourceSets.test.java.srcDirs
outputs.upToDateWhen { false }
doLast {
println "Building classpath..."
def cp2 = [android.getBootClasspath()[0], it.archivePath]
android.applicationVariants.all { v ->
cp2 += v.getApkLibraries()
}
def classpath = cp2.unique().join(":")
println "Updating emacs..."
exec {
executable "sh"
args "-c", "emacsclient --eval '(setenv \"CLASSPATH\" \""+classpath+"\")'"
}
exec {
executable "sh"
args "-c", "emacsclient --eval '(jdee-bsh-exit)'"
}
}
}
Be aware that I'm using ":" for joining the classpath
project.android.applicationVariants.all { v ->
v.getCompileClasspath(null).getFiles().each{
File f->
f.getAbsolutePath()//this is the one of classpath
}
}
Here is another example a gradle task that generates javadocs with umlgraph + graphiz in an android project and includes classpath for all variants using the coding example given in the user1737310's previous answer. It is manually including android.jar from the selected runtime, I am still looking for a way to retrieve it dynamically.
task javadoc(dependsOn: build) {
setDescription('Generates Javadoc API documentation with UMLGraph diagrams')
setGroup(JavaBasePlugin.DOCUMENTATION_GROUP)
doLast {
def javaFilePath = file('src/main/java')
def cp = [System.getenv('ANDROID_HOME')+'/platforms/android-26/android.jar'];
project.android.applicationVariants.all { v ->
v.getCompileClasspath(null).getFiles().each{
File f->
cp.add(f.getAbsolutePath())//this is the one of classpath
}
}
def classpath = ":"+cp.join(':')
if (javaFilePath.exists()) {
ant.javadoc(classpath: (configurations.umljavadoc).asPath + classpath,
sourcepath: file('src/main/java'),
packagenames: '*',
destdir: "${docsDir}/javadoc",
private: 'false',
docletpath: configurations.umljavadoc.asPath) {
doclet(name: 'org.umlgraph.doclet.UmlGraphDoc') {
param(name: '-inferrel')
param(name: '-inferdep')
param(name: '-qualify')
param(name: '-postfixpackage')
param(name: '-hide', value: 'java.*')
param(name: '-collpackages', value: 'java.util.*')
param(name: '-nodefontsize', value: '9')
param(name: '-nodefontpackagesize', value: '7')
param(name: '-link', value: 'http://java.sun.com/j2se/1.5.0/docs/guide/javadoc/doclet/spec')
param(name: '-link', value: 'http://java.sun.com/j2se/1.5/docs/api')
}
}
}
}
}
`
I have the following script in build.gradle of an app project. All the println lines work fine meaning the task is run. However no files are copied. I am new to Gradle. I am probably missing something very simple. Any tip will be greatly appreciated.
assembleRelease.doLast {
android.applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
def releaseBuildTask = tasks.create(name: 'copy', type: Copy) {
println("Step A")
from 'build/outputs/apk/'
println("Step B")
into 'build/outputs/debug/'
println("Finished")
}
releaseBuildTask.mustRunAfter variant.assemble
}
}
println "copying task finished"
}
Update (2015-01-02)
I have noticed that I can do other file tasks such as deleting, renaming without any problem, but not copying.