4

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?
  • I have posted my answer in the below mentioned link http://stackoverflow.com/questions/20900814/add-pre-built-so-files-in-project-using-gradle-0-7-3 – Ahmad Ali Nasir Jan 06 '14 at 11:25

3 Answers3

2

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')
}
Greg
  • 3,731
  • 1
  • 29
  • 25
  • .so files are in libs/armeabi, in pure Intellij Idea without gradle project builds and runs, but I want to put it in the android-studio with gradle – Andrey Degtiarev Jun 14 '13 at 20:53
  • Can you give more information on your project layout? I'm fairly certain that your error is due to an incorrect path somewhere. – Greg Jun 14 '13 at 23:52
0

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

insitusec
  • 191
  • 1
  • 5
0

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'))
}
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147