How do I use this Gradle Script in Android Studio? - android

I found this gradle script built for android studio to allow the use of the NDK until the team can create an official (and more understandable) way of accomplishing this. I don't really understand how it works or how it ties in to a normal gradle script as I am still new to this IDE. Can anybody explain it to me?
The script is:
//////////////
// NDK Support
//////////////
// If using this, Android studio will fail run the following to set the environment
// variable for android studio:
// launchctl setenv ANDROID_NDK_HOME
// /Users/boos_patrick/Development/Android/android-ndk-r8e
// otherwise remove the dependsOn part and run ./gradlew buildNative from the
// command line
task copyNativeLibs(type: Copy, dependsOn: 'buildNative') {
dependsOn 'buildNative'
from(new File('libs')) { include '**/*.so' }
into new File(buildDir, 'native-libs')
}
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir new File(buildDir, 'native-libs')
}
task buildNative(type: Exec) {
if (System.env.ANDROID_NDK_HOME != null) {
def ndkBuild = new File(System.env.ANDROID_NDK_HOME, 'ndk-build')
commandLine ndkBuild
} else {
doLast {
println '##################'
println 'Skipping NDK build'
println 'Reason: ANDROID_NDK_HOME not set.'
println '##################'
}
}
}

Related

Gradle execute android tasks in specific order

I would like to merge Android gradle tasks into one and execute them in a specific order.
task.dependsOn did not work for me for a list of tasks, neither did the shouldRunAfter method.
task buildAll {
shouldRunAfter = ['clean', 'checkstyle', 'build']
}
What is the best way to run Android gradle tasks in specific order?
I ended up using doLast in connection with exec. In this example we define buildAll task which executes clean, checkstyle, build and lint in order - one after the other.
task buildAll {
doLast {
exec {
commandLine "${getRootDir()}/gradlew", "clean", "checkstyle", "build", "lint"
}
}
}
you can also use:
task buildAll {
doLast {
exec {
commandLine "${getRootDir()}/gradlew", "clean"
}
exec {
commandLine "${getRootDir()}/gradlew", "checkstyle"
}
exec {
commandLine "${getRootDir()}/gradlew", "build"
}
exec {
commandLine "${getRootDir()}/gradlew", "lint"
}
}
}

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
}

gradle dsl ndkBuild() not found

Error:(8, 0) Gradle DSL method not found: 'ndkBuild()'
Possible causes:<ul><li>The project 'OCR4' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file</li><li>The build file may be missing a Gradle plugin.
Apply Gradle plugin</li>
I have the above error in my code although I have included all the dependencies. The path to ndk-build is also specified
task ndkBuild(type: Exec) {
workingDir = file("${android.sdkDirectory}/ndk-bundle")
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'C:\\Users\\yogita\\Download\\android-ndk-r10e\\ndk-build.cmd', '-C', file('.').absolutePath,
'-j', Runtime.runtime.availableProcessors()
} else {
commandLine './ndk-build', '-C', file('.').absolutePath,
'-j', Runtime.runtime.availableProcessors()
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
// cleanup task
task ndkClean(type: Exec) {
workingDir = file("${android.sdkDirectory}/ndk-bundle")
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'C:\\Users\\yogita\\Download\\android-ndk-r10e\\ndk-build.cmd', '-C', file('.').absolutePath, 'clean'
} else {
commandLine './ndk-build', '-C', file('.').absolutePath, 'clean'
}
}
tasks.withType(Delete) {
cleanTask -> cleanTask.dependsOn ndkClean
}

Android Studio Gradle with native libs error

Sorry for my english...
I have last android studio (14 june 2013).
Create new Android project.
Add .so files to /libs/armeabi
Edit build.gradle to
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar','libs/jcPKCS11.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 16
}
}
task copyNativeLibs(type: Copy) {
from(new File(project(':JaCertTest').getProjectDir(), 'libs/armeabi')) { include '**/*.so' }
into new File(buildDir, 'native-libs')
}
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir new File('build/native-libs')
}
I received an error:
FAILURE: Build failed with an exception.
What went wrong:
A problem was found with the configuration of task ':JaCertTest:packageDebug'.
Directory 'build\native-libs' specified for property 'jniDir' does not exist.
How it is correct to write an build script?
This will happen if your copyNativeLibs task fails to find any files, and therefore doesn't create the "build\native-libs" directory. Are you sure that there are .so files in your "libs/armeabi" directory?
Also, keep in mind that your script won't actually compile the native code. You still need to do that yourself by running ndk-build to generate the .so libraries.
Here is an example of how to get your script to compile your native code. Note, this requires that ndk-build is in your PATH.
// Task to run ndk-build
task ndkBuild(type: Exec) {
commandLine 'ndk-build', '-j', Runtime.runtime.availableProcessors()
}
task copyNativeLibs(type: Copy) {
from(new File(project(':JaCertTest').getProjectDir(), 'libs/armeabi')) { include '**/*.so' }
into new File(buildDir, 'native-libs')
}
// Make copyNativeLibs depend on ndkBuild since we must build the libraries
// before we can copy them.
copyNativeLibs.dependsOn 'ndkBuild'
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir new File('build/native-libs')
}
Try this in your build.gradle:
dependencies {
// whatever you'd normally have here...
compile fileTree(dir: 'libs', include: '*.jar')
}
task packageNativeLibs(type: Jar) {
baseName 'libtest' // adjust to what you want your lib to be called
// libs is where normally a jni project puts objects so let's look there....
from(file('libs/armeabi/')) {
include '**/*.so'
}
into('libs/armeabi') // pay attention if using a different ABI like x86
destinationDir(file('libs/'))
}
tasks.withType(Compile) { compileTask -> compileTask.dependsOn packageNativeLibs }
clean.dependsOn 'cleanPackageNativeLibs'
Then you should be good
You can try this:
// Include the native-libs folder into the final APK
tasks.withType(com.android.build.gradle.tasks.PackageApplication) {
pkgTask ->
pkgTask.jniFolders = new HashSet<File>()
pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))
}

Categories

Resources