1. Summary
I am having an problem. 1 of my custom tasks running depended on each other is having difficulties extracting
tar.gz
files. Thus failing entire build.
I want to achieve extraction of those tars, but as it can be experienced result is not correct.
Output from Console:
> Task :app:ndkBuilds FAILED
Android NDK: Your APP_BUILD_SCRIPT points to an unknown file: SDL-release-2.0.20/Android.mk
/home/rafal/AndroidSDK/ndk/21.4.7075529/build/core/add-application.mk:88: *** Android NDK: Aborting... . Stop.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:ndkBuilds'.
> Process 'command '/home/rafal/AndroidSDK/ndk/21.4.7075529/ndk-build'' finished with non-zero exit value 2
The cultplit task here:
extractArchives()
Code of that particular task is at bottom.
2. What have been done so far
I have tried to use
doFirts {}
Also did noticed when running a build 2nd time without cleaning. It does extract and script proceed to the next task.
3. Code
Here is entire code:
task downloadFiles(type: Download) {
src([
'https://github.com/libsdl-org/SDL/archive/release-2.0.20.tar.gz',
'https://github.com/openssl/openssl/archive/OpenSSL_1_1_1l.tar.gz',
'https://github.com/curl/curl/archive/curl-7_80_0.tar.gz'
])
dest "${buildDir}"
overwrite false
}
task extractArchives() {
dependsOn downloadFiles
fileTree("${buildDir}").matching {
include "*.tar.gz"
}.visit {
FileVisitDetails details -> exec {
commandLine ('tar', '-zxf', "${details.file.path}")
}
}
}
task ndkBuilds(type: Exec) {
dependsOn extractArchives
def ndkDir = android.ndkDirectory
executable = "$ndkDir/ndk-build"
args = ['NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_ABI := armeabi-v7a arm64-v8a',
'APP_PLATFORM=android-21',
'APP_BUILD_SCRIPT=SDL-release-2.0.20/Android.mk']
}
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(clean)) {
downloadFiles.enabled = false
extractArchives.enabled = false
ndkBuilds.enabled = false
}
}
//extractArchives.dependsOn downloadFiles
//ndkBuilds.dependsOn extractArchives
preBuild.dependsOn ndkBuilds
Related
I am trying to configure a new Android project to use JaCoCo for test coverage and fail when coverage is below a certain threshold. I have used this documentation, as well as looking at many SO posts. I expect the task to fail, since I don't have test coverage, but it succeeds without any warnings.
My :app build.gradle file includes the following:
plugins {
...
id 'jacoco'
}
jacoco {
toolVersion = "0.8.7"
reportsDirectory = layout.buildDirectory.dir('app/build/reports')
}
task jacocoTestCoverageVerification(type: JacocoCoverageVerification, dependsOn: ['jacocoTestReport']) {
onlyIf = { true }
violationRules {
failOnViolation = true
rule {
limit {
minimum = 1.0
}
}
}
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
finalizedBy jacocoTestCoverageVerification
onlyIf = {true}
reports {
xml.enabled true
html.enabled true
html.outputLocation = layout.buildDirectory.dir('app/build/reports')
}
}
tasks.withType(Test) {
finalizedBy jacocoTestReport
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
android {
...
jacoco{
version = "0.8.7"
}
}
When I try to run tests, using any of the following:
./gradlew test
./gradlew jacocoTestReport
./gradlew jacocoTestCoverageVerification
The results are always the same:
...
> Task :app:connectedDebugAndroidTest
Starting 2 tests on Nexus 5X - 8.1.0
> Task :app:createDebugAndroidTestCoverageReport
> Task :app:createDebugCoverageReport
> Task :app:jacocoTestReport
> Task :app:jacocoTestCoverageVerification
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 31s
93 actionable tasks: 93 executed
Build Analyzer results available
1:10:48 PM: Task execution finished 'test'.
I expect the task to fail, since I have no test coverage. I have added a utility class and method, and partially implemented a test so that the JaCoCo report shows 60% coverage for one class, but this has not changed anything.
My build jar task works fine in grade 3.5.3, but it doesn't work in grade 3.6.1. I checked my build files, however, the /build/intermediates/packaged-classes/ dir is weirdly missing.
The gradle scripts:
task buildJar(dependsOn: ['build'], type: Jar) {
doFirst {
manifest {
attributes 'Jar-VersionName': jarVersionName
attributes 'Jar-VersionCode': jarVersionCode
}
}
from zipTree(file('build/intermediates/packaged-classes/release/classes.jar'))
appendix = ""
baseName = "pp"
version = jarVersionName + "_" + jarVersionCode
classifier = "release"
extension = "jar"
destinationDir = file('build/libs/')
}
//dx --dex --output=target.jar origin.jar
task buildDex(dependsOn: ['buildJar'], type: Exec) {
workingDir buildDir.absolutePath + "/libs"
executable "dx"
args "--dex"
args "--output=" + buildJar.archiveName
args buildJar.archiveName
}
The error log:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':p2p:buildJar'.
> Cannot expand ZIP '/Users/william/AndroidStudioProjects/Work/lib_plugin/project/build/intermediates/packaged-classes/release/classes.jar' as it does not exist.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
I've looked around and apparently I've got the choice between these solutions, but they are so old solution for me.
No release bundle folder after upgrading Android Studio
.jar file not generating in android studio
Gradle not generate jar
So, where is the new path of the generated jar?
I found it! When I upgrade gradle version to 4.0, I find the class.jar in build/intermediates/aar_main_jar/release/classes.jar path.
so, the full build task code is below:
def jarPath = buildDir.absolutePath + "/libs"
def jarBaseName = "plugin"
task buildJar(dependsOn: ['build'], type: Jar) {
doFirst {
manifest {
attributes 'Jar-VersionCode': jarVersionCode
}
}
from zipTree(file('build/intermediates/aar_main_jar/release/classes.jar'))
// [archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]
archiveBaseName = jarBaseName
archiveAppendix = null
archiveVersion = "$jarVersionName-$jarVersionCode"
archiveClassifier = "release"
archiveExtension = "jar"
destinationDirectory = file(jarPath)
}
// dx --dex --output=target.jar origin.jar
task buildDex(dependsOn: ['buildJar'], type: Exec) {
workingDir jarPath
executable "dx"
args "--dex"
args "--output=" + buildJar.archiveFileName.get()
args buildJar.archiveFileName.get()
}
My project was running perfect with gradle plugin version 3.0.1 after updating to version 3.4.1 I am going through sync failure.
Previously I was using wrapper 4.10.1 which is now updated to 5.1.1.
afterEvaluate(new Action<Project>() {
#Override
void execute(Project project) {
tasks.getByName("assembleRelease").doLast {
tasks.copyReleaseBuild.execute() //error is here
tasks.copyReleaseBuildToXX.execute()
tasks.copyReleaseBuildToXXXX.execute()
}
}
copyReleaseBuild task is written something like below...
task copyReleaseBuild(type: Copy) {
def releaseDir = getProjectProperty('releaseDir')
if (releaseDir?.trim()) {
//if release folder is provided
def releaseAarFile =
getProjectProperty('sourceCodeDir') + "/android-corekit/kit/build/outputs/aar/kit-release.aar"
from releaseAarFile
into releaseDir
}
task copyReleaseBuildToXX(type: Copy) {
from "./build/outputs/aar/kit-release.aar"
into "../kitwrapper/libs"
}
task copyReleaseBuildToXXXX(type: Copy) {
from "./build/outputs/aar/kit-release.aar"
into "../kitwrapperapp/libs"
}
}
I have been trying to resolve from last two days but nothing is working as I lack knowledge of groovy.
Please check error log I am getting while building the project...
It happens because you are using an updated version of gradle (check the migration to gradle v5)
The following properties and methods of TaskInternal have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.
execute()
executer
getValidators()
addValidator()
Now you can't call the execute method directly, but you have to use the tasks dependencies to obtain the same result.
Just define in your build.gradle:
task copyReleaseBuild {
dependsOn 'assembleRelease'
//...
}
and remove this:
afterEvaluate(new Action<Project>() {
#Override
void execute(Project project) {
tasks.getByName("assembleRelease").doLast {
tasks.copyReleaseBuild.execute() //error is here
}
}
Upgrading your build from Gradle 4.x to 5.0
The following properties and methods of TaskInternal have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.
execute()
executer
getValidators()
addValidator()
in your case seems you have dependency between tasks: copyReleaseBuild must run after assembleRelease
so, this simple build.gradle represents this dependency:
task copyReleaseBuild {
dependsOn 'assembleRelease'
doLast {
println 'run copyReleaseBuild'
}
}
task assembleRelease {
doLast {
println 'run assembleRelease'
}
}
and the command gradle copyReleaseBuild
runs both tasks:
# gradle copyReleaseBuild
> Task :assembleRelease
run assembleRelease
> Task :copyReleaseBuild
run copyReleaseBuild
BUILD SUCCESSFUL in 2s
2 actionable tasks: 2 executed
I'm developing an Android app using FFmpeg. I have built FFmpeg into *.so files and put them into jniLibs as follows:
src
--main
----jniLibs
------armeabi
--------libavcodec-57.so
--------libavformat-57.so
--------xx.so
While in grade script, abifilter of ndk is armeabi.
In java files, I have succeeded load these .so files and the built apk also contains them. However, when I use any API (e.g. av_register_all()) of them in a .c file under src/jni folder, build error comes:
Error:(14) undefined reference to 'av_register_all'
Error:Execution failed for task ':app:compileDebugNdk'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/zhouyf/Library/Android/sdk/ndk-bundle/ndk-build'' finished with non-zero exit value 2
It seems that problem exists in linker. But I found answer that just putting .so files to jniLibs/armeabi will be OK.
Do I need modify build.gradle file to link those .so files or else?
P.S.
If I don't call the API, the app will run successfully, only with warning: W/linker: libavformat-57.so: unused DT entry: type 0x6ffffffe arg 0x60e0
W/linker: libavformat-57.so: unused DT entry: type 0x6fffffff arg 0x2
Environment:
Android Studio 2.1.1
Mac OS X 10.11.5
You should add libraries as dependencies when you build by using ndk-build.
Disable default ndk-build, in your build.gradle
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = []
}
Add your own ndk-build task, in your build.gradle
def ndkDir = properties.getProperty("ndk.dir")
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine ndkDir + File.separator + 'ndk-build.cmd', '-C', file('src/main/jni').absolutePath
} else {
commandLine ndkDir + File.separator + 'ndk-build', '-C', file('src/main/jni').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
Finally, in your Android.mk, add the libraries like this.
LOCAL_MODULE := # ...
# ...
LOCAL_LDFLAGS += -L/path/of/the/libraries -lavcodec-57 -lavformat-57 ...
I'm currently working on a nbk project for android. I have the current commands in my build.gradle, so that I can build from my gradle wrapper:
def ndkDir = "/Development/android-sdk-macosx/ndk-bundle"
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath,
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath,
'clean'
}
I am able to build using ./gradlew buildNative, but when I try gradle assembleDebug I get errors:
compiling TextRenderer.cpp failed.
/Users/user/android-ndk/san-angeles/app/src/main/jni/src/Renderers/TextRenderer.cpp:5:10: fatal error: 'ft2build.h' file not found
#include <ft2build.h>
^
1 error generated.
compiling BoxRenderer.cpp successful.
compiling triangle.cpp successful.
Finished compileSanangelesArmeabi-v7aDebugSharedLibrarySanangelesMainCpp, see full log file:///Users/user/android-ndk/san-angeles/app/build/tmp/compileSanangelesArmeabi-v7aDebugSharedLibrarySanangelesMainCpp/output.txt.
I am using a freetype library that I cross compiled with issues, but I got it passed that point (I think):
Was able to cross compile Freetype2, now what?
Edit:
I have changed my gradle build to this:
apply plugin: 'com.android.model.application'
model {
android {
...
sourceSets.main {
jniLibs.srcDir 'src/main/jni/freetype/lib'
jni.srcDirs = []
}
...
}
}
task buildNative (...){
...
}
task cleanNative (...){
...
}
But now I get this error:
Gradle sync failed: Cause: com.android.build.gradle.managed.AndroidConfig$Impl
If you don't want to struggle with the ever-changing C++ support of the experimental gradle plugin, you can simply use build.gradle generated by Android Studio. To disable automatic compilation of C++ files in the jni folder, you can override jni.srcDirs:
jni.srcDirs = []
jniLibs.srcDir 'src/main/jni/freetype/lib'
Here I override jniLibs.srcDir so that the library compiled with ndk-build will be included in the APK file.
Actually, I prefer to keep jni.srcDirs pointing at my C++ files (this way, I don't need another IDE running to work with them), and I disable the gradle tasks (somewhere in build.gradle file):
tasks.all {
task -> if (task.name.contains('compileDebugNdk') ||
task.name.contains('compileReleaseNdk'))
task.enabled = false
}
I can also teach the system to run buildNative and cleanNative when necessary:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
clean.dependsOn cleanNative
Note that the next version of Android Studio, 2.2 (now in public beta), makes ndk-build a first class citizen.