From 27bce8924f5819719e7b9fd3096969295b2d5bd6 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Sun, 27 Nov 2011 08:17:49 +0000 Subject: [PATCH] Adds a test project for building JNI Pascal Android apps git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2164 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/jnitest/AndroidManifest.xml | 21 +++++ .../examples/jnitest/build.properties | 17 ++++ .../android-ndk/examples/jnitest/build.xml | 85 ++++++++++++++++++ .../examples/jnitest/default.properties | 11 +++ .../examples/jnitest/jni/nativetest.pas | 70 +++++++++++++++ .../examples/jnitest/local.properties | 10 +++ .../examples/jnitest/res/drawable/icon.png | Bin 0 -> 3180 bytes .../examples/jnitest/res/values/strings.xml | 5 ++ .../com/pascal/jnitest/AndroidJNITest.java | 34 +++++++ 9 files changed, 253 insertions(+) create mode 100644 bindings/android-ndk/examples/jnitest/AndroidManifest.xml create mode 100644 bindings/android-ndk/examples/jnitest/build.properties create mode 100644 bindings/android-ndk/examples/jnitest/build.xml create mode 100644 bindings/android-ndk/examples/jnitest/default.properties create mode 100755 bindings/android-ndk/examples/jnitest/jni/nativetest.pas create mode 100644 bindings/android-ndk/examples/jnitest/local.properties create mode 100755 bindings/android-ndk/examples/jnitest/res/drawable/icon.png create mode 100755 bindings/android-ndk/examples/jnitest/res/values/strings.xml create mode 100755 bindings/android-ndk/examples/jnitest/src/com/pascal/jnitest/AndroidJNITest.java 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 0000000000000000000000000000000000000000..75024841d327c4fbaefef7c8e9c8d0e892895f93 GIT binary patch literal 3180 zcmV-y43qPTP)-8%y z>?Lk&?1bQy00olTw5rOVwrM3QR77j2sF13vY1JS6sbZvxR;?=9{z8-xlkf-)&`7j@ zLPDIDmN(g8j2*{G>^OcucGvIhdUtxhGxyHUj@Mv*5J2jUj_=Ifnc4H5^PR_?jbWN5 zeUXQUzTo5k06yzs=!5%z{F9Jj7?1K{fTfRP;}p;BPws!b?{g2}k>CBkB{CIxbbfw5 zu&}U@aRCDXfOeKMEa3xB{pG?UJCJiQ7-_%)Z`S)Bu!3D(!%^gw)?^i4ZzND z5(~p)Pqpn2e^vmRc|IAcuA-ZF-YARX;}bMCF~RdV4Gs+pnCaHjJMX;HJ`Mx|A^?UlH$P8vbMxY*I4ckEIFH6+3?ya&Vy|ILPE5)y>q`lz z(_B8xIJ@terw_J$tOrz}(uKZ0DladihaP%}Mn*=ckIx$$8|ksf9uwu9W8J!S0tPdr zzOI(?^78D@alHj9W`m2z6ZWy?t(aVk<50es@0~tF16|#;?eG!uFD%mZ z#`P4b*-V3VTj<2GSHx4#J^Qq=S`UPC{Ql=jJHF3PH~H^w2ExJU@Re6|BvdxuT*%xZB*^{E4!MslIov^*2=VdbYw{>%ralpXsBTJONzHA^PWkyFn>KDF+S*{>I65{i;Ks(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2dLArwa*&9&?H#IdSfj`d{#M=m=M3$6ED|o=?_gVL()2I&K zNfp3L86K#u-Lmh>mAKFW*1TrZMhV_D2MzUCJ<`y(TL+*9|D|Kcj&Im=;HHuUy?8C9gBMDVEB?ow6%MZ!L*^=eDCq6&<>2HE zf8{!A+|ejSi4655MzeYIwX~kA@F0T#{|mW54GoV_c||2t!Y`Y6c-JsI>9-ONt2@s@ zpOo6vD21W;mxf+R}d66yeI1D=%ho34pHmAR_f$)J2sRxo0^+sO}~}! zc)T>sAkJ|S93CE)*OAFoaz(}UggOwAf+hf<^oXL*q8osZ3s#uZ<1g5lO?>&DY}Uty z8?T$f6sh{Qo+F(6pKPhyfXz5I}jZp zkfFeRD`-t>KlN|wJ=ZG<0zH&z2SM86rfQ@0)*Z^HESXimU5sY4zPk>{*jitIlmi?4 zN81}nYdd$|NJB%zszWfOT3dl`*a2#|f*aQj4GnQ0)hjQ*Iy<`=T#0J4NoooDEoT}< z%hG>o0QU}#jgS45moBN+A-VF4E!W^)=wS}XJ#Z|>z-Tb-<}t@CpJ4{|n&R9&;_9`)FSlGI1MWHTHIQEy(%kAJo3 z{PFAkju2a;C2Ji>x$&@3+ zzhQQE_WQ}DymRL-R}*|!4ZueY2C;_~Z{(+hmlBEgtql!Py2|5!0t^7z)gsDo S`hb-H0000 + + 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