I am started learning android ndk. I am new in it. I had created one project in Android studio 3.0.1 using adding c++ support. In this application user enter number in activity and using c++ code it tells whether its prime number or not. Here is my Activity code:
package com.app.androidkt.samplendk;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText number_chk;
Button result_btn;
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result_btn = (Button)findViewById(R.id.result_btn);
number_chk = (EditText) findViewById(R.id.number_chk);
final TextView tv = (TextView) findViewById(R.id.sample_text);
result_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int number;
try{
number = Integer.parseInt(number_chk.getText().toString());
tv.setText(isPrimeNumber(number));
}
catch (Exception e)
{
tv.setText("Please enter valid number");
}
}
});
// Example of a call to a native method
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
//public native String stringFromJNI();
private native String isPrimeNumber(int number);
}
my native-lib.cpp is like:
#include <jni.h>
#include <string>
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_app_androidkt_samplendk_MainActivity_isPrimeNumber(
JNIEnv *jenv,
jobject self,
jint number)
{
std::string result = "Prime Number";
int i;
for(i = 2; i <= number / 2; ++i)
{
if(number % i == 0)
{
result = "Not a Prime Number";
break;
}
}
return jenv->NewStringUTF(result.c_str());
}
my app.gradle is like below, I had not changed anything in this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.app.androidkt.samplendk"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
My application structure is like:
Upto this everything is perfect. Application runs and give me expected result. Now what i want is - Create new project and use above project .so file so in my new project I don't want to write c++ logic again. For this I am planning create a new Project in that add directory as jniLibs and copy .so file of above project. But I did not got .so file in above project. When I extract apk file of above project then I got only one so file.
- So How to create .so file for above project
- How to use this .so file in another project
Any suggestion and answer will be appreciated. Thanks in advance.