Native interfaces in Intellij

117 views
Skip to first unread message

Dennis Rogers

unread,
Jun 12, 2021, 5:01:09 PM6/12/21
to CodenameOne Discussions
I'm trying to create a native interface to access android's SpeechRecognitionHelper. Following the manual I created andinterface that extends NativeInterface i.e.

import com.codename1.system.NativeInterface;

public interface MyNative extends NativeInterface {
String getSpeech(String hi);
}

The manual then says I can generate initial native code by right clicking on the interface and selecting "Generate Native Access" . This option doesn't seem to be available in my setup and I don't know where to go from here.

-Dennis

Shai Almog

unread,
Jun 12, 2021, 9:41:08 PM6/12/21
to CodenameOne Discussions
The plugin is only applicable to Ant projects not to maven projects.
For maven Generate Native Interfaces is one of the run configurations.

Dennis Rogers

unread,
Jun 13, 2021, 6:08:30 PM6/13/21
to CodenameOne Discussions
I generated the native interface but when I try to build it, it says it can't find com.codename1.system.NativeInterface.

Shai Almog

unread,
Jun 13, 2021, 9:37:18 PM6/13/21
to CodenameOne Discussions
On which file?
notice that the native side shouldn't include the native interface, it should only be in common.

Dennis Rogers

unread,
Jun 14, 2021, 5:00:09 PM6/14/21
to CodenameOne Discussions
Sorry but I'm a little confused. I created a file, MyNative.java, in the common folder along with my other code:

package com.symdesign.shoppinggenie;

import com.codename1.system.NativeInterface;
public interface MyNative extends NativeInterface {
     String helloWorld(String hi);
}

When I run "Generate Native Interfaces" I get the error "cannot access com.codename1.system.NativeInterface" but nevertheless
it creates a file in MyProject/src/main/java/com.symdesign/myproject/ called MyNativeImpl.java which looks like the advertised stub. (Is it in this file that I should put my native code? )

Shai Almog

unread,
Jun 14, 2021, 10:09:56 PM6/14/21
to CodenameOne Discussions
That sounds like a bug in the maven project. Let me check.

Dennis Rogers

unread,
Jun 16, 2021, 12:30:09 AM6/16/21
to CodenameOne Discussions
Since I added the generated the native interface I can no longer do cloud builds. Is there a way I can remove the native interface I created for the moment?

Steve Hannah

unread,
Jun 16, 2021, 8:02:59 AM6/16/21
to codenameone...@googlegroups.com
Add the following to the dependences section of your javase/pom.xml file:

<dependency>
<groupId>com.codenameone</groupId>
<artifactId>codenameone-core</artifactId>
<scope>provided</scope>
</dependency>

That should fix the issue.  (Mind you you haven't shared any error messages so it's hard to say what your specific problem is).

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/5676afcc-c439-4018-a258-573ffc9fb6f6n%40googlegroups.com.


--
Steve Hannah
Software Developer
Codename One

Dennis Rogers

unread,
Jun 16, 2021, 12:18:48 PM6/16/21
to CodenameOne Discussions
Thanks, but that didn't seem to help. I don't get any errors when building the app. I get these errors when trying to load it onto the phone:

$ adb install -i ShoppingGenie-debug.apk 

Performing Streamed Install
adb: failed to install ShoppingGenie-debug.apk: Error: Unable to open file: 8370465
Consider using a file under /data/local/tmp/
Error: Can't open file: 8370465

Exception occurred while executing:
java.lang.IllegalArgumentException: Error: Can't open file: 8370465
at com.android.server.pm.PackageManagerShellCommand.setParamsSize(PackageManagerShellCommand.java:461)
at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:1060)
at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:169)
at android.os.ShellCommand.exec(ShellCommand.java:104)
at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:21745)
at android.os.Binder.shellCommand(Binder.java:881)
at android.os.Binder.onTransact(Binder.java:765)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:4860)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:4015)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)


Dennis Rogers

unread,
Jun 23, 2021, 6:15:12 PM6/23/21
to CodenameOne Discussions
I'm at a loss in trying to do a native interface. I created the class "MyNative" 

package com.symdesign.myapp;

import com.codename1.system.NativeInterface;

public interface MyNative extends NativeInterface {
public String doInNative(String hi);
}

Then I ran the tool "create native interfaces" which created the file "MyNativeImpl.java" in the javase folder which 
I modified to: 

package com.symdesign.myapp;

public class MyNativeImpl implements com.symdesign.shoppinggenie.MyNative{
public String doInNative(String param) {
return "Dummy";
}

public boolean isSupported() {
return true;
}
}

If I run this in the simulator it seems to work ok, but when I compile it to the cloud I get an error file with the error at the top:

