如何将视觉C ++库导入Android Studio项目?

 2023-01-21    266  

问题描述

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

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

.因此,文件由VS2017项目产生

build.gradle(模块):

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通过在antibal_lib.cpp中创建函数来修复它时,我将继续遇到错误.我尝试关注文档在这里.

推荐答案

您可以使用由Visual Studio构建的libRandString.so;您不需要externalNativeBuild块,而不是ndkBuild任务,也不需要nativeLibsToJar nativeLibsToJar build.gradle 中的任务. Android Studio将从 jnilibs 目录中获取预构建的文件,并为您打包APK.

以上所述是小编给大家介绍的如何将视觉C ++库导入Android Studio项目?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/26302.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。