Using parts of android lib in non-android project - java

I am currently attempting to port an android app to desktop. I realize that the majority of the android based code cannot be reused, but what about "smaller parts" which dont depend on the android architecture?
I am mostly talking about support or math related classes like SparseArray, Matrix and so on.

Those may work. But you will have a rough time figuring out what works and what doesn't. Even once you do that you will need to create a whitelist somewhere like in your IDE that doesn't auto-complete classes you don't want or can't use and you will also want to fail your build. Maybe you should create a jar containing only the classes you want to use that you determine are usable.

Related

How to emulate an Android type file system in a Java desktop application?

I have had some experience programming in Java and a little with Java for Android. I haven't done much with Android but enough for me to gain respect for the standard file system within the project. I like how all the resources are contained separately in XML files instead of hard coded into the application.
My question is: Is there a way to emulate this same system when making a project designed to run as a desktop application?
I know I could just code it myself, including the XML files in the jar, set up a separate class to read them, and reference that class when using the resources, but is there a built in way to do this?
I think what you need is just a key-value pair system. JSON is quite enough to fulfill your requirements.
How to parse JSON in Java
I don't think so..
The xml files are compiled, in a certain way that its better to find another approach.
You don't want to write the android compiler from scratch.

Java Library in Android Project

I want to make a java library (so I can compile it into a jar) that makes calls to a server. I then want to share this amongst android application projects so that I can access those simple calls.
Is using HTTPRequest the best way to do this?
http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html
Something about making web calls in the java way and sharing them in android apps seems odd to me. should this be an Android Library instead? (I believe those should only be used when resources are involved)
Sorry for the potentially simple question.
You can develop a common Java library that you can use with both android , and regular java projects.
Take a look at OkHttp , it shows you how to achieve this. It is related to HTTP client, may be it already provides everything you need. You can extend it where needed.
For a simpler example (~20 java classes), take look at Okio , the underlying io library used by OkHttp.

Create Android library jar to work with non-Android project

I am working on a library that ideally will have a strictly-Java component and added functionality for Android-specific projects, with the intent to be most useful for Android apps, but also work with other Java apps.
But my question is: how should this be designed? I do not plan on needing resources, so I want to compile it into a JAR, but would I need to make two JARs, one of the Java stuff and another for the Android stuff? Or would a Java-only application be able to use a single JAR so long as it does not use the Android components?
If you make a library that uses pure java and does not use any android apis. It will work on both standard java and android java. However if the library uses any android apis it can't be used in a standard java project.
As far as I know, the JAR would be good for both types of aplications. It seems to mee that both JARs (just Java and android) are totally identical and thus equally compatible. If you don't use any of the android components, including Resources there should not be a problem at all.
You should still check whether you depend on libraries which are available on android and any normal Java distribution or -if not- either tell the user to preinstall the depending libraries or ship them whitin your JAR package and build path. Be carefull not to use libraries which are not available on android because the user has no or really few options to install them on himself.
What I don't understand is why you think your library is more usefull to android developers. I can not think of any example where this could happen. If the problem is really specific for android, you should consider developing the library android-only. If the problem is more general the lib will be usefull to all developers that might come to this problem, not just android.

On/Off native support in project by one click

My project has next structure:
Java part for GUI
Native part(C++) for logic
they are connected by socket.
So, in development process I run native part on desktop computer and java part in android device. They connect by WiFi and it is very comfortable to debug every part.
But sometimes I need build full and all-sufficient .apk where native part included in it as .so libs and calling from java code.
For this reason I try to find a handy way to "ON and OFF" native support in project. Now I see only one way - comment/uncomment all in Android.mk(it is important to prevent including .so libs in apk because they are big) and comment/uncomment call of native finction in java.
But I don't like it...
Thank you!
What you describe looks like two different projects that share some (maybe much of) common code. You can easily have such setup in Eclipse, you can use linked folders to make source sharing between the two projects automatic. But it may be safer to use your source control's powers to maintain sync between sources for the two projects.

Create Android apps in Eclipse sharing common library

is it possible to create java libraries the apps can use shared?
It is totally clear to me how to create a library project and how to use it while shared between several another projects, compile each project into different apps.
But in this case the library gets compiled into each app separated.
What I want is to create a library, compile it, install it into the device (or emulator) and several apps calling into this library.
And when it is needed to change something the apps use shard in this library, I just re-work the library, re-compile it, replace it on device (or emulator), and the apps use the new library, all apps use the new functions.
Is it possible?
I googled it, but I couldn't find the solution.
I am using Win + Eclipse, I can't use native code (since I know only Java).
Thanks
UPDATE: Thank you for your suggestions, I know about using Services and Activities started explicitly in order to share functionality.
I asked about libraries because I am investigating the possibilites. If there is no way to use common library, what is the purpose of uses-library... in the app manifest?
Thank you
It's more likely that you need a service as a library. Have you considered this possibility? You can create it as a separate application, define an interface for your service and use it in other apps. So as the interface does not change this will not influence on other apps that depends on this service.
The Android model installs each application as a separate user (UID) on the device and the users have no access to other application's/user's files. Therefore, you can't share libraries as such.
As Yury suggested, you might need a service or an activity that can be invoked from multiple applications.

Categories