title: How to import visual c++ library into android studio project?
body: <p>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.</p>
  
  <p>RandString.java:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/IAlvb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IAlvb.png" alt=".so files produces by VS2017 project"></a></p>
  
  <p>build.gradle (module):</p>
  
  <pre><code>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 -&gt; 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'
  }
  </code></pre>
  
  <p>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 <a href="https://blogs.msdn.microsoft.com/vcblog/2017/04/18/android-and-ios-development-with-c-in-visual-studio/" rel="nofollow noreferrer">here</a>.</p>