使用Android Studio 2.3.3和CMake

时间:2017-07-07 02:55:41

标签: android cmake add executable

我正在通过CMake通过稳定的gradle(http://tools.android.com/tech-docs/external-c-builds)测试新的Android Studio C / C ++构建。

在我的应用中,已经植根的设备需要使用我在Android Studio中编译的依赖于ABI的二进制文件。

当我尝试使用

编译标准可执行文件时
add_executable(mybinary src/main/cpp/mybinary.cpp)

在构建文件夹中生成二进制文件:

app/.externalNativeBuild/cmake/debug/armeabi/mybinary

但它们似乎不是可执行的二进制文件

file  app/.externalNativeBuild/cmake/debug/armeabi/mybinary
mybinary: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked

处理这种需求的正确方法是什么?草地任务是可行的吗?

的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.zhy.myapplication"
        minSdkVersion 22
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    defaultConfig {
        externalNativeBuild {
            cmake {
                targets "mybinary"
                abiFilters 'armeabi'
            }
        }
    }
}

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.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

的CMakeLists.txt

    # For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )


add_executable(mybinary src/main/cpp/mybinary.cpp)
target_link_libraries( mybinary native-lib)
target_include_directories (mybinary PUBLIC src/main/cpp/mybinary.h})

我遵循了这个主题,但不正确: Compile and use ABI-dependent executable binaries in Android with Android Studio 2.2 and CMake

0 个答案:

没有答案