I have a functionality to achieve, which is a difficult one. But someone else already achieved it in their app. So i decompiled the apk file to find out what he did.
And i found that he has lib like libs/armabi/XXXX.so
Then in the code, he is loading the the lib like this
static
{
System.loadLibrary("XXXX");
}
Then he call the native method like
public native int addTwoNumbes(int a, int b);
So i copied the lib to my libs/armabi/XXX.so
When i call , System.loadLibrary(XXXX) , no error was thown.
But when i call the addTwoNumbers method, it gives me UnsatisfiedLinkError.
Can anyone point me in the right direction , if its not a huge problem for you a quick fix code really helps me as im in kinda hurry (as everyone :) )
the java class name should be exactly the same name, same package as frim where you took the code, for more details read about jni
Related
I've successfully written a go mobile library and used it from an Android Java app.
I've also successfully called a c function from JNI. This one has the JNIEnv pointer.
I'd like to pass the context/JNIEnv from Java to Go.
Here's where it seems to get complicated:
Go Mobile does not seem to use JNIEnv at all.
LoadJNI.java does have a context object and claims to pass it to the Go side. But there it isn't visible.
I've been able to include jni.h using the "C" directive
I can transfer the JNIEnv from JNI/C or store it there as a global variable but that's an ugly kludge.
https://github.com/golang/go/issues/16876 talks about a proposal to do reverse binding but it's unclear what the status is.
https://godoc.org/golang.org/x/mobile/cmd/gobind also talks about it but the following code snippet
import "Java/java/lang/System"
t := System.CurrentTimeMillis()
returns
cannot find package "Java/java/lang/System" in any of:
/usr/local/go/src/Java/java/lang/System (from $GOROOT)
/Users/----/go/src/Java/java/lang/System (from $GOPATH)
In C I solved it with a compiler directive: #cgo CFLAGS: -I...
But I'm not sure how to do it for Java
In short, I'm now stuck and would appreciate help. Ideally, I'd just like to pass the object as a parameter, if possible. However, I'd settle for any reverse binding to call Java from Go.
I hate to answer my own question, but I'd hate someone to go through this as well too. I'll update this answer as I go along.
Basic Setup
Edit build.gradle and add GOPATH and GO to gobind (additions in bold):
gobind {
pkg = ".../reverse/reverse"
GOPATH = "/Users/johndoe/go"
GO = "/usr/local/go/bin"
}
How to develop
Start from the reverse example. Get it to compile and run.
Make one modification and recompile.
Missing gobind
It turns out the installation process neglected to build gobind. Run go install golang.org/x/mobile/cmd/gobind
I'm Stuck
Trying to modify the reverse project to do anything other than what it's already doing is failing. Even stuff like passing an object and calling toString will fail.
More to come...
Not able to call any native method from java file.Getting this error not able to resolve.I already check method name in c file absolute match and .so file also loaded.Getting Log for JNI_OnLoad ENTRY.
Based on your question it is hard to judge.
I suggest to start with something super simple. For example, take a look here:
http://jnicookbook.owsiak.org/recipe-No-001/
This is super simple, Hello World, sample for JNI based codes.
Just take a look at all the pieces required to successfully run the code and try to replicate it in your environment.
I was happy using arc4random_uniform since iOS, and also for iOS targets of cocos2d-x.
Turns out it doesn't work for Android. The error:
error: 'arc4random_uniform' was not declared in this scope
How can I get around this?
Worst case, at compile time I'd check if arc4random_uniform() exists, and if not, use some other method (like the old arc4random()...).
I really would like to avoid using different code bases for different targets here.
Any other suggestions?
Note: As cocos2d-x is a "one code"→"many platforms", delegating this issue to Java code for Android would work against that.
Some of the C++ libs you can use in ios are not available in Android.
Unfortunately arc4ramndom is just one of them.
So the only way is to use an alternative of the stdlib like std::rand() or the default random engine if you want something more.
This is an example about how to use std::default_random_engine to get a random value in a given interval.
int randomValue(int from, int to) {
std::random_device rd;
std::default_random_engine e1(rd());
std::uniform_int_distribution<int> uniform_dist(from, to);
int mean = uniform_dist(e1);
return mean;
}
You can use Cocos2d native methods for generating random numbers. CCRANDOM_0_1() for example generates a random CGFloat between 0 and 1.
this is a real basic question, but I am having a frustrating time learning how to compile/run an android pong program from a website. The tutorial I am following is here - http://mikeyhogarth.wordpress.com/2010/10/09/how-to-develop-pong-for-android/
I keep trying, and I am getting errors. I am new to android (but not programming) so I am having trouble identifying the problem.
Also the tutorial mentions layout.xml, but I do not see a layout.xml in eclipse, I see a main.xml, is that the same thing?
Attached is my screenshot with errors. Suggestions? Have I configured eclipse/java/android wrong? Is there a problem with the tutorial? I appreciate links to any other good game/graphic based starting tutorials you may have to offer.
Screenshot here
Looks like you need to declare your package in your java files like so:
package com.mikey.pong;
public class GameState {
//screen width and height
final int _screenWidth = 300;
final int _screenHeight = 420;
....
Looks like the tutorial assumed you would provide this.
and yes, your main.xml file is considered a layout, hence living in the layout dir.
It seems that you didn't import the classes contained in the android SDK to your project. In eclipse, press CTRL+SHIFT+O to invoke the import feature.
It looks like you need to add import statements to the classes you've copied from the tutorial. Double clicking on one of the errors (i.e. SurfaceHolder cannot be resolved to a type) should take you to it's place in the code. Hovering over where it says SurfaceHolder for a moment should pop-up something that will allow you to "Import 'SurfaceHolder' (android.view)". Try doing that and see if it starts getting rid of some of those errors.
I compiled this library using NDK into an .so. I can successfully load it with System.LoadLibary() but when I try the example code for the library, it throws an UnsatisfiedLinkError. I checked the source code for the library and it indeed has the functions I'm using with the same parameters (except JNIEnv and jobject) along with the same returns. Plus, surely the creator of the library would share working example code. So what am I doing wrong? I copied the exact code from the page, and changed the variable names accordingly.
Without more detail it's difficult to say. Having looked at the library on that site, it's possible that you've named your class differently. You need to call your Java class Giffle and it has to be in the package org.jiggawatt.giffle. This isn't a naming convention that you would stumble upon by accident ;-)
package org.jiggawatt.giffle;
public class Giffle {
static {
System.loadLibrary("gifflen");
}
public native int Init(String gifName, int w, int h,
int numColors, int quality,
int frameDelay);
public native void Close();
public native int AddFrame(int[] inArray);
}
To use it, you would either make the calls in the Giffle class, or probably a cleaner way is to use an instance:
Giffle giffle = new Giffle();
giffle.Init(...);
giffle.AddFrame(...);
giffle.Close();
Does seem a bit odd that the Java part wasn't in the zip, especially as the class name is hardcoded into the C symbol. Maybe the guy who wrote it had a whole bunch of extra code in the Java part that he didn't want anyone to see.
As far as I can tell, the C and C++ code is complete. You shouldn't need to modify it at all. The implementations of the native methods are in gifflen.cpp, and have names like Java_org_jiggawatt_giffle_Giffle_Init. They have the correct native-side arguments for the JNI calling convention.
How are you building this? I found that I had to do Project->Clean… in Eclipse every time after running ndk-build to let Eclipse pick up the .so file and copy it to the apk.
Try this method. http://mpigulski.blogspot.com/2010/09/debugging-dlopen-unsatisfiedlinkerror.html it helps me.