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.
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"
]
In normal Java, a programmer is able to inquire with String tmpPath = aClass.getProtectionDomain().getCodeSource().getLocation().getPath(); to determine the source jar for a class.
In Android's version of Java, this has been disabled, as indicated in getClass().getProtectionDomain().getCodeSource().getLocation().getPath() Throw a null pointer exception, which indicates, and I've confirmed aClass.getProtectionDomain() always returns null.
Although traditional jar files are not a part of the Android build process, information as to what packages and what versions are being used can often be helpful to the programmer.
How can the programmer learn the equivalent information (about the sources and versions of dependencies) in Android as they can in other Java environments?
The approach below, using a gradle dependencies listing, does not give the programmer access to the dependencies at runtime, as does the approach with other Java environments that use jar files, but should be a reflection of what dependencies and versions are in effect during runtime.
One way to get a listing of dependencies is the following:
Android Studio
Terminal
Typed Gradle Command (Windows)
gradelw -q app:dependencies
The above command will generate "too much" output, containing much more than the runtime classpath. It's possible to look through the output and find the portion of interest. In my case, it was: releaseRuntimeClasspath - Resolved configuration for runtime for variant: release, so the way to get only the target output is using the command:
gradlew -q app:dependencies --configuration releaseRuntimeClasspath
I want to program in Java or other JVM languages like Scala, Kotlin, or Groovy. When I am programming on my projects, I only want to have import statements in my Java/Scala/Kotlin source files without the need to state the packages a second time in a Gradle/Maven build script. Instead I want to work as I would do in Python, i.e. have my import statements at the beginning of my source files and I am done.
The packages should then automatically included when I am compiling if all packages are installed in a central local package management system or otherwise get an error message telling me that I have to install a missing package. It should essentially work the same as for Python and PIP respectively.
Is a workflow like this possible preferably with Groovy or Maven?
Thanks in advance!
The closest I can think is Grape:
Grape is a JAR dependency manager embedded into Groovy. Grape lets you quickly add maven repository dependencies to your classpath, making scripting even easier. The simplest use is as simple as adding an annotation to your script:
#Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate
Like in this example:
#Grab('net.sourceforge.nekohtml:nekohtml:1.9.16')
def parser = new org.cyberneko.html.parsers.SAXParser()
def page = new XmlParser(parser).parse('https://news.google.com/nwshp?hl=zh-TW&tab=wn')
page.depthFirst().DIV.grep{ it.'#class'=='title' }.each {
println it.A.SPAN.text()
}
I can't judge the Groovy landscape since I don't have any experience there, but for developing Java or Scala applications this exact workflow is not possible as far as I know.
Regarding "I only want to have import statements..." I think the closest you can get is good Maven/Gradle/sbt integration in an IDE, like IntelliJ that automatically adds the desired library to your build system's configuration file when using the correct shortcut. It (at least) works for some Java libraries when you're dealing with a Maven project in IntelliJ.
And regarding your other wish to have packages automatically included when compiling: On the JVM there is the concept of the fat JAR (also called uber JAR), which is basically a JAR (Java archive) that contains all dependencies and is thus self-contained. Usually you can start the application contained in it with a single java command.
To build fat JARs you need to have the approriate plugin for your build system:
For Maven that would be the maven shade plugin, see https://maven.apache.org/plugins/maven-shade-plugin/
For sbt you can use the assembly plugin, see https://github.com/sbt/sbt-assembly
Gradle probably has something similar
A lot of Java frameworks also come with their own build plugins that make building such self-contained applications relatively simple (Spring Boot is one example, but only suitable for applications that on top of an HTTP server)
Hope this helps, althought it's not an accurate answer to your question. :)
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.
In Eclipse, using the CCW plug-in, I want to load a clojure file into a REPL. The problem is that I have an import statement for one of my own java classes, but apparently it is not in my classpath.
(ns my-clj-ns
(:import [alg.gen Enumerator]))
Do I have to make jars out of every class that I want use/test in a Clojure REPL?
Currently, trying to load my clj into a REPL results in an error:
"Load file in Clojure REPL" did not complete normally. Please see the log for more information.
java.lang.NullPointerException
Any help would be greatly appreciated.
You can let leiningen compile these for you using,
:javac-options {:destdir "classes/"}
:java-source-path "src/main/java" ; location of Java source
options or manually compile them and move the class files to the classes/ directory. No need to create a jar.
When you're in the ccw repl, you can hit alt-e to see the stack trace. If you're getting a NullPointerException, I don't think its a classpath issue.
Your code looks fine to me.
I suspect the issue is with your Eclipse Java Build Path, which determines what Eclipse includes in the classpath for your application.
In particular, if your Java class is in a separate project, you will need to either add that project to the build path (right click on project / Properties / Java Build Path / Projects) or package it as a jar.
When you start to have more sophisticated build requirements, you may also want to start looking at Maven to handle this kind of thing for you. Maven is a pain to learn / set up in the first place but it pays of in the long run.
Leiningen is also a great tool to use but I personally don't use it for the following reasons:
It is great on the command line, but doesn't integrate so nicely with an Eclipse workflow
Maven is more widely used and better supported in the Java world
There is really nice guide if you want to learn how to do this.
https://github.com/technomancy/leiningen/blob/master/doc/MIXED_PROJECTS.md
But in gist, have a project definition like the following for Java source code.
(defproject megacorp/superservice "1.0.0-SNAPSHOT"
:description "A Clojure project with a little bit of Java sprinkled here and there"
:source-paths ["src/clojure"]
:java-source-paths ["src/java"])