Found hyp.beamId
Tried to start hyperlog but failed: Failed to start hyperlog: Cannot run program "hyp": error=2, No such file or directory
User-level: 9000
Request Args: 
-----------------
build.incSources=1
hyp.beamId=9cfefcf3-f2c3-467c-b459-83c8c6dc3181
ios.includePush=true
java.version=8
maven.codenameone-maven-plugin=7.0.26
ios.newStorageLocation=true
maven.codenameone-core.version=7.0.26
android.release=false
android.debug=true
-------------------
.
.

Dennis Rogers

unread,
Jun 23, 2021, 6:19:17 PM6/23/21
to CodenameOne Discussions
I forgot to add the code used to test it:

if(cn != null && cn.isSupported()) {
    rv = cn.doInNative("string");
Log.p(rv);
}

which returns "Dummy" as expected.
-------------------


Shai Almog

unread,
Jun 23, 2021, 9:50:50 PM6/23/21
to CodenameOne Discussions
Where's the rest of the error log?
Also you didn't implement the native code for each of the native platforms e.g. Android would have its own impl file and so would iOS.
Did you look at the How Do I video tutorial for native interfaces? It's pre-maven but should cover all of this.

Dennis Rogers

unread,
Jun 26, 2021, 6:21:03 PM6/26/21
to CodenameOne Discussions
Do you need to add native code for all of the native platforms? At the moment I only need it for android. I did go through the video tutorial.

I didn't include the error file from the cloud build because it was so long but nevertheless I've attached it below. 


error (10).txt

Shai Almog

unread,
Jun 26, 2021, 9:47:06 PM6/26/21
to CodenameOne Discussions
Just the Android version is enough. If the Android native file there?
Under which directory from project root?

What's the cn1.plugin.version in the pom.xml?

Dennis Rogers

unread,
Jun 26, 2021, 11:23:28 PM6/26/21
to CodenameOne Discussions
I'm not quite sure what you mean by the "Android native" file. The file with my NativeInterface interface code is in "common/src/main/java/com/symdesigns/shoppinggenie/MyNative.java.
The cn1.pluging.version is 7.0.26 .

Shai Almog

unread,
Jun 27, 2021, 9:43:20 PM6/27/21
to CodenameOne Discussions
Not common. Under the android hierarchy you should have a file with the name MyNativeImpl.java

Dennis Rogers

unread,
Jun 28, 2021, 10:56:58 AM6/28/21
to CodenameOne Discussions
The file MyNative.java I created. I assume it was used to create the MyNativeImpl.java. I see that file is in three places:

android/target/classes/com/symdesign/shoppinggenie/MyNativeImpl.java
android/src/main/java/com/symdesign/shoppinggenie/MyNativeImpl.java
javase/src/main/java/com/symdesign/shoppinggenie/MyNativeImpl.java

when I do "generate native interfaces" only the last one is updated. Should the other two be there ? They are about 3 weeks old.

Shai Almog

unread,
Jun 28, 2021, 9:44:15 PM6/28/21
to CodenameOne Discussions
By default we don't overwrite files that are already there. I suggest deleting and regenerating.

Dennis Rogers

unread,
Jun 30, 2021, 3:14:28 PM6/30/21
to CodenameOne Discussions
I deleted the files and it did seem to compile and run on an android device but I can't do a build for the iphone.

When I did the "genereate native interfaces" it didn't seem to create a "MyNativeImpl" file for the iphone and it generates an error file which I've attached.
error.txt

Shai Almog

unread,
Jun 30, 2021, 10:05:05 PM6/30/21
to CodenameOne Discussions
In iOS it generates native_com_symdesign_shoppinggenie_MyNativeImplCodenameOne.m and native_com_symdesign_shoppinggenie_MyNativeImplCodenameOne.h
Which you need to edit.

For some reason it seems native_com_symdesign_shoppinggenie_MyNativeImplCodenameOne.h is missing. Do you have any idea why?

Dennis Rogers

unread,
Jul 1, 2021, 12:14:31 AM7/1/21
to CodenameOne Discussions
The files appear to be here:

src/main/objectivec/com_symdesign_shoppinggenie_MyNativeImpl.h
src/main/objectivec/com_symdesign_shoppinggenie_MyNativeImpl.m

Shai Almog

unread,
Jul 1, 2021, 11:20:34 PM7/1/21
to CodenameOne Discussions
They don't have the right name. I'm checking.

Steve Hannah

unread,
Jul 2, 2021, 9:07:53 AM7/2/21
to codenameone...@googlegroups.com
Looks like there is a bug right now in the maven plugin regarding where it generates the IOS native interfaces.

It seems to be generating them in src/main/objectivec (i.e. in the "src" directory of the root project).  They should be generated in ios/src/main/objectivec. (i.e. inside the ios submodule).
If seems to be doing the same thing with the "javascript" native files.

Move the contents of your src/main/objectivec directory into ios/src/main/objectivec, and it should fix your issue.

Steve

Dennis Rogers

unread,
Jul 5, 2021, 8:21:02 AM7/5/21
to CodenameOne Discussions
Thanks!
Reply all
Reply to author
Forward
0 new messages