diff --git a/bindings/android-ndk/examples/jnitest/AndroidManifest.xml b/bindings/android-ndk/examples/jnitest/AndroidManifest.xml new file mode 100644 index 000000000..50e597cba --- /dev/null +++ b/bindings/android-ndk/examples/jnitest/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + diff --git a/bindings/android-ndk/examples/jnitest/build.properties b/bindings/android-ndk/examples/jnitest/build.properties new file mode 100644 index 000000000..edc7f2305 --- /dev/null +++ b/bindings/android-ndk/examples/jnitest/build.properties @@ -0,0 +1,17 @@ +# This file is used to override default values used by the Ant build system. +# +# This file must be checked in Version Control Systems, as it is +# integral to the build system of your project. + +# This file is only used by the Ant script. + +# You can use this to override default values such as +# 'source.dir' for the location of your java source folder and +# 'out.dir' for the location of your output folder. + +# You can also use it define how the release builds are signed by declaring +# the following properties: +# 'key.store' for the location of your keystore and +# 'key.alias' for the name of the key to use. +# The password will be asked during the build when you use the 'release' target. + diff --git a/bindings/android-ndk/examples/jnitest/build.xml b/bindings/android-ndk/examples/jnitest/build.xml new file mode 100644 index 000000000..b3f5ea6e0 --- /dev/null +++ b/bindings/android-ndk/examples/jnitest/build.xml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/bindings/android-ndk/examples/jnitest/default.properties b/bindings/android-ndk/examples/jnitest/default.properties new file mode 100644 index 000000000..9d79b12c7 --- /dev/null +++ b/bindings/android-ndk/examples/jnitest/default.properties @@ -0,0 +1,11 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system use, +# "build.properties", and override values to adapt the script to your +# project structure. + +# Project target. +target=android-4 diff --git a/bindings/android-ndk/examples/jnitest/jni/nativetest.pas b/bindings/android-ndk/examples/jnitest/jni/nativetest.pas new file mode 100755 index 000000000..31d9d6cfc --- /dev/null +++ b/bindings/android-ndk/examples/jnitest/jni/nativetest.pas @@ -0,0 +1,70 @@ +library nativetest; +{$ifdef fpc} + {$mode delphi} +{$endif} + +uses + SysUtils, + jni in 'jni.pas', + log in 'log.pas'; + +const curClass:JClass=nil; + nativeCodeLoaded:JfieldID=nil; + +function Java_com_bero_nativetest_Main_stringFromJNI(env:PJNIEnv;this:jobject):jstring;{$ifdef mswindows}stdcall;{$else}cdecl;{$endif} +var x:single; +begin + __android_log_write(ANDROID_LOG_INFO,'nativetest','Java_com_bero_nativetest_Main_stringFromJNI entered'); + curEnv^.SetLongField(curEnv,curClass,nativeCodeLoaded,1); + x:=8; + result:=env^.NewStringUTF(env,pchar('Hello from native free pascal code by BeRo to the java world on the android platform ! '+floattostr(x*0.5))); + __android_log_write(ANDROID_LOG_INFO,'nativetest','Java_com_bero_nativetest_Main_stringFromJNI exited'); +end; + +const NativeMethods:array[0..0] of JNINativeMethod= + ((name:'stringFromJNI'; + signature:'()Ljava/lang/String;'; + fnPtr:@Java_com_bero_nativetest_Main_stringFromJNI;)); + +function JNI_OnLoad(vm:PJavaVM;reserved:pointer):jint;{$ifdef mswindows}stdcall;{$else}cdecl;{$endif} +begin + curVM:=vm; + __android_log_write(ANDROID_LOG_INFO,'nativetest','JNI_OnLoad called'); + if curVM^.GetEnv(curVM,@curEnv,JNI_VERSION_1_6)<>JNI_OK then begin + __android_log_write(ANDROID_LOG_FATAL,'nativetest','curVM^.GetEnv failed'); + result:=JNI_ERR; + exit; + end; + + curClass:=curEnv^.FindClass(curEnv,'com/bero/nativetest/Main'); + if not assigned(curClass) then begin + __android_log_write(ANDROID_LOG_FATAL,'nativetest','curEnv^.FindClass failed'); + result:=JNI_ERR; + exit; + end; + if curEnv^.RegisterNatives(curEnv,curClass,@NativeMethods[0],length(NativeMethods))<0 then begin + __android_log_write(ANDROID_LOG_FATAL,'nativetest','curEnv^.RegisterNatives failed'); + result:=JNI_ERR; + exit; + end; + + nativeCodeLoaded:=curEnv^.GetFieldID(curEnv,curClass,'nativeCodeLoaded','J'); + if not assigned(nativeCodeLoaded) then begin + __android_log_write(ANDROID_LOG_FATAL,'nativetest','curEnv^.GetFieldID failed'); + result:=JNI_ERR; + exit; + end; + + result:=JNI_VERSION_1_6; +end; + +procedure JNI_OnUnload(vm:PJavaVM;reserved:pointer);{$ifdef mswindows}stdcall;{$else}cdecl;{$endif} +begin +end; + +exports JNI_OnLoad name 'JNI_OnLoad', + JNI_OnUnload name 'JNI_OnUnload', + Java_com_bero_nativetest_Main_stringFromJNI name 'Java_com_bero_nativetest_Main_stringFromJNI'; + +begin +end. diff --git a/bindings/android-ndk/examples/jnitest/local.properties b/bindings/android-ndk/examples/jnitest/local.properties new file mode 100644 index 000000000..6a8ab1aa4 --- /dev/null +++ b/bindings/android-ndk/examples/jnitest/local.properties @@ -0,0 +1,10 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must *NOT* be checked in Version Control Systems, +# as it contains information specific to your local configuration. + +# location of the SDK. This is only used by Ant +# For customization when using a Version Control System, please read the +# header note. +sdk.dir=/home/felipe/Programas/android-sdk-linux diff --git a/bindings/android-ndk/examples/jnitest/res/drawable/icon.png b/bindings/android-ndk/examples/jnitest/res/drawable/icon.png new file mode 100755 index 000000000..75024841d Binary files /dev/null and b/bindings/android-ndk/examples/jnitest/res/drawable/icon.png differ diff --git a/bindings/android-ndk/examples/jnitest/res/values/strings.xml b/bindings/android-ndk/examples/jnitest/res/values/strings.xml new file mode 100755 index 000000000..dd202524f --- /dev/null +++ b/bindings/android-ndk/examples/jnitest/res/values/strings.xml @@ -0,0 +1,5 @@ + + + Hello World, Main! + NativeTest + diff --git a/bindings/android-ndk/examples/jnitest/src/com/pascal/jnitest/AndroidJNITest.java b/bindings/android-ndk/examples/jnitest/src/com/pascal/jnitest/AndroidJNITest.java new file mode 100755 index 000000000..aea7f1941 --- /dev/null +++ b/bindings/android-ndk/examples/jnitest/src/com/pascal/jnitest/AndroidJNITest.java @@ -0,0 +1,34 @@ +package com.pascal.jnitest; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.TextView; +import android.util.Log; + +public class AndroidJNITest extends Activity { + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + + TextView tv = new TextView(this); + tv.setText( stringFromJNI() ); + setContentView(tv); + } + + public native String stringFromJNI(); + + public long nativeCodeLoaded=0; + + static { + try { + Log.i("JNI", "Trying to load libnativetest.so"); + System.loadLibrary("nativetest"); + } catch(UnsatisfiedLinkError ule) { + Log.e("JNI", "WARNING: Could not load libnativetest.so"); + ule.printStackTrace(); + } + + } +} \ No newline at end of file