First working android example app using jni.
authorDaniel G <giri@nwrk.biz>
Sat, 26 Jan 2019 01:01:14 +0000 (02:01 +0100)
committerDaniel G <giri@nwrk.biz>
Sat, 26 Jan 2019 01:01:14 +0000 (02:01 +0100)
_template/src/Makefile
_template/src/android-src/java/MainActivity.java
_template/src/android-src/lib/armeabi-v7a/libtest.so [new file with mode: 0755]
_template/src/android-src/res/layout/activity_main.xml
_template/src/android-src/src/build.sh [new file with mode: 0755]
_template/src/android-src/src/test.c [new file with mode: 0644]

index bb661d0..cdca381 100644 (file)
@@ -76,6 +76,7 @@ ANDROID_KEYSTORE_PATH=~/.android/debug.keystore
 ANDROID_KEYSTORE_NAME=androiddebugkey
 ANDROID_KEYSTORE_PW=android
 ANDROID_CP=$(ANDROID_SDK_ROOT)/platforms/$(ANDROID_PLATORM)/android.jar
+ADB=$(ANDROID_SDK_ROOT)/platform-tools/adb
 AAPT=$(ANDROID_SDK_ROOT)/build-tools/$(ANDROID_BUILD_TOOLS_VERSION)/aapt
 DX=$(ANDROID_SDK_ROOT)/build-tools/$(ANDROID_BUILD_TOOLS_VERSION)/dx
 ZIPALIGN=$(ANDROID_SDK_ROOT)/build-tools/$(ANDROID_BUILD_TOOLS_VERSION)/zipalign
@@ -340,11 +341,17 @@ out_apk:
        $(DX) --dex --output=$(GENDIR_ANDROID)/classes.dex $(CLASSDIR_ANDROID)
        $(AAPT) package -f -M $(ANDROID_SRC)/AndroidManifest.xml -S $(ANDROID_SRC)/res -I $(ANDROID_CP) -F $(GENDIR_ANDROID)/$(PROJNAME).apk.unaligned
        cd $(GENDIR_ANDROID) && $(AAPT) add $(GENDIR_ANDROID)/$(PROJNAME).apk.unaligned classes.dex
+       #cd $(ANDROID_SRC) && $(AAPT) add $(GENDIR_ANDROID)/$(PROJNAME).apk.unaligned lib/arm64-v8a/libtest.so
+       #cd $(ANDROID_SRC) && $(AAPT) add $(GENDIR_ANDROID)/$(PROJNAME).apk.unaligned lib/armeabi/libtest.so
+       cd $(ANDROID_SRC) && $(AAPT) add $(GENDIR_ANDROID)/$(PROJNAME).apk.unaligned lib/armeabi-v7a/libtest.so
 
 clean_android_apk:
        rm -rf $(OUT_ANDROID)
        rm -rf $(GENDIR_ANDROID)
        rm -rf $(CLASSDIR_ANDROID)
 
+apk_install_adb:
+       $(ADB) get-state 1>/dev/null 2>&1 && $(ADB) install -r $(OUT_ANDROID)/$(PROJNAME).apk
+
 .PHONY: before_debug_linux out_debug_linux after_debug_linux before_release_linux out_release_linux after_release_linux before_debug_windows out_debug_windows after_debug_windows
 .PHONY: before_debug_arm out_debug_arm after_debug_arm before_release_arm out_release_arm after_release_arm before_release_windows out_release_windows after_release_windows
index bbe0c24..366135e 100644 (file)
@@ -2,11 +2,24 @@ package com.giri.template;
 
 import android.app.Activity;
 import android.os.Bundle;
+import android.widget.TextView;
+
 
 public class MainActivity extends Activity {
+  
   @Override
-  protected void onCreate(Bundle savedInstanceState) {
-    super.onCreate(savedInstanceState);
-    setContentView(R.layout.activity_main);
-  }
+    protected void onCreate(Bundle savedInstanceState) {
+       super.onCreate(savedInstanceState);
+       setContentView(R.layout.activity_main);
+        TextView tv = (TextView)findViewById(R.id.hello_textview);
+        tv.setText(stringFromJNI() );
+        //tv.setText("LOLOLOLO");
+    }
+
+
+    public native String  stringFromJNI();
+
+    static {
+        System.loadLibrary("test");
+    }
 }
\ No newline at end of file
diff --git a/_template/src/android-src/lib/armeabi-v7a/libtest.so b/_template/src/android-src/lib/armeabi-v7a/libtest.so
new file mode 100755 (executable)
index 0000000..d9401d1
Binary files /dev/null and b/_template/src/android-src/lib/armeabi-v7a/libtest.so differ
index 2c5482a..c1760db 100644 (file)
@@ -1,6 +1,7 @@
 <TextView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/hello_textview"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:text="@string/hello_world"
diff --git a/_template/src/android-src/src/build.sh b/_template/src/android-src/src/build.sh
new file mode 100755 (executable)
index 0000000..a07c7e3
--- /dev/null
@@ -0,0 +1,2 @@
+/usr/lib/android-ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc --sysroot=/usr/lib/android-ndk/platforms/android-24/arch-arm -o test.o -c -fPIC ./test.c
+/usr/lib/android-ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc --sysroot=/usr/lib/android-ndk/platforms/android-24/arch-arm -shared ./test.o -o libtest.so
diff --git a/_template/src/android-src/src/test.c b/_template/src/android-src/src/test.c
new file mode 100644 (file)
index 0000000..cbc4a89
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#include <string.h>
+#include <jni.h>
+
+/* This is a trivial JNI example where we use a native method
+ * to return a new VM String. See the corresponding Java source
+ * file located at:
+ *
+ *   hello-jni/app/src/main/java/com/example/hellojni/HelloJni.java
+ */
+JNIEXPORT jstring JNICALL
+Java_com_giri_template_MainActivity_stringFromJNI( JNIEnv* env,
+                                                  jobject thiz )
+{
+#if defined(__arm__)
+    #if defined(__ARM_ARCH_7A__)
+    #if defined(__ARM_NEON__)
+      #if defined(__ARM_PCS_VFP)
+        #define ABI "armeabi-v7a/NEON (hard-float)"
+      #else
+        #define ABI "armeabi-v7a/NEON"
+      #endif
+    #else
+      #if defined(__ARM_PCS_VFP)
+        #define ABI "armeabi-v7a (hard-float)"
+      #else
+        #define ABI "armeabi-v7a"
+      #endif
+    #endif
+  #else
+   #define ABI "armeabi"
+  #endif
+#elif defined(__i386__)
+#define ABI "x86"
+#elif defined(__x86_64__)
+#define ABI "x86_64"
+#elif defined(__mips64)  /* mips64el-* toolchain defines __mips__ too */
+#define ABI "mips64"
+#elif defined(__mips__)
+#define ABI "mips"
+#elif defined(__aarch64__)
+#define ABI "arm64-v8a"
+#else
+#define ABI "unknown"
+#endif
+
+    return (*env)->NewStringUTF(env, "Hello from JNI !  Compiled with ABI " ABI ".");
+}