ndk build task not getting called when building project through command line - android

I have created following ndk task for building native component.
task clean(type: Delete) {
delete rootProject.buildDir}
task ndkBuild(type: Exec) {
println "hello 1"
commandLine 'ndk-path/ndk-build', 'NDK_PROJECT_PATH=project path'
println "hello 2"}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild}
Now when I call "./gradlew ndk-build" then .so is created under libs folder. But when I do "./gradlew assembleDebug" or "./gradlew build" then I get folowing outputs
hello 1
hello 2
But .so is not created. What is the problem?

Related

how to run other gradle command in the gradle

I have two module, app and module1
app is not dependency module1
module1 assembleRelease task will change data in app
so,I want to run module1:assembleRelease automatically when I run app:assembleRelease.
apply plugin: 'com.android.application'
android {
....
afterEvaluate {
preBuild.dependsOn({
runModuleaR.execute()
})
}
}
task runModuleaR(type: Exec) {
println "runModuleaR start"
commandLine "cmd", "gradlew", ':module1:assembleRelease'
println "runModuleaR finish"
}
is my build.gradle
But it has no effect
How do I change it?Thanks

How to run .cmd file as task in Gradle?

I have .cmd file that I want to include everytime build.gradle is run by Android Studio.
Let's say the filename is create-artifacts.cmd and so in my module's build.gradle.
android {
...
task createArtifacts(type:Exec) {
commandLine = ["create-artifacts.cmd"]
workingDir = file("$rootDir")
}
}
How may I be helped with this?
See the ExecTask documentation
task createArtifacts(type:Exec) {
commandLine 'cmd', '/c', 'create-artifacts.cmd'
workingDir rootDir
}

Run firebaseUploadReleaseProguardMapping task from app/build.gradle file

Is there any way to run gradle task from app/build.gradle file, so that when I build release APK task "firebaseUploadReleaseProguardMapping" will run automatically.
You can use dependsOn for example (your app/build.gradle):
apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.firebase-crash'
android {
}
dependencies {
}
task release
task archiveRelease(type: Copy) {
from './build/outputs/apk', './build/outputs/'
into "../releases/${rootProject.ext.configuration.version_code}"
include('app-release.apk', 'mapping/release/mapping.txt')
rename('app-release.apk', "${rootProject.ext.configuration.package}_${rootProject.ext.configuration.version_name}_${rootProject.ext.configuration.version_code}.apk")
}
project.afterEvaluate {
dependencyUpdates.dependsOn clean
assembleRelease.dependsOn clean
def publishApkRelease = project.tasks.getByName("publishApkRelease")
publishApkRelease.dependsOn assembleRelease
release.dependsOn publishApkRelease, firebaseUploadReleaseProguardMapping, archiveRelease
}
I created a new task called release. It depends on publishApkRelease (comes from gradle-play-publisher), firebaseUploadReleaseProguardMapping and archiveRelease. And publishApkRelease depends on assembleRelease.
At the ned you just call ./gradlew release and it will build your release version, uploads the apk to Google play, the mapping file to Firebase and archive a copy of the apk and mapping file.

Running ./gradlew buildNative passes, but gradle build does not

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.

Gradle build issue

I am trying to build android project with imported c-libraries and get next error.
Error:Execution failed for task ':app:compileDebugNdk'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/Applications/Android Studio.app/ndk/ndk-build NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/Users/eugene/KREF14001/app/build/ndk/debug/Android.mk APP_PLATFORM=android-19 NDK_OUT=/Users/eugene/KREF14001/app/build/ndk/debug/obj NDK_LIBS_OUT=/Users/eugene/KREF14001/app/build/ndk/debug/lib APP_ABI=all
Error Code:
2
Output:
usage: dirname path
/Users/eugene/build/core/init.mk:471: * Android NDK: Aborting . Stop.
What does it mean? How can i fix this problem?
I've read that the NDK_HOME shouldn't contain any space in its path (even if it's \ escaped).
This is the code i use to build using android-ndk from gradle. For this add ndk directory path in gradle.properties ie . add ndkdir=/home/user/android-ndk-r9d and put all jni files in a folder native in src/main/ as you can see from code posted below. It will create jar with native libs which you can use normally as in System.loadLibrary("library");
dependencies {
compile fileTree(dir: "$buildDir/native-libs", include: '*.jar')
}
task ndkBuild(type: Exec) {
commandLine "$ndkdir/ndk-build", "--directory", "$projectDir/src/main/native", '-j', Runtime.runtime.availableProcessors(),
"APP_PLATFORM=android-8",
"APP_BUILD_SCRIPT=$projectDir/src/main/native/Android.mk",
"NDK_OUT=$buildDir/native/obj",
"NDK_APP_DST_DIR=$buildDir/native/libs/\$(TARGET_ARCH_ABI)"
}
task nativeLibsToJar(type: Zip, description: 'create a jar with native libs') {
destinationDir file("$buildDir/native-libs")
baseName 'native-libs'
extension 'jar'
from fileTree(dir: "$buildDir/native/libs", include: '**/*.so')
into 'lib/'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn nativeLibsToJar
}
nativeLibsToJar.dependsOn 'ndkBuild'

Categories

Resources