Android Studio CMake依賴第三方庫

來源:互聯網
上載者:User

標籤:sha   group   code   src   lis   ini   實現   apk   style   

 這裡實現一個簡單的功能在APP裡調用libnative-lib.so裡的add。libnative-lib.so去調用libthird.so裡的third_add來實現

JniUtil 
public class JniUtil {    static {        System.loadLibrary("native-lib");        System.loadLibrary("third");    }    public  static native int add(int x,int y);}

 

libnative.cpp

#include <jni.h>#include <string>#include "third.h"extern "C"{JNIEXPORT jint JNICALLJava_com_test_ndkthirdso_JniUtil_add(JNIEnv *env, jclass type, jint x, jint y) {    // TODO    return  third_add(x,y);}JNIEXPORT jstring JNICALLJava_com_test_ndkthirdso_MainActivity_stringFromJNI(        JNIEnv *env,        jobject /* this */) {    std::string hello = "Hello from C++";    return env->NewStringUTF(hello.c_str());}}

 

 

 

 

這裡編譯好一個自己寫一個libthird.so庫實現一個加法功能

third.h

#ifndef __H_THIRD#define __H_THIRD#ifdef __cplusplusextern "C" {#endifint third_add(int a, int b);#ifdef __cplusplus}#endif#endif // __H_THIRD

third.cpp

#include <jni.h>#include "third.h"int third_add(int a, int b){return a+b;};

產生如下

 

 

 

 項目工程結構如下:

 

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)add_library(native-lib             SHARED             src/main/cpp/native-lib.cpp )find_library(log-lib              log )#動態方式載入 third是libxxxx.so的xxxx部分add_library(third SHARED IMPORTED)SET(third_path ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libthird.so)#設定要串連的so的相對路徑,${ANDROID_ABI}表示so檔案的ABI類型的路徑,這一步引入了動態加入編譯的soset_target_properties(third  PROPERTIES IMPORTED_LOCATION ${third_path})MESSAGE(STATUS “src  third so path= ${third_path}”)#配置載入native依賴include_directories( ${ProjectRoot}/app/src/main/cpp/external/include )target_link_libraries(native-lib third ${log-lib} )

 

gradle

apply plugin: ‘com.android.application‘android {    compileSdkVersion 26    buildToolsVersion "26.0.0"    defaultConfig {        applicationId "com.test.ndkthirdso"        minSdkVersion 15        targetSdkVersion 26        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"        externalNativeBuild {            cmake {                cppFlags "-frtti -fexceptions"            }        }        ndk {            // Specifies the ABI configurations of your native            // libraries Gradle should build and package with your APK.            abiFilters  ‘armeabi‘, ‘armeabi-v7a‘        }    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘        }    }    externalNativeBuild {        cmake {            path "CMakeLists.txt"        }    }}dependencies {    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])    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.+‘    compile ‘com.android.support.constraint:constraint-layout:1.0.2‘    testCompile ‘junit:junit:4.12‘}

 

Android Studio CMake依賴第三方庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.