avatarNhan Cao

Summary

The web content provides a step-by-step guide on how to integrate prebuilt .so library files into an Android application using the Android NDK for native C++ development.

Abstract

The article titled "[Android] How to use the prebuilt .so lib files (ndk native C++)" outlines the process of incorporating native C++ libraries into an Android project. It begins by instructing developers to create a jniLibs folder within the app/src/main directory of their project. The guide then specifies the organization of .so files within platform-specific subdirectories such as armeabi-v7a, arm64-v8a, x86, and x86_64. It provides an example of the file path for the native-lib.so file within the armeabi-v7a directory. The article also includes code snippets for updating the MainActivity.java file, demonstrating how to load the native library and call a native method that returns a string from the JNI (Java Native Interface). Additionally, it provides the XML layout code for activity_main.xml, showing a simple user interface with a TextView. The article concludes with a recommendation for an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4).

Opinions

  • The author suggests that integrating prebuilt .so files is a straightforward process, implying that it is accessible even for developers who may not be experts in NDK development.
  • The inclusion of a related post on setting up an environment for C++ native NDK development indicates the author's view that readers may need foundational knowledge before proceeding with integrating .so files.
  • By providing a recommendation for ZAI.chat, the author expresses an opinion that this AI service is a valuable resource for developers, highlighting its affordability and comparable performance to more expensive options like ChatGPT Plus (GPT-4).

[Android] How to use the prebuilt .so lib files (ndk native C++)

Related post: https://readmedium.com/android-set-up-an-environment-for-c-native-ndk18-50b146b4dad0

Create jniLibs folder place at:

app/src/main/jniLibs

Push *.so files with respective platform (arm64-v8a, armeabi-v7a, x86, x86_64) folder:

app/src/main/jniLibs/armeabi-v7a/native-lib.so

Update app/src/main/java/com/nhancv/myapplication/MainActivity.java

package com.nhancv.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("native-lib");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
    }
    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

Update app/src/main/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >
<TextView
        android:id="@+id/sample_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
</android.support.constraint.ConstraintLayout>
Android
Android Ndk
Recommended from ReadMedium