Skip to content

Commit

Permalink
Fix "could not load library libc++_shared.so" error
Browse files Browse the repository at this point in the history
  • Loading branch information
kujyp committed Nov 8, 2019
1 parent b7b6703 commit 479c159
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_STL=c++_static'
cppFlags "-std=c++11"
}
}
ndk {
abiFilters 'x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a'
}
}
buildTypes {
release {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--allow-multiple-definition -Wl,--whole-archive")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} \
-Wl,--allow-multiple-definition \
-Wl,--whole-archive")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_samplenative3_MainActivity_stringFromJNI(
Java_com_example_samplenative3_MainActivity_internalStringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/com/example/samplenative3/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private static boolean sNativeLibraryLoaded = false;

// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
try {
System.loadLibrary("native-lib");
sNativeLibraryLoaded = true;
} catch (UnsatisfiedLinkError e) {
sNativeLibraryLoaded = false;
}
}

@Override
Expand All @@ -22,9 +28,16 @@ protected void onCreate(Bundle savedInstanceState) {
tv.setText(stringFromJNI());
}

private String stringFromJNI() {
if (!sNativeLibraryLoaded) {
return "error";
}
return internalStringFromJNI();
}

/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String internalStringFromJNI();
}

0 comments on commit 479c159

Please sign in to comment.