如何将visual c ++库导入android studio项目?

时间:2017-07-26 14:16:05

标签: android c++ android-studio java-native-interface

我在Visual Studio 2017中为iOS,UWP和Android创建了一个共享的可视化c ++跨平台移动库。我成功地能够为C#UWP中的库创建一个Windows RT Component包装器。我删除了iOS库,因为我不需要它。剩下的就是Android项目。目前我正在努力如何编写共享库的包装器并将其导入android studio。我查看了MSDN上提供的文档,但它更深入地介绍了创建跨平台c ++应用程序,而没有详细介绍如何利用共享静态库。

RandString.java:

package com.myapplication;

/**
 * Created by yorel56 on 7/26/2017.
 */

public class RandString {
    static{
        System.loadLibrary("libRandString");
    }
    public native String GetString();
    public native String GetString(int index);
}

.so files produces by VS2017 project

build.gradle(module):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.myapplication"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    sourceSets.main {
        jni.srcDirs = [] //disable automatic ndk-build call
        jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of jniLibs
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn(nativeLibsToJar)
    }

    task ndkBuild(type: Exec, description: 'compile native code') {
        def ndkDir = "C:\\Users\\yorel56\\AppData\\Local\\Android\\sdk\\ndk-bundle"
        workingDir "src/main/jni"
        commandLine "$ndkDir/ndk-build"
    }

    task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
        destinationDir file("$buildDir/native-libs")
        baseName 'native-libs'
        extension 'jar'
        from fileTree(dir: 'libs', include: '**/*.so')
        into 'lib/'
    }
    nativeLibsToJar.dependsOn {
        ndkBuild  // comment that, when you don't want to rerun ndk-build script
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.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:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

我目前得到的错误是“原生”'这里不允许,当我选择让Android Studio通过在native_lib.cpp中创建一个函数来修复它时,我继续得到错误。我尝试按照文档here

1 个答案:

答案 0 :(得分:1)

您可以使用Visual Studio构建的libRandString.so;您不需要externalNativeBuild阻止而不需要ndkBuild任务,而不需要 build.gradle 中的nativeLibsToJar任务。 Android Studio将从 jniLibs 目录中获取预建文件并将其打包到APK中。