Android nkd-build script finisched with error (exit value 2) - android

Every time i try to build an android app, that uses ndk to run native code, i get the following error:
Error:Gradle: Execution failed for task ':sampleNDK:ndkBuild'.
> Process 'command 'C:\NDK\android-ndk-r10e/ndk-build.cmd'' finished with non-zero exit value 2
I use Intellij with a grandle projekt. I think the error appears in this function in the build.gradle file:
task ndkBuild(type: Exec) {
def ndkDir = plugins.getPlugin('com.android.application').sdkHandler.getNdkFolder()
if(org.gradle.internal.os.OperatingSystem.current().windows){
commandLine "$ndkDir/ndk-build.cmd", 'NDK_PROJECT_PATH=src/main'
//does not work too
//commandLine "$ndkDir\\ndk-build.cmd", 'NDK_PROJECT_PATH=src/main'
}else{
commandLine "$ndkDir/ndk-build", 'NDK_PROJECT_PATH=src/main'
}
}
In fact that i'm using windows, i think the error appears in the if case.
Does anyone know what went wrong or what i can try to avoid the failure?
UPDATE
That's the whole build script. Maybe that is helpful.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.1'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "de.anmi.android.samplendk"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
sourceSets.main.jni.srcDirs = []
sourceSets.main.jniLibs.srcDirs = ['src/main/libs']
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
task ndkBuild(type: Exec) {
// http://stackoverflow.com/questions/28615439/android-gradle-plugin-1-1-0-getndkfolder-not-found-anymore-any-replacement
def ndkDir = plugins.getPlugin('com.android.application').sdkHandler.getNdkFolder()
if (org.gradle.internal.os.OperatingSystem.current().windows) {
commandLine "$ndkDir/ndk-build.cmd", 'NDK_PROJECT_PATH=src/main'
//does not work too
// commandLine "$ndkDir\\ndk-build.cmd", 'NDK_PROJECT_PATH=src/main'
} else {
commandLine "$ndkDir/ndk-build", 'NDK_PROJECT_PATH=src/main'
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}

Related

No signature of method: when building flutter app with flavours

I added and updated build.gradle file so I can have flavours in builds for the flutter app.
following are my files.
app/build.gradle
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
android {
compileSdkVersion 33 //flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
applicationId "com.fitt_app.app_android"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
minSdkVersion 20 //flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
flavorDimensions "default"
productFlavors {
dev {
dimension "default"
applicationIdSuffix ".dev"
versionNamSuffix "-dev"
resValue "string", "app_name", "[DEV] FittApp"
}
qa {
dimension "default"
applicationIdSuffix ".qa"
versionNamSuffix "-qa"
resValue "string", "app_name", "[QA] FittApp"
}
uat {
dimension "default"
applicationIdSuffix ".uat"
versionNamSuffix "-uat"
resValue "string", "app_name", "[UAT] FittApp"
}
prod {
dimension "default"
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "androidx.multidex:multidex:2.0.1"
}
build.gradle
buildscript {
ext.kotlin_version = '1.6.10'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
when I try to do debug or run with the command flutter run -t lib/main_dev.dart --flavor dev or choosing build configuration from the android studio I always end up getting the error.
Launching lib/main_dev.dart on sdk gphone64 arm64 in debug mode...
Running Gradle task 'assembleDebug'...
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/jasondwaku/Documents/FlutterProjects/FitnessPassportNZ/fpnz/android/app/build.gradle' line: 34
* What went wrong:
A problem occurred evaluating project ':app'.
> No signature of method: build_888rk26lsi0wg9imxmtj0txq3.android() is applicable for argument types: (build_888rk26lsi0wg9imxmtj0txq3$_run_closure2) values: [build_888rk26lsi0wg9imxmtj0txq3$_run_closure2#4d8250d6]
* 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
Exception: Gradle task assembleDebug failed with exit code 1
I tried to find a solution but nothing works so far. Glad if someone can help me with this.

Failed ndkBuild process 'command 'sh' on Windows or Linux Android Studio

for several weeks i learn how to compile IBR-DTN source from IBR-DTN Github , and now im being stuck at Android NDK stuff.
here my failure log on Android Studio, i tried Windows version and Linux version there are always same error
Error:Execution failed for task ':app:ndkBuild'.
> A problem occurred starting process 'command 'sh''
and this is my build.gradle
apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'
def createVersionName() {
if (System.getenv().containsKey("BUILD_NUMBER")) {
if (System.getenv().containsKey("GIT_COMMIT")) {
return rootProject.ext.versionName + "-" + System.getenv("GIT_COMMIT").substring(0, 7)
} else {
return rootProject.ext.versionName + "-" + System.getenv("BUILD_NUMBER")
}
} else {
return rootProject.ext.versionName
}
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "de.tubs.ibr.dtn"
minSdkVersion 9
targetSdkVersion 22
versionCode System.getenv("BUILD_NUMBER") as Integer ?: rootProject.ext.versionCode
versionName createVersionName()
}
signingConfigs {
release {
storeFile file(System.getenv("KEYSTORE") ?: "publish.keystore")
storePassword System.getenv("KEYSTORE_PASSWD")
keyAlias System.getenv("KEY_ALIAS") ?: android.defaultConfig.applicationId
keyPassword System.getenv("KEY_PASSWD")
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = []
}
}
// enable signing if the keystore exists
if (android.signingConfigs.release.storeFile.exists()) {
android.buildTypes.release.signingConfig = android.signingConfigs.release
}
task ndkBuild(type: Exec) {
executable "sh"
workingDir "src/main/jni"
args "build.sh"
}
preBuild.dependsOn ndkBuild
dependencies {
compile 'de.tubs.ibr.dtn:library:1.1.2'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.jjoe64:graphview:3.1.+'
compile 'com.google.zxing:core:3.2.+'
compile 'com.google.zxing:android-integration:3.2.+'
}
Here is the error log from Android Studio:
Looking for help to finish my thesis, i appreciate every single advice here. MUCH LOVE XOXO
I haven't seen this syntax for defining a Gradle exec task before.
task ndkBuild(type: Exec) {
executable "sh"
workingDir "src/main/jni"
args "build.sh"
}
I would define it like this instead, per the Gradle docs:
task ndkBuild(type: Exec) {
if (isWindows()) {
commandLine 'cmd', '/c', 'src/main/jni/build.bat'
}
else {
commandLine 'src/main/jni/build.sh'
}
}
I was fighting against the same problem (also with IBR DTN). I could fix the problem by adding the NDK-path to the PATH variable inside the src/main/jni/build.sh script.
#!/bin/sh
PATH=/…/android-sdk/ndk-bundle/:$PATH
export PATH
…
And this link might also be helpful:
https://github.com/ibrdtn/ibrdtn/wiki/Build-IBR-DTN-for-Android
Well for starter I would suggest you use exactly the same ndk version against which they tested. ndk. Download and extract it and then simply change variable path directly in src/main/jni/build.sh. Then run the script it will build it and lastly run ./gradlew. I have tested it again and again, it works only on Linux. I used mac but there were some scary errors. I am also doing thesis on it
I think that your application need ROOT permission.

Execution failed for task ':app:buildNative'

The same error question has been asked on SO, but their solutions didn't work for me. I couldn't figure it out myself, therefore I need help. Here's my build.gradle:
apply plugin: 'com.android.application'
def VUFORIA_SDK_DIR = '../../..'
def JAR_DIR = 'build/java/vuforia'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir "src/main/libs"
}
defaultConfig {
applicationId "com.qualcomm.QCARSamples.ImageTargets"
minSdkVersion 8
targetSdkVersion 22
versionCode 200
versionName "5.0"
}
archivesBaseName = rootProject.projectDir.getName()
buildTypes {
release {
minifyEnabled false
ndk {
abiFilters "armeabi-v7a"
}
}
debug {
minifyEnabled false
debuggable true
ndk {
abiFilters "armeabi-v7a"
}
}
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
println('compiling jni code with ndk-build...')
def ndkDir = android.ndkDirectory
if (System.properties['os.name'].toLowerCase().contains('windows')) {
commandLine "$ndkDir/ndk-build.cmd",
'-C', file('src/main/jni').absolutePath
// Additional ndk-build arguments, such as NDK_DEBUG, can be provided here
} else {
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath
// Additional ndk-build arguments, such as NDK_DEBUG, can be provided here
}
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = android.ndkDirectory
if (System.properties['os.name'].toLowerCase().contains('windows')) {
commandLine "$ndkDir/ndk-build.cmd",
'-C', file('src/main/jni').absolutePath,
'clean'
} else {
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath,
'clean'
}
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
}
dependencies {
compile files("$VUFORIA_SDK_DIR/$JAR_DIR/Vuforia.jar")
}
Gradle build always fails with this error:
Error:Execution failed for task ':app:buildNative'.
A problem occurred starting process 'command 'null/ndk-build''
What could be the reason?
My reading of what you have here is as follows:
def ndkDir = android.ndkDirectory
May be returning a null value. The other option i see is that
commandLine "$ndkDir/ndk-build",
Doesn't properly reference your ndkDir variable, and your commandLine execution fails on that.
Error:Execution failed for task ':app:buildNative'. A problem occurred starting process 'command 'null/ndk-build''
aligns well with those two theories.
Try ensuring you don't get a null back when trying to asign to ndkDir, and/or converting the variable to a string prior to the commandLine command.

Android NDK- Error:Execution failed for task ':app:buildNative'

I am developing an Video Compression app so i am using NDK and getting error when run the app.
I seen the many stackOverflow Questions (Execution error in app:buildNative) but the stackOverflow solutions are not works for this error.
Gradle Build Message:
Gradle tasks [:app:assembleDebug]
:app:buildNative FAILED
Error:Execution failed for task ':app:buildNative'.
> A problem occurred starting process 'command 'null/ndk-build.cmd''
Is anyone help for my questions?
build.gradle(Module:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId "com.xxxx.videocompressor"
minSdkVersion 16
targetSdkVersion 22
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
jniLibs.srcDir 'src/main/libs'
// This is not necessary unless you have precompiled libraries in your project.
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir/ndk-build.cmd",
'-C', file('src/main/jni/').absolutePath, // Change src/main/jni the relative path to your jni source
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir/ndk-build.cmd",
'-C', file('src/main/jni/').absolutePath, // Change src/main/jni the relative path to your jni source
'clean'
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
productFlavors {
}
}
dependencies {
compile 'com.android.support:support-v4:22.2.+'
compile 'com.android.support:support-v4:22.2.+'
compile 'com.android.support:appcompat-v7:22.2.+'
compile 'com.android.support:recyclerview-v7:22.2.+'
compile 'com.android.support:design:22.2.+'
compile 'com.google.android.gms:play-services-ads:7.8.0'
}
Instead of using only this
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
Use this
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}

Configure Gradle to execute custom build step before starting compilation

I've started using Gradle today and after searching for an hour and trying every possible answer from SO (e.g. 1) and different blogs (e.g. 2) and documentations (e.g. 3) I need some help.
My question is simple: How to execute a custom build-step (in my case the execution of ndk-build with a customized Android.mk) as part of the regular build-process?
The build.gradle looks like this:
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "myApp.prototype"
minSdkVersion 16
targetSdkVersion 19
testApplicationId "myApp.prototype.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
sourceSets.main.jni.srcDirs = []
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
def rootDir = project.rootDir
def localProperties = new File(rootDir, "local.properties")
Properties properties = new Properties()
localProperties.withInputStream { instr ->
properties.load(instr)
}
def ndkDir = properties.getProperty('ndk.dir')
println ndkDir
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine "$ndkDir\\ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
} else {
commandLine "$ndkDir/ndk-build",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services-location:6.5+'
compile 'com.android.support:support-v4:19.1.0'
compile 'com.google.code.gson:gson:2.2.4'
compile fileTree(dir: new File(buildDir, 'libs'), include: '*.jar')
}
When executing gradle ndkBuild from the command-line, everything works fine. But I want that Android Studio automatically runs ndkBuild when it runs the rest of the Android compile procedures (such as generateDebugSources, preBuild, preDebugBuild, ...).
I have tried to attach myself to these events like this:
gradle.projectsEvaluated {
preBuild.dependsOn(ndkBuild)
}
but regardless where I put that code, or what task I use from the variety of tasks available (when running gradle tasks), nothing seems to work.
Have you tried adding a dependency for ndkBuild on JavaCompile tasks ?
android {
...
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}

Categories

Resources