This is a simple "Hello, World" Android app that demonstrates how to use JNI (Java Native Interface) to call native code from an Android app.
To build and run this app, you will need the following:
- Android Studio
- Android SDK
- JDK (Java Development Kit)
Building and running the app
- Clone the repository:
git clone https://github.com/Kreijstal/android-hello.git
- Open the project in Android Studio:
cd android-hello
open -a Android\ Studio .
- Build and run the app:
*In Android Studio, click the "Run" button (the green arrow icon) to build and run the app. *Alternatively, you can build the app from the command line using Gradle:
./gradlew installDebug
or
./gradlew assembleDebug
The app consists of a single activity (MainActivity) that displays a "Hello, World!" message on the screen. The message is generated by a native function (hello()) written in C.
The native function is called from the Java code using JNI. The Java code loads the native library (libkreijstaltest.so) and calls the hello() function using the System.loadLibrary() method and the native keyword.
For example:
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("kreijstaltest");
}
public native String hello();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
textView.setText(hello());
}
}
The native code for the hello() function is located in the jni directory. To build the native code, the app uses the CMakeLists.txt file and the Android.mk file, which are located in the jni directory.
For example:
#include <jni.h>
#include <string.h>
jstring
Java_work_encasa_pendejo_helloworld_MainActivity_hello(JNIEnv* env, jobject thiz)
{
return (*env)->NewStringUTF(env, "Hello, World!");
}
Resources