如何添加" something.a"库文件到我的C ++ Android Gradle项目?

时间:2016-09-14 13:12:09

标签: android c++ android-studio gradle android-ndk

我有一个名为somelibrary/lib/c++/armeabi-v7a/libsomelibrary.a的库。如何将其添加到Android项目中的Gradle文件中?

显然,该库还包含somelibrary/include/somelibrary/*.h下的标题,但这很简单,因为我可以在cppFlags.addAll(['-I' + file('somepath/somelibrary/include')])添加一行build.gradle

但是我如何将.a文件添加到项目中,以便链接适用于所有二进制格式,不仅是armeabi-v7a而且还有arm64-v8a,armeabi和x86?该库包含所有这些二进制格式的.a文件版本。除此之外,它还包含两个不同的c ++标准库的版本:" c ++"和" gnustl"。我没有图书馆的源代码。

1 个答案:

答案 0 :(得分:2)

这取决于gradle版本的使用情况,如果是build:gradle-experimental:0.7.3'

在app build.gradle文件中:

  • 首先定义您的库存储库

    repositories {
        libs(PrebuiltLibraries) {
            your_lib {
               headers.srcDir "${your_lib_path}/include"
               binaries.withType(StaticLibraryBinary) {
               staticLibraryFile =   file("${your_lib_path}/your_library.a")
                }
            }
      }
    }
    
  • 定义你的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" 
      }
    
  • 定义库源

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

     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"])
                    }
     }
    

整个代码:

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"])
        }
    }

  }

您必须根据您的gradle版本修改您的gradle文件。

希望,这有帮助。

干杯。

垂发。