I am developing a library for Android applications which does not use native code (JNI). I have tried suppling the library as an external jar in my Android projects but this method does not include the library contents in the apk and thus throws class not found errors when run in the emulator or device. I have also tried creating the library as an Android project in itself and this does work, but only for public static properties (not methods). With the library and application both being in separate apk's I can see that the VM notices references to the library and can read some properties, but when an attempt to instantiate a class in the library is executed I get class not found even though I can read the public static properties from it (very frustrating!!).
I realize that Davlik byte code is not the same as Java byte code but I am having trouble even finding good information about how to solve what would seem to be a very simple issue in Android. I am looking into the old PlatformLibrary stuff right now but I am not convinced this will work either since the sample has been removed from the Android site :(
So help me out if you can, if I find the answer before this happens I will share it.
viva la Android!
I have tried suppling the library as
an external jar in my Android projects
but this method does not include the
library contents in the apk and thus
throws class not found errors when run
in the emulator or device.
Put the JAR in your libs/ directory, and it will automatically be included in the APK.
This sample project from one of my books shows using the BeanShell JAR this way. Also, all of the GitHub repos starting with "cwac-" on my GitHub page are projects designed to build JARs to be included in Android projects via the libs/ directory.
Related
I have an android app project. I have a Java class with a main() method that I need to run and that main() method needs to have all of the classes and dependencies from the android project in the classpath.
However, all of the tasks to build the android app will produce an android-compatible output which means that all of the classes that I need are smashed down into .dex files so that the android system can read them.
I need to build all of the code/dependencies for my android app project BUT NOT DEX THEM.
Is there a task I can call that will produce the compiled classes/jars/etc without smashing them into dex files? Like, perhaps the task that comes right before dexing?
Or is there a way I can create a specialized build task that will disable dexing all together so that I can just produce all the classes/jars in normal Java format, so that I can then include those as my classpath when I call my main() method?
I'm very, very new to gradle, so if you answer please try to be specific because I likely am not able to make logical jumps or inferences if your answer is advanced. I've spent like 2 weeks trying to learn gradle to figure out how to solve this problem (which at its core is because there is a bug in the latest version of Android Studio. In previous versions, Android Studio would generate the gradle code needed to run this with the appropriate classpath, but the latest version of Android Studio is generating gradle code that uses deprecated properties and so it horks).
I've been able to figure out how to create a jar of just the classes from my personal code, but it doesn't include the classes from all of the dependencies/libraries.
I've tried 100 different iterations of various gradle configurations and attempts. I don't really have gradle code that I can provide as an example of what I'm trying to do, because I can't seem to find anything close.
Gradle 7.3.3
In other words...
My app code is in com.kenny.stuff. That code depends on com.j256.ormlite which depends on android.* and/or androidx.*.
When my android :app project builds, it will produce app-debug.apk, which DOES NOT contain the .class files for com.kenny.stuff, com.j256.ormlite, androidx.*, etc.
During the android build process, the .class files are smashed down into .dex files because the android dalvik system needs them in that structure. So, the apk contains:
app-debug.apk:
/META-INF
/classes.dex
/classes2.dex
/classes3.dex
and inside those dex files are the smashed together .class files.
but what I need is a jar that is:
what-i-need.jar:
/META-INF
/com/kenny/stuff/Stuff.class
/com/kenny/stuff/OtherStuff.class
/com/j256/ormlite/ORMLiteStuff.class
/com/j256/ormlite/ORMLiteOtherStuff.class
/androidx/*.class
etc.
So, I need to be able to compile and collect dependencies for my android project BUT NOT smash all those .class files down into .dex files.
Context: I use ORMlite in my project, which is a 3rd party library that uses #DatabaseTable annotations on Java classes in order to create database tables that are mapped to the objects. ORMlite relies on a config.txt file which describes the table mappings. That file needs to be generated by one of ORMlite's methods. In order to run it, I need my classes and ormlite in the classpath. ormlite needs the other android classes in the classpath.
I used to be able to do this with a simple feature in Android Studio, wherein I could create a Run Configuration that would let me have all of those things in my classpath and run a main() method which contained the code to generate the config.txt file. However, the latest version of Android Studio has a bug and the gradle code it generates for that Run Configuration crashes. So I need a workaround in which I can run a java class's main() method with all of those app classes in my classpath.
I've downloaded some jar files (e.g. gson-2.8.5.jar) and want to add them to visual studio code locally without any extra downloads so I can benefit from autocomplete feature of classes and methods, How should I do it?
I've searched a while in google and could not find anything useful, so even links to other answers is appreciated.
As suggested by LeoZhu-MSFT which references this in comments all I had to do was open only the folder containing stand-alone Java files and if necessary put any jar dependency in a lib subdirectory (I was previously opening all directories with code snippets I work on in different languages as playground).
guys, with my friend started working on a school project - developing a java app for android. He started it, now it's my turn to do my job, so i got the code he already has, set up Android SDK for Eclipse and downloaded all the neccessary files and packages, but it still gives me errors and I can't even run the project.
One of the errors (the main one, I think) is that it can't import android.support.v7.app.ActionBarActivity
If anyone knows what can I do (I've tried all kinds of solutions from the internet, but none work) I would be very grateful.
Here is a screenshot of the code
http://s17.postimg.org/8aw952lha/Capture.jpg
See if you have imported the ActionBar project to your Eclipse workspace, also, right click on your project->properties->android and check that you have referenced/linked the ActionBar project there.
If you are using a repository without the proper ignore files it is possible that you have imported your partners configurations and then have the references to the auxiliary projects broken (you just need to update that).
Hope it helps.
android.support.v7.app.ActionBarActivity is a support library. This allows older versions of Android (before native support for an action bar was available) to use the action bar.
Since you are having difficulty importing it, I'd assume you do not have the support library installed.
Full instructions are available here:
http://developer.android.com/tools/support-library/setup.html
A snippet for adding support libraries to Eclipse:
Make sure you have downloaded the Android Support Library using the SDK Manager.
Create a libs/ directory in the root of your application project.
Copy the JAR file from your Android SDK installation directory (e.g., /extras/android/support/v4/android-support-v4.jar) into your application's project libs/ directory.
Right click the JAR file and select Build Path > Add to Build Path.
I've been trying to use the library SoundTouch in my android app in order to change the tempo of an mp3 file.
I've managed to compile the library following the steps outlined here: http://www.surina.net/soundtouch/README-SoundTouch-Android.html
My question now is how do I import and use the compiled library in my android project in Eclipse?
From following the steps in the link above I've ended up with .so files that I've added into the libs folder of my project. I've also changed the build path to include this new library.
The library is now showing up under "referenced libraries" so I'm guessing I'm some where on the right track?
In my java file I've tried importing the library like this:
static
{
System.loadLibrary("soundtouch");
}
The program builds and runs fine but I don't think the library is actually being loaded as I can't reference any of soundTouch's methods.
Any help would be much appreciated!
The JNI file provided with the SoundTouch library is merely a demonstration and does not provide any means of using any of SoundTouch's features in an Android app from Java.
I have imported a .jar into my Eclipse Android project (importing it to the libs folder and adding the jar to the libraries in Java Build Path), but when I attempt to use it, I get a java.lang.VerifyError. The library is Jiwigo 0.13.1.1; a java library for accessing Piwigo image galleries. I know this should work, because there's an open source android project called RegalAndroid that uses it too (though, an older version, and it copied the source instead of properly importing it). The .jar is the only copy of the library on my computer, so I'd be surprised if it was a version clash error. Any ideas?
Here's my error log (shortened because all the rest is normal android stuff):
FATAL EXCEPTION: main
java.lang.VerifyError: fr.mael.jiwigo.transverse.session.SessionManager
at com.terrapages.itemdetailsactivities.FavoritesDetailsActivity.initViews(FavoritesDetailsActivity.java:179)
at com.terrapages.itemdetailsactivities.FavoritesDetailsActivity.onResume(FavoritesDetailsActivity.java:83)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
at android.app.Activity.performResume(Activity.java:3823)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
[...]
And the line it crashes on (with dummy string values that are different in the code):
SessionManager sessionManager = new SessionManager("username", "password", "gallery-url");
This is the first use of the library anywhere in the code.
I'm the developer of Jiwigo and read this post this afternoon by chance.
As I do not develop on Android, I didn't even know that my api was not compatible with android.
So after I read this post, I made some changes on my API : I replaced the old Apache HttpClient version with a newest one : 4.1 (I think it's compatible with android isn't it ?)
And I removed all JDom references. The code now uses org.w3c.dom.Document instead. The jar is here : http://maven.le-guevel.com/artifactory/libs-release-local/fr/mael/jiwigo/jiwigo-ws-api/0.2b/
As I said, I just made the modifications this afternoon, and I did not test it very well, so it could be unstable (it seems to work on my project).
Bye.
Any ideas?
SessionManager is using a class or method that does not exist in Android. Assuming this is the source for the class in question, there is no way that will work on Android without substantial modifications, as it uses:
an old version of Apache HttpClient
JDOM
neither of which are in Android.
I had a similar issue with my projects after doing the Android ADT , and Java updates.
Try these steps
Remove links to your external libraries in your Android project.
Move all your external libraries (including Google Ad libraries) to \libs folder inside your Android project.
Re-link your libraries, that now exist in the Project\libs directory.
Hopefully this will help