Skip to content

Commit 75d1bc8

Browse files
Merge pull request amanjeetsingh150#9 from pramonow/sample_jni
Added sample for jni kotlin
2 parents c6a8356 + 7b8036c commit 75d1bc8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+909
-0
lines changed

JniSample/.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches/build_file_checksums.ser
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
.DS_Store
9+
/build
10+
/captures
11+
.externalNativeBuild

JniSample/.idea/codeStyles/Project.xml

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JniSample/.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JniSample/.idea/gradle.xml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JniSample/.idea/misc.xml

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JniSample/.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JniSample/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JniSample/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Jni android sample
2+
3+
JNI stands for Java Native Interface. This sample is showing how to call native c/c++ method to be used inside android.
4+
In order to run this sample, you need to install <b>NDK</b> in android SDK manager beforehand.
5+
6+
If you looked through, the project contains c/c++ file which contains:
7+
8+
#include <jni.h>
9+
#include "sample-jni.h"
10+
11+
12+
extern "C"
13+
JNIEXPORT jint JNICALL
14+
15+
Java_com_pramonow_androidadvanced_JniActivity_getKey(JNIEnv* pEnv, jobject instance, jint key1) {
16+
return key1+200;
17+
}
18+
19+
And the getKey function will be able to be called inside the activity:
20+
21+
//Our Jni method
22+
external fun getKey(key:Int):Int
23+
24+
companion object {
25+
init {
26+
System.loadLibrary("sample-jni")
27+
}
28+
}
29+
30+
override fun onCreate(savedInstanceState: Bundle?) {
31+
super.onCreate(savedInstanceState)
32+
setContentView(R.layout.activity_jni)
33+
34+
Toast.makeText(this,getKey(55).toString(),Toast.LENGTH_SHORT).show() // will show 255
35+
}

JniSample/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

JniSample/app/CMakeLists.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Sets the minimum version of CMake required to build your native library.
2+
# This ensures that a certain set of CMake features is available to
3+
# your build.
4+
5+
cmake_minimum_required(VERSION 3.10.2)
6+
7+
# Specifies a library name, specifies whether the library is STATIC or
8+
# SHARED, and provides relative paths to the source code. You can
9+
# define multiple libraries by adding multiple add_library() commands,
10+
# and CMake builds them for you. When you build your app, Gradle
11+
# automatically packages shared libraries with your APK.
12+
13+
add_library( # Specifies the name of the library.
14+
sample-jni
15+
16+
# Sets the library as a shared library.
17+
SHARED
18+
19+
# Provides a relative path to your source file(s).
20+
src/main/cpp/sample-jni.cpp )
21+
22+
# Searches for a specified prebuilt library and stores the path as a
23+
# variable. Because CMake includes system libraries in the search path by
24+
# default, you only need to specify the name of the public NDK library
25+
# you want to add. CMake verifies that the library exists before
26+
# completing its build.
27+
28+
find_library( # Sets the name of the path variable.
29+
log-lib
30+
31+
# Specifies the name of the NDK library that
32+
# you want CMake to locate.
33+
log )
34+
35+
# Specifies libraries CMake should link to your target library. You
36+
# can link multiple libraries, such as libraries you define in this
37+
# build script, prebuilt third-party libraries, or system libraries.
38+
39+
target_link_libraries( # Specifies the target library.
40+
sample-jni
41+
42+
# Links the target library to the log library
43+
# included in the NDK.
44+
${log-lib} )

JniSample/app/build.gradle

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 28
9+
defaultConfig {
10+
applicationId "com.pramonow.androidadvanced"
11+
minSdkVersion 19
12+
targetSdkVersion 28
13+
versionCode 1
14+
versionName "1.0"
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
17+
externalNativeBuild {
18+
cmake {
19+
cppFlags ""
20+
}
21+
}
22+
}
23+
buildTypes {
24+
release {
25+
minifyEnabled false
26+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
27+
}
28+
}
29+
externalNativeBuild {
30+
cmake {
31+
path "CMakeLists.txt"
32+
}
33+
}
34+
}
35+
36+
dependencies {
37+
implementation fileTree(dir: 'libs', include: ['*.jar'])
38+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
39+
implementation 'com.android.support:appcompat-v7:28.0.0'
40+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
41+
testImplementation 'junit:junit:4.12'
42+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
43+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
44+
45+
implementation 'com.google.android.gms:play-services-maps:16.0.0'
46+
}

JniSample/app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.pramonow.androidadvanced
2+
3+
import android.support.test.InstrumentationRegistry
4+
import android.support.test.runner.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getTargetContext()
22+
assertEquals("com.pramonow.androidadvanced", appContext.packageName)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<resources>
2+
<!--
3+
TODO: Before you run your application, you need a Google Maps API key.
4+
5+
To get one, follow this link, follow the directions and press "Create" at the end:
6+
7+
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=BB:B8:C9:CA:19:9F:4E:00:FC:B0:BF:0F:21:37:7E:5C:BB:6B:39:29%3Bcom.pramonow.androidadvanced
8+
9+
You can also add your credentials to an existing key, using these values:
10+
11+
Package name:
12+
BB:B8:C9:CA:19:9F:4E:00:FC:B0:BF:0F:21:37:7E:5C:BB:6B:39:29
13+
14+
SHA-1 certificate fingerprint:
15+
BB:B8:C9:CA:19:9F:4E:00:FC:B0:BF:0F:21:37:7E:5C:BB:6B:39:29
16+
17+
Alternatively, follow the directions here:
18+
https://developers.google.com/maps/documentation/android/start#get-key
19+
20+
Once you have your key (it starts with "AIza"), replace the "google_maps_key"
21+
string in this file.
22+
-->
23+
<string name="google_maps_key" translatable="false" templateMergeStrategy="preserve">
24+
AIzaSyDyvCJstB4Xt5QAsJq7hTOmxtz3_bVYu1k
25+
</string>
26+
</resources>
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.pramonow.androidadvanced">
5+
6+
<!--
7+
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
8+
Google Maps Android API v2, but you must specify either coarse or fine
9+
location permissions for the 'MyLocation' functionality.
10+
-->
11+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
12+
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
13+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
14+
15+
<application
16+
android:allowBackup="true"
17+
android:icon="@mipmap/ic_launcher"
18+
android:label="@string/app_name"
19+
android:roundIcon="@mipmap/ic_launcher_round"
20+
android:supportsRtl="true"
21+
android:theme="@style/AppTheme"
22+
tools:ignore="GoogleAppIndexingWarning">
23+
<activity android:name=".JniActivity">
24+
<intent-filter>
25+
<action android:name="android.intent.action.MAIN"/>
26+
27+
<category android:name="android.intent.category.LAUNCHER"/>
28+
</intent-filter>
29+
</activity>
30+
31+
<!--
32+
The API key for Google Maps-based APIs is defined as a string resource.
33+
(See the file "res/values/google_maps_api.xml").
34+
Note that the API key is linked to the encryption key used to sign the APK.
35+
You need a different API key for each encryption key, including the release key that is used to
36+
sign the APK for publishing.
37+
You can define the keys for the debug and release targets in src/debug/ and src/release/.
38+
-->
39+
<meta-data
40+
android:name="com.google.android.geo.API_KEY"
41+
android:value="@string/google_maps_key"/>
42+
43+
</application>
44+
45+
</manifest>

0 commit comments

Comments
 (0)