如何在添加库时合并两个Android.mk文件?

时间:2017-05-29 07:49:09

标签: android android-ndk

我是Android的新手,所以我不完全了解如何制作Android.mk文件。 我的项目运行正常,遵循提供Android.mk的在线教程。但是后来我需要添加一个缺少的库,并且教程中还有另一个Android.mk,现在我试图将两者合并在一起。

这是我的Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#opencv
OPENCVROOT:= C:\OpenCV-2.4.10-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES := com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp


LOCAL_LDLIBS += -llog

LOCAL_MODULE := MyOpencvLibs


include $(BUILD_SHARED_LIBRARY)

以下是我需要添加的Android.mk:

LOCAL_PATH  := $(call my-dir)
OPENCV_PATH := D:/CODE/OpenCV-2.4.10-android-sdk/sdk/native/jni

include $(CLEAR_VARS)
LOCAL_MODULE    := nonfree
LOCAL_SRC_FILES := libnonfree.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
OPENCV_INSTALL_MODULES := on
OPENCV_CAMERA_MODULES  := off
include $(OPENCV_PATH)/OpenCV.mk

LOCAL_C_INCLUDES :=             \
    $(LOCAL_PATH)               \
    $(OPENCV_PATH)/include

LOCAL_SRC_FILES :=              \
    nonfree_jni.cpp

LOCAL_MODULE := nonfree_demo
LOCAL_CFLAGS := -Werror -O3 -ffast-math
LOCAL_LDLIBS := -llog -ldl
LOCAL_SHARED_LIBRARIES += nonfree

include $(BUILD_SHARED_LIBRARY)

我对最后一个Android.mk感兴趣的是libnonfree.so,我不需要cpp文件。

以下是我项目的结构:

enter image description here

编辑:

感谢您的回答。我已经完成了建议并且我的Android.mk现在看起来像这样:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

#opencv
OPENCVROOT:= C:\OpenCV-2.4.10-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES := com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp
LOCAL_LDLIBS += -llog
LOCAL_SHARED_LIBRARIES += nonfree
LOCAL_MODULE := MyOpencvLibs
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := nonfree
LOCAL_SRC_FILES := libnonfree.so
include $(PREBUILT_SHARED_LIBRARY)

但问题仍然存在。我得到了#34;未定义的参考"方法libnonfree.so应该添加。我认为这个问题来自于Android.mk,但也许它来自其他地方。

我已将System.loadLibrary("nonfree");添加到MainACtivity

#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/nonfree/features2d.hpp"

我不知道自己做错了什么。

编辑2:

以下是错误:

Application.mk:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-16

我不知道在build.gradle中设置arguments="V=1"的位置

我的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.adrien.ndkopencvtest4"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    sourceSets.main {
        //jniLibs.srcDirs = ['src/main/jniLibs']
        jni.srcDirs = [] //disable automatic ndk-build call
    }
    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        commandLine "C:/Users/Adrien/AppData/Local/Android/sdk/ndk-bundle/ndk-build.cmd",
                'NDK_PROJECT_PATH=build/intermediates/ndk',
                'NDK_LIBS_OUT=src/main/jniLibs',
                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                'NDK_APPLICATION_MK=src/main/jni/Application.mk'
    }
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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.github.wseemann:FFmpegMediaMetadataRetriever-armeabi-v7a:1.0.14'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile project(':openCVLibrary2410')

}

编辑3:

" ndk-build V = 1" :

C:\Users\Adrien\AndroidStudioProjects\NDKOpencvTest4\app\src\main\jni>ndk-build V=1

