In my android project I am using an external jar library which utilizes a runtime library libiconv.so. I have the library included in my project's lib directory. The library is included for all three architectures in the following directory hirarcy.
libs>
armeabi>libiconv.so
armeabi-v7a>libiconv.so
x86>libiconv.so
But I am getting Exceptions as logged by log cat:
05-23 12:18:58.857 3081-3081/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load libiconv.so from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.tariq.buynow-1.apk,libraryPath=/data/app-lib/com.tariq.buynow-1]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.tariq.buynow.CameraActivity.<clinit>(CameraActivity.java:30)
Where at CameraActivity.java:30 is:
static { System.loadLibrary("libiconv.so"); }
I have also tried
static { System.loadLibrary("iconv"); }
Is it some thing to do with gradle configuration, as I am new to Android Studio, or the error source is something else?
I had this same problem trying to use ZBar SDK. You most likely need to edit your build.gradle to make sure that the native libraries are included in your APK. This is how I fixed your problem: https://stackoverflow.com/a/16993006/2221876
System.loadLibrary() looks in in the lib directories of the device, not the application. To do what you want you'll need to copy the lib from the app onto the phone's file system, and load it from there. I would suggest putting the libs in /res/raw/ and do it this way:
try {
// Point the input stream to the library in /res/raw
InputStream is = context.getResources().openRawResource(R.raw.libiconv);
// Create a BufferedInputStream from the InputStream
BufferedInputStream bis = new BufferedInputStream(is);
// Set the FileOutputStream to the app's data directory
FileOutputStream fos = context.openFileOutput("libiconv.so", Context.MODE_PRIVATE);
byte data[] = new byte[1024];
int count;
while ((count = bis.read(data, 0, 1024)) != -1) {
fos.write(data, 0, count);
}
fos.close();
bis.close();
is.close();
System.load(context.getFilesDir() + "/libiconv.so");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
This may not work exactly, but the general gist of this should.
Add the following line into your build.gradle scripts
jniLibs.srcDirs = ['libs'] // copy the *.so to package
In the project view, you must can see :
Module/libs/ABI NAME/*.so
Related
i'm using the DownloadManager to download files and save in a folder inside downloads folder in my application context folder on internal storage, this works fine in Android 6 but in android 4 the path returns null.
This code works in android 6 but not in android 4
File p = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
i have change to this and works
File p = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if(p == null){
p = getApplicationContext().getDir(Environment.DIRECTORY_DOWNLOADS, MODE_PRIVATE);
}
My problem is in DownloadManager, when i use the follow code an NullPointerReference occurs
req.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS + "/viewer", fileName);
can anyone help me?
EDIT
This is the stack trace
FATAL EXCEPTION: main
java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:441)
at android.app.DownloadManager$Request.setDestinationFromBase(DownloadManager.java:508)
at android.app.DownloadManager$Request.setDestinationInExternalFilesDir(DownloadManager.java:470)
at br.com.digitaldoc.ddv4.activities.MainActivity$7.onClick(MainActivity.java:405)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:958)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1088)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2861)
at android.widget.AbsListView$1.run(AbsListView.java:3535)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4931)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
at dalvik.system.NativeStart.main(Native Method)
and this line MainActivity.java:405 is
req.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS + "/viewer", fileName);
I am receiving an exception (via Crashlytics) as follows:
java.lang.NoClassDefFoundError: com.example.android.R$drawable
at com.example.android.adapters.ExampleAdapter.(ExampleAdapter.java:59)
at com.example.android.activities.Dashboard.onCreate(Dashboard.java)
at android.app.Activity.performCreate(Activity.java:5152)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5056)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(NativeStart.java)
My code at that line is as follows (line 59 refers to showStubImage):
contactImageOptions = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.default_contact)
.showImageOnFail(R.drawable.default_contact)
.showImageForEmptyUri(R.drawable.default_contact)
.cacheInMemory(true)
.cacheOnDisc(true)
.build();
I cannot reproduce this exception on my IDE and its seems that not all my users get this. The drawable "default_contact" does exist on the IDE and is part of the project (i.e it does not come from a library). I have also noticed that 92% of the users getting this exception are from Amazon devices.
I got the same exception when I moved *.java file from "app" to new created module (on Android Studio).
This java file referenced to R.drawable.com_facebook_profile_default_icon Facebook module resource.
The problem was solved simply by rebuild the project.
I've been trying to get ANTLR-3.5-complete.jar to work on an Android app, but I can't get it to work! Bart Kiers's answer HERE has helped me a lot, but I still can't get it to work.
I'm trying to make a Button that, when clicked, will validate whether the text in the EditText view is correct according to my grammar.g, but it doesn't matter whether the text I input is correct or not, my app always crashes when I click the button while instantiating the ANTLRStringStream. Here's what I have:
public void onClickVerify(View v) throws TypeNotPresentException {
String source = "foobar";
Log.e("TestDebug", "Instantiating views");
TextView out = (TextView) findViewById(R.id.textView1);
EditText in = (EditText) findViewById(R.id.editText1);
try {
Log.e("TestDebug", "Getting source from EditText");
source = in.getText().toString();
Log.e("TestDebug", "source = " + source);
Log.e("TestDebug", "Instantiating stream");
ANTLRStringStream stream = new ANTLRStringStream(source);
Log.e("TestDebug", "Instantiating lexer");
grammarLexer lexer = new grammarLexer(stream);
Log.e("TestDebug", "Instantiating parser");
grammarParser parser = new grammarParser(
new BufferedTokenStream(lexer));
Log.e("TestDebug", "Applying rule to parser");
out.setText(source + " = " + !parser.failed());
} catch (IllegalStateException ise) {
out.setText("Exception caught");
}
}
And here's what I see in the logs:
Instantiating views
Getting source from EditText
source = test33
Instantiating stream
And then the "Unfortunately, TestAntlrApp has stopped." window pops up.
EDIT: Here's more of my logcat.
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
... 11 more
Caused by: java.lang.NoClassDefFoundError: org.antlr.runtime.ANTLRStringStream
at com.test.antlr.MainActivity.onClickVerify(MainActivity.java:89)
Something about not finding the class ANTLRStringStream class? However there seems to be no errors/warnings in the code and I can even see the class's hierarchy.
Due to a recent ADT update, you can't just add external libraries to the Build Path using Eclipse's conventional way of doing it. Instead, you have to put it inside the libs folder for it to be packed within the .apk. If you simply add it to the build path, it will be ignored when building your .apk.
Symptom : The app was not recognizing external library classes.
Solution : Put the .jar file in the libs folder.
Folks,
I am working on an android application where I need a third party .so library. I built
this third party library (with ndk-build) as per their instructions and was then looking
to include this .so in to my Android project.
Therefore I followed the steps described in docs/PREBUILTS.html and successfully build the
new .so in the jni/prebuilt directory. Now I tried leveraging the .so facilities by using it in a simple test android app. So what i do is :
static {
Log.i("load so > ","load so");
System.loadLibrary("xyz");
}
/* The native functions */
private static native int openFile(String filename);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
String path = getPathForDownloadDirectoryFile();
Log.i("file path> ", path);
int num= openFile(path);
}catch(Exception e){
Log.e(">", "could not open the file");
}
}
Now when I run my app I get a debug message saying :
No JNI_OnLoad found in /data/data/com.example.myfirstapp/lib/xyz.so 0x411e6738, skipping init
and then the application shuts down.
For More Info, Here is the error log :
No JNI_OnLoad found in /data/data/com.example.mysecondapp/lib/xyz.so 0x411e67a0, skipping init
W/dalvikvm( 570): No implementation found for native Lcom/example/mysecondapp/MainActivity;.openFile:(Ljava/lang/String;)I
D/AndroidRuntime( 570): Shutting down VM
W/dalvikvm( 570): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
E/AndroidRuntime( 570): FATAL EXCEPTION: main
E/AndroidRuntime( 570): java.lang.UnsatisfiedLinkError: Native method not found: com.example.mysecondapp.MainActivity.openFile:(Ljava/lang/String;)I
E/AndroidRuntime( 570): at com.example.mysecondapp.MainActivity.openFile(Native Method)
E/AndroidRuntime( 570): at com.example.mysecondapp.MainActivity.onCreate(MainActivity.java:31)
E/AndroidRuntime( 570): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime( 570): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime( 570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E/AndroidRuntime( 570): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime( 570): at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime( 570): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime( 570): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 570): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 570): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime( 570): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 570): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime( 570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 570): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 146): Force finishing activity com.example.mysecondapp/.MainActivity
As I could see that native implementation for the openFile() method was not found but the same xyz.so lib worked pretty neat with the original sample app from the third party. I am pretty much a starter with Android-ndk world.
Java-Android-NDK Ninjas ..any guess on what I might be missing ?
I'll highly appreciate any help here :)
As guycole said "No JNI_OnLoad" is just a warning , your problem lies elsewhere .
As you mentioned you successfully compiled your "so" file , the problem may lie in your function signatures inside your c/C ++ code it should be something like this
JNIEXPORT jint JNICALL Java_com_your_package_class_method(JNIEnv *d, jobject e, jstring f)
{
//some action
}
The function signatures comes from the header file which is generated using javah tool.You need to generate header file and use the function signature with your package name. For different package and class names the header file and corresponding function signature will change .
worked pretty neat with the original sample app from the third party
This might be the reason its running on the sample app and not on your app.
refer: https://thenewcircle.com/s/post/49/using_ndk_to_call_c_code_from_android_apps
The "No JNI_OnLoad" message is just a warning. JNI_OnLoad is an optional initialization hook.
I guess your problem is inside the openFile() method. Try commenting out the call from Java and see how far you get.
I have a blog post about JNI and some sample code at http://guycole.blogspot.com/2012/03/yet-another-android-ndk-blog-posting.html - perhaps you will find it useful.
Good luck.
It also comes with this log
??-?? ??:??:??.???: INFO/(): java.lang.UnsatisfiedLinkError: Couldn't load *: findLibrary returned null
right??
I think it's the problem of android.mk files.
1:try to swith to armabi v7.
2:load funciton will call open(). check permission of the so.
As mentioned in the previous answers, No JNI_OnLoad is only a warning.
I had got similar problem, I figured the problem is because of file operations.
My app was not having external storage write permission.After adding the below code in
manifest it was working fine
I am getting following Exception when trying to run this code.
java.lang.NoSuchMethodError: org.apache.poi.POIDocument.< init >
Code Snippet:
try {
File file = new File(externalPath + "/abc.doc");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!");
run.setFontSize(2 * 18);
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream out = new FileOutputStream(new File(externalPath + "/agnew.doc"));
doc.write(out);
out.flush();
out.close();
} catch (Exception ex) {
Log.e("Exception==","=="+ex.toString());
ex.printStackTrace();
}
Logcat:
Logcat : FATAL EXCEPTION: main : java.lang.NoSuchMethodError: org.apache.poi.POIDocument. :
at org.apache.poi.hwpf.HWPFDocumentCore.(HWPFDocumentCore.java:145) :
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:218) :
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:186) :
at com.vikas.prudent.CreateDocument.onCreate(CreateDocument.java:45) :
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) :
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) :
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) :
at android.app.ActivityThread.access$2300(ActivityThread.java:125) :
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) :
at android.os.Handler.dispatchMessage(Handler.java:99) :
at android.os.Looper.loop(Looper.java:123) :
at android.app.ActivityThread.main(ActivityThread.java:4627) :
at java.lang.reflect.Method.invokeNative(Native Method) :
at java.lang.reflect.Method.invoke(Method.java:521) :
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) :
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) :
at dalvik.system.NativeStart.main(Native Method)
Just looking at the exception, it appears to be lib version mis-match. Looks like HWPFDocumentCore.java:145 is trying to construct a new POIDocument but cannot find the expected constructor to call. I would take a look at line 145 of HWPFDocumentCore.java and see what constructor it is expecting. Then look for a POI library which has such a constructor in the POIDocument.
It sounds like you have two copies of Apache POI on your classpath, and old one and a new one. My hunch is that your HWPF jar (Scratchpad) is new, but it's picking up an old core POI jar, which is why you're getting the exception.
What you need to do is review all the jars on your classpath, and identify the POI related jars, then ensure you have a consistent set of them.
The POI FAQ has and entry on this very problem, along with some Java code you can use to print out which jar the POI classes come from. If you can't spot the wrong jars directly, try porting something like the code shown there to your android code to help you find the older jar.