2

I am Using Android Studio 1.5.1 Gradle version 2.8. Gradle experimental plugin v0.4.0 I have a library with native C code and java code as well. I am generating a library CoreLib-fat-release.aar with native libraries for all architectures(libcore.so) - arm64-v8a, armeabi, armeabi-v7a, mips, mips64, x86, x86-64. I have a second library depending on my Core library. I made Core library as shared library.

In second library gradle file:

jni {
            dependencies {
                project ":CoreLib" buildType "release" productFlavor "fat" linkage "shared"
            }
    }

It is able to build properly, but when I open and see the second library aar file, I dont see libcore.so in all architecture folders.

Eg: SecondLibrary-fat-release.aar => SecondLibrary-fat-release-zip Inside SecondLibrary-fat-release/jni folder, only x86 and x86_64 folder contains libcore.so file. Other folders (arm64-v8a, armeabi, armeabi-v7a, mips, mips64) doesnot contain libcore.so file.

Because of this reason, if I use my Second Library in a 64 bit device, it is complaining UnSatisfiedLink error saying Found 32-bit library instead of 64-bit.

HEre is my build.gradle of second library:

apply plugin: 'com.android.model.library'
model {
android {
    compileSdkVersion = 21
    buildToolsVersion = "23.0.2"
    defaultConfig.with {
        minSdkVersion.apiLevel = 21
        targetSdkVersion.apiLevel = 21
        testInstrumentationRunner = "android.test.InstrumentationTestRunner"
    }
    lintOptions.with {
        abortOnError = false
    }
}
android.ndk {
    moduleName = "secondlibrary
    cppFlags.addAll("-I${file(folder_path)}".toString(),
            "-I${file(sourcesDir +folder1)}".toString(),
            "-I${file(sourcesDir +folder2)}".toString(),
    )
    cppFlags.add("-frtti")
    cppFlags.add("-fexceptions")
    CFlags.addAll("-I${file(folder_path)}".toString(),
            "-I${file(sourcesDir +folder1)}".toString(),
            "-I${file(sourcesDir +folder2)}".toString(),
    )
    CFlags.add("-O3")
    CFlags.add("-funroll-loops")
    CFlags.add("-Wall")
    ldLibs.addAll(["android", "log", "stdc++", "c", "m", "z", "EGL", "GLESv2"])
    stl = "gnustl_shared"
}
// jni is the default dir; config this if yours is in different directory
android.sources {
    androidTest {
        java {
            source {
                srcDirs '../test_sources_path/src'
            }
        }
    }
    main {
        manifest {
            source {
                srcDirs '.'
            }
        }
        java {
            source {
                srcDir "src"
            }
        }
        jni {
            dependencies {
                project ":CoreLib" buildType "release" productFlavor "fat" linkage "shared"
            }
            source {
                srcDirs = ["jni",
                           path1,
                           path2,
                           "libcore.so"]
            }
        }
        jniLibs {
            dependencies {
                library file(path_to_binaries + "libcore.so") abi "armeabi"
                library file(path_to_binaries + "libcore.so") abi "x86"
                library file(path_to_binaries + "libcore.so") abi "armeabi-v7a"
                library file(path_to_binaries + "libcore.so") abi "mips"
                library file(path_to_binaries + "libcore.so") abi "x86_64"
                library file(path_to_binaries + "libcore.so") abi "arm64-v8a"
                library file(path_to_binaries + "libcore.so") abi "mips64"
            }
        }
    }
}
android.productFlavors {
    create("fat")
}
repositories {
    libs(PrebuiltLibraries) {
        CoreLib {
            binaries.withType(SharedLibraryBinary) {
                sharedLibraryFile = file(path_to_binaries + "${targetPlatform.getName()}/libcore.so")
            }
        }
    }
}
 }
 dependencies {
     compile project(':CoreLib-fat-release')
 }

NOTE: If I make CoreLib linkage as 'static' instead of 'shared', then all libcore.so files are copied properly to all the folders.

Please let me know if this is a programming issue or a bug in experimental plugin. Please suggest me if you know any alternative? Thank you.

  • Planning to use 0.6.0 experimental plugin, as they clearly mentioned about the improvement in native library dependency resolution. Will update this post based on my findings. http://tools.android.com/tech-docs/new-build-system/gradle-experimental **0.6.0-alpha3 DSL for specifying prebuilt libraries dependency has been changed. Updated to Gradle 2.8. Fixed various issues with native library dependency resolution.** – Chandra Lakkimsetty May 21 '16 at 18:25

1 Answers1

0

This problem got resolved by the doing the following:

  1. I have changed the experimental plugin version from 0.4.0 to 0.7.0 (gradle version 2.10).
  2. Changes required in build.gradle:

    • We need to define variables outside model element.
    • we can remove

      jniLibs {
              dependencies {
                   library file(path_to_binaries + "libcore.so") abi "armeabi"
                   library file(path_to_binaries + "libcore.so") abi "x86"
                   library file(path_to_binaries + "libcore.so") abi "armeabi-v7a"
                   library file(path_to_binaries + "libcore.so") abi "mips"
                   library file(path_to_binaries + "libcore.so") abi "x86_64"
                   library file(path_to_binaries + "libcore.so") abi "arm64-v8a"
                   library file(path_to_binaries + "libcore.so") abi "mips64"
              }
      }
      

As this block of code is sufficient:

repositories {
libs(PrebuiltLibraries) {
    CoreLib {
        binaries.withType(SharedLibraryBinary) {
            sharedLibraryFile = file(path_to_binaries + "${targetPlatform.getName()}/libcore.so")
        }
    }
}