[armeabi-v7a] SharedLibrary  : libMyOpencvLibs.so
C:/Users/Adrien/AppData/Local/Android/sdk/ndk-bundle/build//../toolchains/llvm/preb
uilt/windows-x86_64/bin/clang++.exe -Wl,-soname,libMyOpencvLibs.so -shared --sysroo
t=C:/Users/Adrien/AppData/Local/Android/sdk/ndk-bundle/build//../platforms/android-
16/arch-arm C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/obj/l
ocal/armeabi-v7a/objs/MyOpencvLibs/com_example_adrien_ndkopencvtest4_OpencvNativeCl
ass.o C:/Users/Adrien/AppData/Local/Android/sdk/ndk-bundle/build//../sources/cxx-st
l/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a -lgcc C:/Users/Adrien/Andro
idStudioProjects/NDKOpencvTest4/app/src/main/obj/local/armeabi-v7a/libopencv_java.s
o C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/obj/local/armea
bi-v7a/libnonfree.so  -gcc-toolchain C:/Users/Adrien/AppData/Local/Android/sdk/ndk-
bundle/build//../toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 -no-c
anonical-prefixes -target armv7-none-linux-androideabi -Wl,--fix-cortex-a8  -Wl,--b
uild-id -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-sh
ared-textrel -Wl,--fatal-warnings   -LC:/Users/Adrien/AppData/Local/Android/sdk/ndk
-bundle/build//../platforms/android-16/arch-arm/usr/lib -llog -lc -lm -o C:/Users/A
drien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/obj/local/armeabi-v7a/libMy
OpencvLibs.so
C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
drien_ndkopencvtest4_OpencvNativeClass.cpp:214: error: undefined reference to 'cv::
SURF::SURF(double, int, int, bool, bool)'
C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
drien_ndkopencvtest4_OpencvNativeClass.cpp:222: error: undefined reference to 'cv::
SURF::SURF()'
C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
drien_ndkopencvtest4_OpencvNativeClass.cpp:204: error: undefined reference to 'VTT
for cv::SURF'
C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
drien_ndkopencvtest4_OpencvNativeClass.cpp:204: error: undefined reference to 'VTT
for cv::SURF'
C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp:204: error: undefined
reference to 'VTT for cv::SURF'
C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp:449: error: undefined
reference to 'cv::SURF::SURF(double, int, int, bool, bool)'
C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp:457: error: undefined
reference to 'cv::SURF::SURF()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/obj/local/armeabi-v7a/libMyOpencvLibs.so] Error 1

解决方案:

我的libnonfree.so文件不正确,我下载了另一个。我还将Application.mk文件更改为:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-16

2 个答案:

答案 0 :(得分:1)

如果您想为项目添加预建的共享库,则不需要单独的 Android.mk

我们来看看你的剧本,

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#opencv
OPENCVROOT:= C:\OpenCV-2.4.10-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES := com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp


LOCAL_LDLIBS += -llog

LOCAL_MODULE := MyOpencvLibs


include $(BUILD_SHARED_LIBRARY)

并简单地在底部添加您需要的额外模块:

include $(CLEAR_VARS)
LOCAL_MODULE    := nonfree
LOCAL_SRC_FILES := libnonfree.so
include $(PREBUILT_SHARED_LIBRARY)

现在,文件C:\Users\Adrien\AndroidStudioProjects\NDKOpencvTest4\app\src\main\jni\libnonfree.so将打包到您的APK中。

如果您需要此库链接 libMyOpencvLibs.so (这意味着,如果 com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp 中的代码使用从 libnonfree.so导出的某些函数,你也应该在你的模块中提出它:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

#opencv
OPENCVROOT:= C:\OpenCV-2.4.10-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES := com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp
LOCAL_LDLIBS += -llog
LOCAL_SHARED_LIBRARIES += nonfree
LOCAL_MODULE := MyOpencvLibs
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := nonfree
LOCAL_SRC_FILES := libnonfree.so
include $(PREBUILT_SHARED_LIBRARY)

答案 1 :(得分:0)

将您的android.mk修改为:

LOCAL_SHARED_LIBRARIES += nonfree

这里的prebuild库,即libnonfree.so在android.mk中声明,稍后通过

引用它
System.loadLibrary("MyOpencvLibs");

现在要使用它,您需要在MainActivity中加载最新构建的库,即

package main

/*
enum levels {
    low1,
    medium,
    high
};
#define CMA 30
typedef enum {
    LOW = 0,
    MEDIUM = 1,
    HIGH = 2
} security;

*/
import "C"
import "fmt"

func main() {

    cc := new(C.enum_levels)
    //cm := new(C._*someliteral*_CMA)
    fmt.Println(*cc)

    fmt.Println(C.MEDIUM)

}

多数民众赞成,现在你已将android.mk文件合并到一个名为MyOpencvLibs的新模块中

为了更好地理解,您可以阅读我的博客:https://androidtutsweb.wordpress.com/2017/04/04/android-mk-for-ndk-build-using-pre-build-libraries/