如何将Visual C ++库导入Android Studio项目? [英] How to import visual c++ library into android studio project?

查看:157
本文介绍了如何将Visual C ++库导入Android Studio项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a shared visual c++ cross platform mobile library in visual studio 2017 for iOS, UWP, and Android. I was successful able to create a Windows RT Component wrapper for the library to use in a C# UWP. I removed the iOS library as I did not need it. All that remain is the Android project. Currently I am struggling how to write a wrapper for the shared library and import it into android studio. I looked the documentation provided on MSDN, but it goes more in depth on creating a cross platform c++ application and not enough detail on how to leverage the shared static libs.

RandString.java:

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);
}

build.gradle(模块):

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中创建一个函数来修复它时,我继续遇到错误.我尝试按照文档此处.

The error i am getting currently is that 'native' is not allowed here, and when i choose to let Android Studio fix it by creating a function in native_lib.cpp, I continue to get errors. I tried following the documentation here.

推荐答案

您可以使用Visual Studio内置的libRandString.so;您不需要 build.gradle 中的externalNativeBuild块,而无需ndkBuild任务,也不需要nativeLibsToJar任务. Android Studio将从 jniLibs 目录中提取预构建的文件,然后将其打包到APK中.

You can use the libRandString.so which was built with Visual Studio; you don't need the externalNativeBuild block and not ndkBuild task and not the nativeLibsToJar task in your build.gradle. Android Studio will pick up the prebuilt files from jniLibs directory and pack them into the APK for you.

这篇关于如何将Visual C ++库导入Android Studio项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