如何将JNI文件链接到外部Android C库?

时间:2016-10-04 15:50:10

标签: android android-ndk cmake java-native-interface

我正在尝试在我的Android项目中使用mbedtls。它使用CMake,方便,似乎最简单的方法是使它保持原样,然后添加一个包含JNI包装器的额外C文件。

我已将源代码下载到./app/mbedtls并将其添加到./app/build.gradle

defaultConfig {
    ...
    externalNativeBuild {
        cmake {
            arguments "-DENABLE_TESTING=OFF" // Testing requires Perl which I don't have.
        }
    }
}
...
externalNativeBuild {
    cmake {
        path "mbedtls/CMakeLists.txt"
    }
}

执行此操作后,gradle构建成功完成(有点令人惊讶!)虽然它实际上并不生成任何libx.a文件。

我还创建了一个文件./app/CMakeLists.txt,其中包含:

cmake_minimum_required(VERSION 3.4.1)
add_library(mbedtls_jni SHARED src/main/cpp/mbedtls_jni.cpp)

我已创建该文件。但是如何才能将该文件和mbedtls链接到一个库中呢?理想情况下,不需要修改mbedtls中的任何内容。

1 个答案:

答案 0 :(得分:0)

试试这个:

  
      
  1. ./应用程序/ SRC /主/ CPP / mbedtls
  2.   

将所有mbedtls library files(*.c and *.h)复制到mbedtls文件夹,然后将所有* .c重命名为* .cpp

  
      
  1. ./应用程序/ SRC /主/ CPP / yourmain.cpp
  2.   

cpp文件夹中的主程序(yourmain.cpp和yourmain.h)。

  
      
  1. ./应用程序/的CMakeLists.txt
  2.   
cmake_minimum_required(VERSION 3.4.1)
    option(USE_SHARED_MBEDTLS_LIBRARY "Build mbedtls TLS shared library." ON)

    find_library( log-lib
                  log )

    set(src_mbed
        src/main/cpp/mbedtls/aes.cpp
        src/main/cpp/mbedtls/aesni.cpp
        src/main/cpp/mbedtls/arc4.cpp
        src/main/cpp/mbedtls/asn1.cpp
        src/main/cpp/mbedtls/asn1write.cpp
        src/main/cpp/mbedtls/base64.cpp
        src/main/cpp/mbedtls/bignum.cpp
        src/main/cpp/mbedtls/blowfish.cpp
        src/main/cpp/mbedtls/camellia.cpp
        src/main/cpp/mbedtls/ccm.cpp
        src/main/cpp/mbedtls/cipher.cpp
        src/main/cpp/mbedtls/cipher_wrap.cpp
        src/main/cpp/mbedtls/cmac.cpp
        src/main/cpp/mbedtls/ctr_drbg.cpp
        src/main/cpp/mbedtls/des.cpp
        src/main/cpp/mbedtls/dhm.cpp
        src/main/cpp/mbedtls/ecdh.cpp
        src/main/cpp/mbedtls/ecdsa.cpp
        src/main/cpp/mbedtls/ecjpake.cpp
        src/main/cpp/mbedtls/ecp.cpp
        src/main/cpp/mbedtls/ecp_curves.cpp
        src/main/cpp/mbedtls/entropy.cpp
        src/main/cpp/mbedtls/entropy_poll.cpp
        src/main/cpp/mbedtls/error.cpp
        src/main/cpp/mbedtls/gcm.cpp
        src/main/cpp/mbedtls/havege.cpp
        src/main/cpp/mbedtls/hmac_drbg.cpp
        src/main/cpp/mbedtls/md.cpp
        src/main/cpp/mbedtls/md2.cpp
        src/main/cpp/mbedtls/md4.cpp
        src/main/cpp/mbedtls/md5.cpp
        src/main/cpp/mbedtls/md_wrap.cpp
        src/main/cpp/mbedtls/memory_buffer_alloc.cpp
        src/main/cpp/mbedtls/oid.cpp
        src/main/cpp/mbedtls/padlock.cpp
        src/main/cpp/mbedtls/pem.cpp
        src/main/cpp/mbedtls/pk.cpp
        src/main/cpp/mbedtls/pk_wrap.cpp
        src/main/cpp/mbedtls/pkcs12.cpp
        src/main/cpp/mbedtls/pkcs5.cpp
        src/main/cpp/mbedtls/pkparse.cpp
        src/main/cpp/mbedtls/pkwrite.cpp
        src/main/cpp/mbedtls/platform.cpp
        src/main/cpp/mbedtls/ripemd160.cpp
        src/main/cpp/mbedtls/rsa.cpp
        src/main/cpp/mbedtls/sha1.cpp
        src/main/cpp/mbedtls/sha256.cpp
        src/main/cpp/mbedtls/sha512.cpp
        src/main/cpp/mbedtls/threading.cpp
        src/main/cpp/mbedtls/timing.cpp
        src/main/cpp/mbedtls/version.cpp
        src/main/cpp/mbedtls/version_features.cpp
        src/main/cpp/mbedtls/xtea.cpp
        src/main/cpp/mbedtls/certs.cpp
        src/main/cpp/mbedtls/pkcs11.cpp
        src/main/cpp/mbedtls/x509.cpp
        src/main/cpp/mbedtls/x509_create.cpp
        src/main/cpp/mbedtls/x509_crl.cpp
        src/main/cpp/mbedtls/x509_crt.cpp
        src/main/cpp/mbedtls/x509_csr.cpp
        src/main/cpp/mbedtls/x509write_crt.cpp
        src/main/cpp/mbedtls/x509write_csr.cpp
        src/main/cpp/mbedtls/debug.cpp
        src/main/cpp/mbedtls/net_sockets.cpp
        src/main/cpp/mbedtls/ssl_cache.cpp
        src/main/cpp/mbedtls/ssl_ciphersuites.cpp
        src/main/cpp/mbedtls/ssl_cli.cpp
        src/main/cpp/mbedtls/ssl_cookie.cpp
        src/main/cpp/mbedtls/ssl_srv.cpp
        src/main/cpp/mbedtls/ssl_ticket.cpp
        src/main/cpp/mbedtls/ssl_tls.cpp
        src/main/cpp/yourmain.cpp
        )

    add_library(yourmain SHARED ${src_mbed})
    set_target_properties(yourmain PROPERTIES VERSION 2.4.2 SOVERSION 0)
    target_link_libraries(yourmain ${mbedtls} ${log-lib})
  
      
  1. ./应用程序/ buid.grandle
  2.   
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26

    buildToolsVersion "27.0.3"

    defaultConfig {
        applicationId "yourdomain.yourapp"
        minSdkVersion 22
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
                cppFlags "-fexceptions"
                cppFlags "-fvisibility=hidden"
                cppFlags "-fvisibility-inlines-hidden"
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"  // this is required
        }
    }

    buildTypes {
        debug {
            debuggable true
            jniDebuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support:design:26.0.0-alpha1'
    compile 'com.android.support:cardview-v7:26.0.0-alpha1'
    testCompile 'junit:junit:4.12'
}