简体   繁体   中英

NDK - linking a shared library

I'm trying to link simple shared library(libhello.so) which I compiled to my native C++ code. Library libhello.so files are:
1. get13.h

#ifndef GET13_H
#define GET13_H
int get13();
#endif

2. get13.cpp

#include "get13.h"

int get13() {
    return 13;
}

Android files are:
1. MyApplication/jni/Android.mk

LOCAL_PATH := $(call my-dir)

LOCAL_MODULE := libhello
LOCAL_SRC_FILES := libhello.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := native-lib
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_SHARED_LIBRARIES := libhello
include $(BUILD_SHARED_LIBRARY)

2. MyApplication/jni/Application.mk

APP_STL := gnustl_static
APP_PLATFORM := android-24

3. MyApplication/jni/libhello.so
4. MyApplication/jni/get13.h - same as the one above
5. MyApplication/jni/native-lib.cpp

#include <jni.h>
#include <string>
#include <sstream>
extern "C"{
#include <get13.h>
JNIEXPORT jint JNICALL
Java_com_example_semko_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject asdf) {
    get13();
    return 1;
}
}

6. MyApplication/app/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    defaultConfig {
        applicationId "com.example.semko.myapplication"
        minSdkVersion 24
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    sourceSets.main {
        jni.srcDirs = []
        jniLibs.srcDir '../jni'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        ndkBuild {
            path '../jni/Android.mk'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:24.2.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

7. MyApplication/app/src/main/java/com.example.semko.myapplication/MainActivity.java

package com.example.semko.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(Integer.toString(stringFromJNI()));
    }

    public native int stringFromJNI();
}

The problem is that I get this error during gradle build in file native-lib.cpp:

Error:(10) undefined reference to `get13'
Error:error: linker command failed with exit code 1 (use -v to see invocation)

I compiled libhello using android toolchain generated with make_standalone_toolchain.py script which is included in Android NDK package.

Only the JNI function itself should be marked as extern "C" , like here:

#include <jni.h>
#include <string>
#include <sstream>
#include <get13.h>

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_semko_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject asdf) {
    get13();
    return 1;
}

If you use javah to prepare a MyApplication/jni/native-lib.h for you, it will have

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_semko_myapplication_MainActivity_stringFromJNI(
    JNIEnv* env,
    jobject asdf);

for you, and in the cpp file, you will

#include "native-lib.h"

and don't need extern "C" at all.

To make sure that your Android.mk is valid for all architectures, consider this change:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libhello
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libhello.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := native-lib
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_SHARED_LIBRARIES := libhello
include $(BUILD_SHARED_LIBRARY)

Update : the important piece is include $(CLEAR_VARS) before LOCAL_MODULE := libhello .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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