I'm building a small game application. I've build the whole engine using java and seperated the engine from the logic, and it worked perfectly fine as a console and a swing-UI application.
now I want to migrate it into android and for some reason my emulator crashes each time I try to create any instance from the package I've build.
I added it to the project as a jar file (and added it to my path). afterwards I added the *.java package to the android project and it still crashes.
your help would be much appreciated.
There may be a million reasons for this. Most likely reasons are:
You compile your code to Java 7 (you have different class format)
You use a dependency that is not present in the Android environment
Try to add your source to the source folder of your Android project, and let the Android environment compile it to you. Do not use the jar file. That will work for sure.
Related
I am getting started with Jena and semantic technologies (I am taking a class on the topic). The lecturer recommends using Eclipse as IDE, but I would like to use Visual Studio Code. How can I import apache.jena? I would like to get information/error messages as you would with any other library.
I have had amongst VSCode's extensions, but did not find any support.
I include "import org.apache.jena.rdf.model.;" and get a "not found" error when I try to compile - unless I also include "-cp "/path/to/apacha/jena/on/my/computer/:." when compiling and running the program.
The goal is to be able to compile and run without having to include a path to the library and if possible, for VSCode to have an understand of the library (giving me warnings, suggestions, error messages osv.)
Use Java in VSCODE. You need to install the Extension Pack for Java, and the documentation contains more information.
Use VSCode to open the Java project folder, expand the project structure and display it on the JAVA PROJECTS panel.
Click the plus icon next to Referenced Libraries to add a .jar file reference. Of course you need to download the Jena library on your machine first.
Or use the following configuration in setting to add reference.
"java.project.referencedLibraries": [
"/path/to/jena/lib/*.jar"
]
I am now coding Android apps without using Android Studio, and when I compile, I encountered the errors:
./src/com/example/projectname/GamePanel.java:8: error: cannot find symbol
import androidx.annotation.NonNull;
where the "^" points at the period before "NonNull", and
error: package androidx.appcompat.app does not exist
And my compile command is:
javac -d obj -sourcepath src -classpath ../android/platforms/android-32/android.jar ./src/com/example/projectname/*.java
The internet tells me that I should set android.useAndroidX to true, but I am not using Android Studio. Is there any way to let javac know that I am using AndroidX?
OK, so the problem is that the NonNull annotation is not defined in that JAR file. (Or at least, not in the android.jar that I found ... on Github.)
I managed to find a JAR containing androidx.annotation.NonNull in the Maven Central Repository: https://mvnrepository.com/artifact/androidx.annotation/annotation/1.3.0
But I suspect that you are going to find more of these issues as your project gets more complicated.
Is there any way to let javac know that I am using AndroidX?
No there isn't. The javac compiler understands nothing about Android let alone AndroidX. It only knows about what you added to the compile-time classpath. So if you are going to persist with compiling using javac directly, you are going to have to figure out how to find the JARs that you need for yourself1.
My recommendation would be:
Just use Android Studio. It really isn't that slow ... when you take into account all of the features it provides to make coding, testing, debugging, etc.
You can also use the Gradle build tool2 independently of Android Studio. It has a plugin designed especially for building Android apps; see https://developer.android.com/studio/build. Among other things, it will automate the downloading of the dependencies and the configuration of the classpath.
1 - And down the track you will need to manually update the JARs that you manually downloaded, etc.
2 - There are alternatives such as Maven, Ant and so on, but AFAIK Gradle has the best integration for Android.
Is there any way to let javac know that I am using AndroidX?
Yes, you can download the JAR files for the libraries you are using and add them to the classpath.
However, this leads down a path of suffering and sadness because you will have to download many different JAR files that are implement different parts the androidx package, depending on what features you need.
I strongly suggest you to use Android Studio. It is an incredible tool that makes Android development easier than it otherwise would be.
If your goal here is to compile your app from the command line, then you need to learn about gradle. This is a tool that will download the dependencies for you then run javac with the correct classpath set. Basically it automates all the steps you would need to take to do this manually.
I have a legacy app with 168 modules in it and I would like to break it up into several java packages from:
com.mycompany.ediary
to
com.mycompany.ediary.util
com.mycompany.ediary.data
com.mycompany.ediary.services
...etc.
This would be to better organize the code. I read where it is ideal to have less than 30 files per package and as you can see, this is way over this. What is the best way to do this? I am currently running Android Studio 1.5
As #Antoniossss says "Just do it!".
Following a slight explanation...
In the older Eclipse days there was a link between the Java packages, and the Android manifest package (now referred to as the "application ID") in the app. This was enforced by Eclipse (almost) requiring that you put your code into a Java package that was named the same as the application ID.
Changing the root (Java) package of your code in Eclipse got a bit tricky, and generally you would want to do that by using the Android Tools menu. This made sure that the R class was correctly imported after the change.
Android Studio makes it much easier to separate the 2 concepts. Plus the terminology makes it easier now, since the accepted term "Android manifest package" is now the "application ID".
This gets set in your gradle build file:
applicationId "mobi.glowworm.demo"
How you organise your Java code does not depend on this value at all anymore. You can use any package structure you want to.
So, yes, "Just do it!"
It wasn't as easy as I thought in Android Studio, yet easier than I thought at the same time.
Adding new packages is basically adding new directories to your project structure, by adding new "packages" and letting Android Studio do the work of refactoring it all in your code. I did have some collisions with the manifest-merger-release-report.txt file, but after manually fixing those and updating my manifest as well, I was able to break up the files into a more organized format. I didn't have to touch the gradle files at all.
Right click on the java folder in your app, select New -> package - then enter in the full package name: com.mycompany.ediary.utils. Then cut and paste the files you want to add to the new directory/package, when prompted for reformatting, hit "reformat", then allow Android studio to do its magic.
I'm using eclipse ADT (most recent version) to develop some android apps. I've followed the getting started tutorial at the dev.android.com and it worked just fine.
But when i tried to create another project with the same targets and sdk's, i've got nothing but errors:
Some errors and my sdk's:
That's the same sdk's i used before these errors started showing up.
What can be causing this?
EDIT: my project won't generate the R.java file
You might need to add a support library to your new project.
Check this link for downloading the support libraries -
http://developer.android.com/tools/support-library/setup.html#download
Please notice that you need to import them into your eclipse and then include one (preferably the newest, not sure about this) in your project.
EDIT:
Before downloading the support libraries you should first check if you have them already.
Try this, right click project folder in the eclipse package explorer, Properties>Android>Library>Add>select a library if you have one.
There might be an invalid library included in your project already, if that happens then you will see it in the Library>Add> menu, remove it and add a new one.
You might have clean your project ........
if yes.....you must check whether R.java file is present in **Project(your project)->gen->package name->R.java**
if not than u have to update Tool Folder in SDK manager
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NoClassDefFoundError - Eclipse and Android
I'm seeing this question is getting asked a lot in many different contexts. Perhaps we can set some strategies for locating and fixing it? I'm noobish myself so all I can contribute are horror stories and questions, sorry...
It seems this is thrown when a class is visible at compile time but not at run time... how can this happen?
In my case I am developing an app that uses the Google APIs, in Eclipse, for the Android platform. I've configured the Project Properties / Java Build Path / Libraries to include the gdata .jars and all is well. When I execute in the emulator I get a force close and the logcat shows a NoClassDefFoundError on a simple new ContactsService("myApp"); I've also tried a new CalendarService("myApp") with the same results.
Is it possible or desirable to statically bind at compile time to avoid the problem?
How could dynamic binding of an add-on library work in the mobile environment anyway? Either it has to be bound into my .apk or else I need to "install" it? ... hmmm.
Advice much appreciated.
It seems this is thrown when a class
is visible at compile time but not at
run time... how can this happen?
The build classpath may include JARs that are not being packaged into the APK.
Is it possible or desirable to
statically bind at compile time to
avoid the problem?
It is possible, desirable, and necessary.
Outside of Eclipse, you just put the JARs you need in libs/ in your project, compile with Ant, and you are done.
Inside of Eclipse, one pattern I have had students use with success is to put the JARs you need in libs/ in your project, add them as JARs to the build path (note: not external JARs), and they get packaged as part of the APK. Note, though, that I do not personally use Eclipse, and so my experience with it is limited.
For those having problem I was having the same error with my app. what I did to solve that was create a new project and copy my resource and source folders along with my manifest file into the new project (I deleted in advance those within the new project created) and voila.
When I got this, the problem was actually deeper in the queue; Dalvik converter had failed to convert some of the referenced libraries and still Eclipse allowed me to launch the project.
Check the Android SDK console to see if there are any errors reported.
In my case, I'm using my own library (MyLib) shared between 2 apps. App A was closed when I added a new class to the library.
When I opened App A to work on it, Eclipse recognised the new class, and I was able to reference it. However on running I got the error.
It turned out that the imported library folder in App A (named something like MyLib_src) didn't reflect the changes made to my library project (MyLib).
To solve this I refreshed App A, the changes reflected, and Android could build my project correctly.
I have found no reference to this version of the problem, so thought I would add it to this list.