Categories: android, c++, c, android-ndk

Problem in android studio prebuilt library

1 answer

I am using a prebuilt c++ library with android studio which is not being picked up by studio. I built this so file using ndk-build in same version of OS and studio but on different machine. Copied so files on new machine then Studio is generating undefined reference errors for new c++ functions in lib. Checking with nm command, symbol is present in so file. It is picking old lib from somewhere and old functions are producing no such errors. Tried:

  1. Invalidating cache and restart.
  2. Build clean project.
  3. ndk-build again using only this so file (no cpp)

But no success.

I put .so file in myproject/app/libs/x86/libalgos.so and also in myproject/app/jni/libs/x86/libalgos.so

cmakelists:

. . . add_library(algos SHARED IMPORTED)     set_target_properties(algos PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/x86/libalgos.so)     target_include_directories(native-lib PRIVATE ${CMAKE_SOURCE_DIR}/ )     target_link_libraries( # Specifies the target library.             native-lib      # Links the target library to the log library     # included in the NDK.             algos ${log-lib}) 

Android.mk [Location: myproject/app/jni]

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE    := algos LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libalgos.so LOCAL_EXPORT_C_INCLUDES := include include $(PREBUILT_SHARED_LIBRARY) 

Applicattion.mk[Location: myproject/app/jni]

APP_CFLAGS += -Wno-error=format-security APP_ABI := x86 APP_MODULES := algos APP_OPTIM := debug 

build.gradle

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions'  android {     compileSdkVersion 29     buildToolsVersion "29.0.3"     defaultConfig {         applicationId "com.example.cryptotrading"         //minSdkVersion 15         minSdkVersion 26 //---changed for rhino implementation (invoke customs)         targetSdkVersion 29         versionCode 1         versionName "1.0"         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"         externalNativeBuild {             cmake {                 cppFlags ""             }         }          ndk {             abiFilters 'x86'         }         javaCompileOptions {             annotationProcessorOptions {                 arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]             }         }      }     compileOptions {         sourceCompatibility JavaVersion.VERSION_1_8         targetCompatibility JavaVersion.VERSION_1_8     }      buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'         }     }     externalNativeBuild {         cmake {             path "src/main/cpp/CMakeLists.txt"             version "3.10.2"         }     }      sourceSets {         main {             jniLibs.srcDirs = ['jni/', 'libs/', 'app/libs/', '${CMAKE_SOURCE_DIR}/../../..//libs/']         }     } }  dependencies {     implementation fileTree(dir: 'libs', include: ['*.jar'])     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"     implementation 'androidx.appcompat:appcompat:1.0.2'     implementation 'androidx.core:core-ktx:1.0.2'     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'     implementation 'androidx.preference:preference:1.1.0-alpha05'     testImplementation 'junit:junit:4.12'     androidTestImplementation 'androidx.test.ext:junit:1.1.0'     androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'     implementation 'com.google.android.material:material:1.0.0'     //implementation files('./libs/js.jar')     implementation 'org.mozilla:rhino:1.7.12'     //implementation 'org.apache.commons:commons-text:1.7'     //compile group: 'commons-codec', name: 'commons-codec', version: '1.7'     //compile 'com.squareup.okhttp3:okhttp:3.4.1'     implementation("com.squareup.okhttp3:okhttp:4.4.0")     implementation 'com.googlecode.json-simple:json-simple:1.1'     //compile 'com.alibaba.fastjson:1.2.47'     def room_version = "2.2.4"     implementation "androidx.room:room-runtime:$room_version"     annotationProcessor "androidx.room:room-compiler:$room_version"     // optional - Kotlin Extensions and Coroutines support for Room     implementation "androidx.room:room-ktx:$room_version"     // optional - RxJava support for Room     implementation "androidx.room:room-rxjava2:$room_version"     implementation 'androidx.annotation:annotation:1.1.0'     compile "com.androidplot:androidplot-core:1.5.7" } 
Received answer to this question:
The best answer according to the author of the question:

This issue was due to presence of old so files in different folders of project. Solved by:

  1. Run find command to get all locations of so file. Delete them.
  2. Regenerate using ndk-build.