Use Android objects outside Android - java

In a Java project I am using a class that is in an Android project and uses an android View object. When I try and use that class in the non-android project I get a no class found error at runtime. How can I import android.widget.view.View into a non-android project? Can I add it to my project build?

Without the android platform runtime, ie.. DalvikVM, etc... you will not be able to run any of the framework necessary to render "Views" in your Java project. Did you think of that?. But to get it coded you would at least need to import Android.jar into your Java project build path. It will likely find the Widget package, but the Widget package will be looking for the Android SDK, etc...
What does android.widget.View give you outside of the Android environment??

Related

Unable to import java.lang.instrument in Android Studio

I am trying to import the Java Instrumentation library (java.lang.instrument) https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html in my Android Studio project but it is complaining that it isn't able to find it.
From what I understand, this library should be built in to Java. While typing in java.lang, I do see existing classes under that package.
Does anyone know how I can use this specific class in Android Studio?
I have tried looking into the Gradle file where dependencies are declared but I am confused as java.lang is built into Java.
I was expecting the java.lang package in Android Studio to contain the Instrument class.

I can't use methods from another class from imported libraries in Android Studio

I used
dependencies {
compile 'com.macroyau:thingspeakandroid:0.2.0'
}
to download the external libraries I need. I created an instance of one of the classes of the lib and upon calling the methods, it seems I can only call the interfaces instead of the actual methods as you can see
here!
It's the first time I am using Android Studio, is it the right way to import external libraries?
Properly add the library by adding new module in android studio
File->New->New Module->Android Library
Choose the path where you downloaded the library.
Then add the dependency and rebuild it.
Check the errors if any

How to add exisiting java source project to Android Studio

I have an existing git repository of pure Java code that I would like to add to my Android project.
I don't want to change any of the existing files. I want to keep my existing package names to simplify the maintenance of my legacy code.
In Eclipse, you can simply add the root of the existing project to your Java Build Path.
How to do the same in Android Studio?
I'm not sure this will help or not. But you may check these.
https://developer.android.com/sdk/installing/migrate.html
Unable to import Eclipse project to Android Studio
How to import eclipse project from git to Android Studio?
My solution to this was to Export my Java project to a JAR.
And then, I added the JAR to the Android Studio project.
File > Project Structure leads to this window:
That way, I could use my files untouched as such.
I am not so happy with this solution because it breaks my revision control system.

Using Android Library on a Regular Java Project

I am looking to use an android class on a regular non-android java project. Specifically I want to use FaceDetector from android.media
I know that there are OpenCV and other libraries I can use to detect a face in an image but I am looking to use androids library. Is there a way to link androids library to a java project?
Thanks
Go to build path of your java project
Add a new external jar
Browse to the location of your android platform installation
(usually C:\Program Files (x86)\Android)
Under android-sdk\platforms\android-x (where x is the version of android you have) select the android.jar file to add to the project.
Now you have the android library under the referenced libraries, and you can use whatever android classes you want in your java project.
Another way of doing that is to use the android source, get only the java files you need from the android library and put them into your java project. The good thing when you do it this way is that you can modify the code and make it more compatible with your java work.
The android library might contain some calls to the system API's. Look for them before including the library with the above mentioned steps. I would recommend you to look into the documentation of the android library and look for the hooks where it calls android system APIs.Modify the source to use your webcam's API and then include it as a library

Adding Non-Android Project to an Android Project

I have three projects in Eclipse: Base, Server, and AndroidClient. The Base and Server are Java 1.7 projects, while the AndroidClient is obviously an android project. The base project has classes and files that are shared between the server and android client.
I understand that the Android doesn't use all of the normal Java classes and there's some deal about the Dalvik being different enough from the JVM that you have to compile them separately. Assuming I'm careful not to use any java classes that android doesn't support in the base project, is there any way to include the base project as a library to the android client in a simple way?
Figured it out. The answer is to open up your Android projects Properties, Build Path, and click "Link Source...". Add the source directory for your base project. Now, your android project will build those files for the android as well.

Categories