Cannot resolve corresponding JNI function Java_com_mozilla_greetings_RustGreetings_greeting
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to replicate the this tutorial for building and using Rust lib in Android app, I build the library successful, and uploaded the generated libs here
The function required to be called by Android using the Java_<Package>_Class_function
theme is:
Java_com_mozilla_greetings_RustGreetings_greeting
My android app structure is as below:
I'm getting the below error at my JNI wrapper:
Cannot resolve corresponding JNI function
Java_com_mozilla_greetings_RustGreetings_greeting
The JNI wrapper is:
package com.mozilla.greetings;
public class RustGreetings {
private static native String greeting(final String pattern);
public String sayHello(String to) {
return greeting(to);
}
}
And the main class is:
package com.mozilla.greetings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class GreetingsActivity extends AppCompatActivity {
static {
System.loadLibrary("greetings");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greetings);
RustGreetings g = new RustGreetings();
String r = g.sayHello("world");
((TextView) findViewById(R.id.greetingField)).setText(r);
}
}
android rust java-native-interface
add a comment |
I'm trying to replicate the this tutorial for building and using Rust lib in Android app, I build the library successful, and uploaded the generated libs here
The function required to be called by Android using the Java_<Package>_Class_function
theme is:
Java_com_mozilla_greetings_RustGreetings_greeting
My android app structure is as below:
I'm getting the below error at my JNI wrapper:
Cannot resolve corresponding JNI function
Java_com_mozilla_greetings_RustGreetings_greeting
The JNI wrapper is:
package com.mozilla.greetings;
public class RustGreetings {
private static native String greeting(final String pattern);
public String sayHello(String to) {
return greeting(to);
}
}
And the main class is:
package com.mozilla.greetings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class GreetingsActivity extends AppCompatActivity {
static {
System.loadLibrary("greetings");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greetings);
RustGreetings g = new RustGreetings();
String r = g.sayHello("world");
((TextView) findViewById(R.id.greetingField)).setText(r);
}
}
android rust java-native-interface
Did you setextern "C"
for the function in your cpp source?
– Alex Cohn
Nov 25 '18 at 9:26
The library seems to be OK, I don't see issues with name mangling. I would suggest movingstatic
withloadLibrary()
into the class with native methods (this is pretty common approach), and then try troubleshooting linkage withjava -verbose:jni
.
– Danila Kiver
Nov 25 '18 at 11:40
add a comment |
I'm trying to replicate the this tutorial for building and using Rust lib in Android app, I build the library successful, and uploaded the generated libs here
The function required to be called by Android using the Java_<Package>_Class_function
theme is:
Java_com_mozilla_greetings_RustGreetings_greeting
My android app structure is as below:
I'm getting the below error at my JNI wrapper:
Cannot resolve corresponding JNI function
Java_com_mozilla_greetings_RustGreetings_greeting
The JNI wrapper is:
package com.mozilla.greetings;
public class RustGreetings {
private static native String greeting(final String pattern);
public String sayHello(String to) {
return greeting(to);
}
}
And the main class is:
package com.mozilla.greetings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class GreetingsActivity extends AppCompatActivity {
static {
System.loadLibrary("greetings");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greetings);
RustGreetings g = new RustGreetings();
String r = g.sayHello("world");
((TextView) findViewById(R.id.greetingField)).setText(r);
}
}
android rust java-native-interface
I'm trying to replicate the this tutorial for building and using Rust lib in Android app, I build the library successful, and uploaded the generated libs here
The function required to be called by Android using the Java_<Package>_Class_function
theme is:
Java_com_mozilla_greetings_RustGreetings_greeting
My android app structure is as below:
I'm getting the below error at my JNI wrapper:
Cannot resolve corresponding JNI function
Java_com_mozilla_greetings_RustGreetings_greeting
The JNI wrapper is:
package com.mozilla.greetings;
public class RustGreetings {
private static native String greeting(final String pattern);
public String sayHello(String to) {
return greeting(to);
}
}
And the main class is:
package com.mozilla.greetings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class GreetingsActivity extends AppCompatActivity {
static {
System.loadLibrary("greetings");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greetings);
RustGreetings g = new RustGreetings();
String r = g.sayHello("world");
((TextView) findViewById(R.id.greetingField)).setText(r);
}
}
android rust java-native-interface
android rust java-native-interface
asked Nov 25 '18 at 5:37
Hasan A YousefHasan A Yousef
6,22544074
6,22544074
Did you setextern "C"
for the function in your cpp source?
– Alex Cohn
Nov 25 '18 at 9:26
The library seems to be OK, I don't see issues with name mangling. I would suggest movingstatic
withloadLibrary()
into the class with native methods (this is pretty common approach), and then try troubleshooting linkage withjava -verbose:jni
.
– Danila Kiver
Nov 25 '18 at 11:40
add a comment |
Did you setextern "C"
for the function in your cpp source?
– Alex Cohn
Nov 25 '18 at 9:26
The library seems to be OK, I don't see issues with name mangling. I would suggest movingstatic
withloadLibrary()
into the class with native methods (this is pretty common approach), and then try troubleshooting linkage withjava -verbose:jni
.
– Danila Kiver
Nov 25 '18 at 11:40
Did you set
extern "C"
for the function in your cpp source?– Alex Cohn
Nov 25 '18 at 9:26
Did you set
extern "C"
for the function in your cpp source?– Alex Cohn
Nov 25 '18 at 9:26
The library seems to be OK, I don't see issues with name mangling. I would suggest moving
static
with loadLibrary()
into the class with native methods (this is pretty common approach), and then try troubleshooting linkage with java -verbose:jni
.– Danila Kiver
Nov 25 '18 at 11:40
The library seems to be OK, I don't see issues with name mangling. I would suggest moving
static
with loadLibrary()
into the class with native methods (this is pretty common approach), and then try troubleshooting linkage with java -verbose:jni
.– Danila Kiver
Nov 25 '18 at 11:40
add a comment |
1 Answer
1
active
oldest
votes
I solved it using JNA
whcih I think is slower than JNI
, I'll write my solution below using JNA
hoping someone provide the required fix using JNI
My app structure is as below, using kotlin
:
- I added the
libjnidispatch.so
to each library build folder, , this can be obtained by extracted the requiredandroid architecture from here, download the required jar, then extract it to get thelibjnidispatch.so
- I created interbface for
jna
JNA.kt
package com.mozilla.greetings
import com.sun.jna.Library
interface JNA : Library {
fun rust_greeting(pattern: String): String
}
I created wrapper for jna, RustGreetings.kt:
package com.mozilla.greetings
import com.sun.jna.Native
class RustGreetings {
fun sayHello(to: String): String =
Native.loadLibrary<JNA>("greetings", JNA::class.java).rust_greeting(to)
}
Main activity, GreetingsActivity.kt:
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
Note:
In order to avoid using findViewById
I used kotlin extension, as explained here and addeding the below to the build.gradle
(module):
apply plugin: 'kotlin-android-extensions'
UPDATE
It looks it is an IDE issue nothing with the code, the app had been executed.
I re-wrote it using Kotlin, and it was executed smoothly as well, below my kotlin code:
GreetingsActivity.kt
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("My Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
RustGreetings.kt
package com.mozilla.greetings
class RustGreetings {
private external fun greeting(pattern: String): String
fun sayHello(to: String): String = greeting(to)
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mozilla.greetings"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
activity_greetings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GreetingsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" android:id="@+id/greetingField"/>
</android.support.constraint.ConstraintLayout>
Below structure and execution, , the apk is here:
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464954%2fcannot-resolve-corresponding-jni-function-java-com-mozilla-greetings-rustgreetin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I solved it using JNA
whcih I think is slower than JNI
, I'll write my solution below using JNA
hoping someone provide the required fix using JNI
My app structure is as below, using kotlin
:
- I added the
libjnidispatch.so
to each library build folder, , this can be obtained by extracted the requiredandroid architecture from here, download the required jar, then extract it to get thelibjnidispatch.so
- I created interbface for
jna
JNA.kt
package com.mozilla.greetings
import com.sun.jna.Library
interface JNA : Library {
fun rust_greeting(pattern: String): String
}
I created wrapper for jna, RustGreetings.kt:
package com.mozilla.greetings
import com.sun.jna.Native
class RustGreetings {
fun sayHello(to: String): String =
Native.loadLibrary<JNA>("greetings", JNA::class.java).rust_greeting(to)
}
Main activity, GreetingsActivity.kt:
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
Note:
In order to avoid using findViewById
I used kotlin extension, as explained here and addeding the below to the build.gradle
(module):
apply plugin: 'kotlin-android-extensions'
UPDATE
It looks it is an IDE issue nothing with the code, the app had been executed.
I re-wrote it using Kotlin, and it was executed smoothly as well, below my kotlin code:
GreetingsActivity.kt
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("My Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
RustGreetings.kt
package com.mozilla.greetings
class RustGreetings {
private external fun greeting(pattern: String): String
fun sayHello(to: String): String = greeting(to)
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mozilla.greetings"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
activity_greetings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GreetingsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" android:id="@+id/greetingField"/>
</android.support.constraint.ConstraintLayout>
Below structure and execution, , the apk is here:
add a comment |
I solved it using JNA
whcih I think is slower than JNI
, I'll write my solution below using JNA
hoping someone provide the required fix using JNI
My app structure is as below, using kotlin
:
- I added the
libjnidispatch.so
to each library build folder, , this can be obtained by extracted the requiredandroid architecture from here, download the required jar, then extract it to get thelibjnidispatch.so
- I created interbface for
jna
JNA.kt
package com.mozilla.greetings
import com.sun.jna.Library
interface JNA : Library {
fun rust_greeting(pattern: String): String
}
I created wrapper for jna, RustGreetings.kt:
package com.mozilla.greetings
import com.sun.jna.Native
class RustGreetings {
fun sayHello(to: String): String =
Native.loadLibrary<JNA>("greetings", JNA::class.java).rust_greeting(to)
}
Main activity, GreetingsActivity.kt:
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
Note:
In order to avoid using findViewById
I used kotlin extension, as explained here and addeding the below to the build.gradle
(module):
apply plugin: 'kotlin-android-extensions'
UPDATE
It looks it is an IDE issue nothing with the code, the app had been executed.
I re-wrote it using Kotlin, and it was executed smoothly as well, below my kotlin code:
GreetingsActivity.kt
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("My Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
RustGreetings.kt
package com.mozilla.greetings
class RustGreetings {
private external fun greeting(pattern: String): String
fun sayHello(to: String): String = greeting(to)
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mozilla.greetings"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
activity_greetings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GreetingsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" android:id="@+id/greetingField"/>
</android.support.constraint.ConstraintLayout>
Below structure and execution, , the apk is here:
add a comment |
I solved it using JNA
whcih I think is slower than JNI
, I'll write my solution below using JNA
hoping someone provide the required fix using JNI
My app structure is as below, using kotlin
:
- I added the
libjnidispatch.so
to each library build folder, , this can be obtained by extracted the requiredandroid architecture from here, download the required jar, then extract it to get thelibjnidispatch.so
- I created interbface for
jna
JNA.kt
package com.mozilla.greetings
import com.sun.jna.Library
interface JNA : Library {
fun rust_greeting(pattern: String): String
}
I created wrapper for jna, RustGreetings.kt:
package com.mozilla.greetings
import com.sun.jna.Native
class RustGreetings {
fun sayHello(to: String): String =
Native.loadLibrary<JNA>("greetings", JNA::class.java).rust_greeting(to)
}
Main activity, GreetingsActivity.kt:
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
Note:
In order to avoid using findViewById
I used kotlin extension, as explained here and addeding the below to the build.gradle
(module):
apply plugin: 'kotlin-android-extensions'
UPDATE
It looks it is an IDE issue nothing with the code, the app had been executed.
I re-wrote it using Kotlin, and it was executed smoothly as well, below my kotlin code:
GreetingsActivity.kt
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("My Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
RustGreetings.kt
package com.mozilla.greetings
class RustGreetings {
private external fun greeting(pattern: String): String
fun sayHello(to: String): String = greeting(to)
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mozilla.greetings"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
activity_greetings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GreetingsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" android:id="@+id/greetingField"/>
</android.support.constraint.ConstraintLayout>
Below structure and execution, , the apk is here:
I solved it using JNA
whcih I think is slower than JNI
, I'll write my solution below using JNA
hoping someone provide the required fix using JNI
My app structure is as below, using kotlin
:
- I added the
libjnidispatch.so
to each library build folder, , this can be obtained by extracted the requiredandroid architecture from here, download the required jar, then extract it to get thelibjnidispatch.so
- I created interbface for
jna
JNA.kt
package com.mozilla.greetings
import com.sun.jna.Library
interface JNA : Library {
fun rust_greeting(pattern: String): String
}
I created wrapper for jna, RustGreetings.kt:
package com.mozilla.greetings
import com.sun.jna.Native
class RustGreetings {
fun sayHello(to: String): String =
Native.loadLibrary<JNA>("greetings", JNA::class.java).rust_greeting(to)
}
Main activity, GreetingsActivity.kt:
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
Note:
In order to avoid using findViewById
I used kotlin extension, as explained here and addeding the below to the build.gradle
(module):
apply plugin: 'kotlin-android-extensions'
UPDATE
It looks it is an IDE issue nothing with the code, the app had been executed.
I re-wrote it using Kotlin, and it was executed smoothly as well, below my kotlin code:
GreetingsActivity.kt
package com.mozilla.greetings
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*
class GreetingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greetings)
val g = RustGreetings()
val r = g.sayHello("My Rust")
greetingField.text = r
}
companion object {
init {
System.loadLibrary("greetings")
}
}
}
RustGreetings.kt
package com.mozilla.greetings
class RustGreetings {
private external fun greeting(pattern: String): String
fun sayHello(to: String): String = greeting(to)
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mozilla.greetings"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
activity_greetings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GreetingsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" android:id="@+id/greetingField"/>
</android.support.constraint.ConstraintLayout>
Below structure and execution, , the apk is here:
edited Nov 26 '18 at 9:31
answered Nov 25 '18 at 12:53
Hasan A YousefHasan A Yousef
6,22544074
6,22544074
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464954%2fcannot-resolve-corresponding-jni-function-java-com-mozilla-greetings-rustgreetin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you set
extern "C"
for the function in your cpp source?– Alex Cohn
Nov 25 '18 at 9:26
The library seems to be OK, I don't see issues with name mangling. I would suggest moving
static
withloadLibrary()
into the class with native methods (this is pretty common approach), and then try troubleshooting linkage withjava -verbose:jni
.– Danila Kiver
Nov 25 '18 at 11:40