简体   繁体   English

如何在我的C ++ Android Gradle项目中添加“something.a”库文件?

[英]How do I add a “something.a” library file to my C++ Android Gradle project?

I have a library called somelibrary/lib/c++/armeabi-v7a/libsomelibrary.a . 我有一个名为somelibrary/lib/c++/armeabi-v7a/libsomelibrary.a How do I add it to the Gradle files in my Android project? 如何将其添加到Android项目中的Gradle文件中?

Obviously the library also contains the headers under somelibrary/include/somelibrary/*.h but that's the easy part because I can just add a line cppFlags.addAll(['-I' + file('somepath/somelibrary/include')]) to my build.gradle . 显然该库还包含somelibrary/include/somelibrary/*.h下的标题, somelibrary/include/somelibrary/*.h很简单,因为我可以添加一行cppFlags.addAll(['-I' + file('somepath/somelibrary/include')])到我的build.gradle

But how do I also add the .a file to the project so that the linking works on all binary formats, not only armeabi-v7a but also arm64-v8a, armeabi and x86? 但是我如何将.a文件添加到项目中,以便链接适用于所有二进制格式,不仅是armeabi-v7a而且还有arm64-v8a,armeabi和x86? The library contains versions of the .a file for all those binary formats. 该库包含所有这些二进制格式的.a文件版本。 And in addition to that, it also contains versions for two different c++ standard libraries: "c++" and "gnustl". 除此之外,它还包含两个不同的c ++标准库的版本:“c ++”和“gnustl”。 I don't have the source code of the library. 我没有库的源代码。

It will depend on what gradle version are using, in case of build:gradle-experimental:0.7.3' 它取决于gradle版本使用的内容,如果是build:gradle-experimental:0.7.3'

In app build.gradle file: 在app build.gradle文件中:

  • Firstly define your library repository 首先定义库存储库

     repositories { libs(PrebuiltLibraries) { your_lib { headers.srcDir "${your_lib_path}/include" binaries.withType(StaticLibraryBinary) { staticLibraryFile = file("${your_lib_path}/your_library.a") } } } } 
  • Define your ndk-module 定义你的ndk模块

      android.ndk { moduleName = "your_ndk_module" platformVersion = 9 toolchain "gcc" debuggable true cppFlags.add("-fexceptions") cppFlags.add("-std=c++11") stl = "gnustl_static" } 
  • define library sources 定义库源

     android.sources { main { jni { dependencies { library "your_lib" linkage "static" } } } } 
  • define your product flavours 定义您的产品口味

      android.productFlavors { create("arm") { ndk.abiFilters.add("armeabi") ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } create("arm7") { ndk.abiFilters.add("armeabi-v7a") ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi-v7a')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } create("arm8") { ndk.abiFilters.add("arm64-v8a") ndk.ldFlags.add("-L${file('src/main/jniLibs/arm64-v8a')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } } 

Whole code: 整码:

apply plugin: 'com.android.model.application'

def your_lib_path = file(project(':app').projectDir).absolutePath + "/your_lib_path"
model {
    repositories {
        libs(PrebuiltLibraries) {
            your_lib {
                headers.srcDir "${your_lib_path}/include"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${your_lib_path}/your_library.a")
                }
            }
      }
  }
  //define your ndk-module: 
  android.ndk {
       moduleName = "your_ndk_module"
       platformVersion = 9
       toolchain "gcc"
       debuggable true
       cppFlags.add("-fexceptions")
       cppFlags.add("-std=c++11")
       stl = "gnustl_static" // Which STL library to use: gnustl or stlport
       }
  //define your android sources
  android.sources {
       main {
           jni {
               dependencies {
                   library "your_lib" linkage "static"
               }
           }
       }
   }

//define your product flavours: 
android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
            ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi')}".toString())
            ndk.ldLibs.addAll(["your_lib"])

        }
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
            ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi-v7a')}".toString())
            ndk.ldLibs.addAll(["your_lib"])

        }
        create("arm8") {
            ndk.abiFilters.add("arm64-v8a")
            ndk.ldFlags.add("-L${file('src/main/jniLibs/arm64-v8a')}".toString())
            ndk.ldLibs.addAll(["your_lib"])
        }
    }

  }

You will have to modify your gradle file according to your gradle version. 您必须根据您的gradle版本修改gradle文件。

Hope, this helps. 希望这可以帮助。

Cheers. 干杯。

Unai. 垂发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM