How do I reference adk resource in a different Android project? - java

I am developing a study project using the mosembro (mobile semantic browser) project inside my Android project. I am using Eclipse and I created my project, downloaded / imported mosembro and included in my project's build path.
So now my code needs to refer to a resource in the mosembro's project. I want to do something like this:
String commonJS = getScript(R.raw.common);
In this snippet, I want to load a javascript file from mosembro's project: res -> raw -> common.js so I need the java syntax to get "R" (precompiled resource-class) of Mosembro from my HelloGoogle project.
What is the syntax to replace the R-class of this with the included project's R-class.
Is that clear?

I found the answer and it is very simple. You can add projects to your project through the Java Build Path.
Right click on the project you are working on
Select Build Path -> Configure Build Path ...
Then pick the Projects tab
Finally, press the Add... button and select the project that you want to include.
This keeps everything in code-sync if you make changes rather than cut-n'-paste.

Related

How to create a JavaFX Maven project in IntelliJ IDEA?

How can I open a JavaFX Maven project from scratch in IntelliJ IDEA? As there is no difference between a Java project and a JavaFx project, I want to open a dedicated JavaFX project (Selection JavaFX from create project Wizard) with Maven module included.
Although dated I'm answering this because I had the same question recently and too many people told me to write my own pom file etc. While that is technically true, it can create more headaches if you aren't careful.
I recommend you:
Create a JavaFX project as you normally would.
Make sure that project view (dropdown in project structure side tool window) is set to 'Project' or 'Packages' (otherwise option in 4th step will not be visible)
Then once it is opened and ready right click on the project folder
Go to "Add Framework Support...".
Check the box for Maven and click "OK".
Let IntelliJ do the work for you at this point.
As for editing FXML files link the IDEA to SceneBuilder. Here is the official documentation. But basically:
Install Scene Builder.
File -> Settings -> Languages & Frameworks -> JavaFX
Point to the exe file.
To Use: Right click on the FXML file and select "Open In SceneBuilder" (way down at the bottom)
And after all, a program doesn't compiled. So this video helped me: Configuring Maven For Java FX
in words:
we need to move sample.fxml to /resources folder. And in Main.java write:
FXMLLoader loader=new FXMLLoader(getClass().getResource("/sample.fxml"));
This steps worked for me (Is a different process):
1. Create a maven project.
2. In the generated pom.xml add the following:
`<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.7.0</version>
<configuration>
<mainClass>your.package.with.mainclass</mainClass>
</configuration>
</plugin>
</plugins>
</build>`
This adds a maven plugin for javafx (more info: https://github.com/javafx-maven-plugin/javafx-maven-plugin)
Create in your resources folder your sample.fxml
Create your controller class for sample.fxml and link
Enjoy!
It takes me a couple of hours to find the right solution.
Many thanks to this video and the author:
https://www.youtube.com/watch?v=qn2tbftFjno
So, following the above video, I would like to recommend:
Download JavaFX SDK from https://gluonhq.com/products/javafx/
Extract the zip file
From the starting page of Intellij IDEA, select Configure/Structure for New Projects
Select Platform Settings/Global Libraries
Select the plus ("+") button/Java
Find the "lib" folder inside your extracted zip file and select all of ".jar" files (except "src.zip"), and then OK
Set a new name for the library, let's say "javafx-whatever-version", then
Apply/OK
Create a JavaFX project as normally.
Select File/Project Structure...
From Project Settings/Project, remember to select the suitable Project SDK and Project language level
From Platform Settings/Global Libraries, right click on the library "javafx-whatever-version" (which you have already added),
choose Add to modules.../OK
Switch to Project Settings/Modules, choose Apply/OK
Back to your project manager, right click on "src"/New/module-info.java
Add new lines to the body of module-info.java:
requires javafx.fxml;
requires javafx.controls;
opens sample;
Run the project and enjoy!
For anyone coming here. I think the best answer lies on the openjfx docs. It contains a separate section for JavaFX & InteliJ, select the Non modular with Maven section.
Ill describe the steps in breif.
Create new Maven project.
Select create from archtype.
Add new archtype ( groupid : org.openjfx , artifactId : javafx-maven-archetypes , version 0.0.1 )
Select the created archtype and Next.
Provide group id org.openjfx ArtifactId hellojavafx and Next.
Change the property archtypeArtifactId value to javafx-archetype-fxml.
Add new property for javafx version : property javafx-version and value : 14.
Finally give a name to project like HelloJavaFX.
Now click finish and wait for inteliJ to build the project.
Once InteliJ finish its work the pom should look like this sample.
PS :
Two more additional tips you need to follow if you follow this approach to get things going smoothly.
JavaFX 14 needs java 11 or newer.
Change the javax maven plugin version to 0.0.1 to 0.0.4 to avoid errors like invalid flag --module-path.
Also to work better Download and install the
If you have already installed Maven, here is the exact user guide below to create a javafx project:
Open intellij
Create javafx project in folder with other maven projects.
Click on folder (project at the top) in intellij and choose run maven and package.
Then if you need plugins for javafx click again on project folder, run maven, plugins and you choose. I needed: javafx: jlink.

referencing (pure java) project in Android project (Could not find class)

I am trying to reference a pure java-Project in my Android-Project -> The Java-Project has a whole bunch of classes I need to use. Oh, and as the first response pointed out: I am using eclipse, yes :)
Only Problem is: I Always get Could not find class 'XXX', referenced from method com.example.helloworld.MainActivity.onCreate. I seem to have missed SOME step or error...?
What I already did:
The Project is added as Project into the Java Build Path (logically nessecary)
It is marked in "Order and Export" in the Java Build Path and pushed to the top (this solved the problem for someone else here when dealing with Jar-files)
Ir is marked in "Project References" on Project settings.
I added the folder where the relevant class is under "Libraries" in build Path... I am not sure if that should be nessecary.
The Java-Project I reference has a whole load of Jars, but if that is the reason, should I not get a different errormessage?
I had the same issue and after some hours of frustration and search I have finally found the answer here:
Android, class not found from imported jar file
Basically, the issue was that the referenced pure-java project or the generated jar was built with Java 1.7, and Android projects are set to use 1.6.
Two ways to do this.
Jar that java project and copy the jar to the libs folder of your Android project.
Add the project as a dependent project to your Android project
Both work wonderfully.
But mind you, this pure-java project must add android.jar and not things like rt.jar :). Else you will get Dalvik exceptions.
Edit :
Dont forget to refresh and Project - Clean your android project.
Two things to check (you mentioned you are using Eclipse):
If your android project needs to use the pure-java project's JARs, check that those JARs are being exported in the build path options of the pure-java project. This is in the build path of the pure-java project you are trying to include, not the build path of the android project that is trying to include it. Under "Order and Export" tab for the pure-java project, note the comment "Exported entries are contributed to dependent projects". In this case, your android project is the dependent project and your pure-java project needs to do the contributing.
Make sure the pure-java project is actually opened in the IDE in addition to your android project. Probably trivial but sometimes overlooked.
If answer does not suffice, you may wish to mention if the missing classes are in .java source files, or if they are only found in JARs in the build path of the pure-java project
Edit: To further track down the issue, please confirm the following to make sure I understood your question:
The pure-java project does successfully build in the IDE
The 'class not found' error is one you are getting when you try and -build- your android project and not when you try and -run- it.
CLASSPATH (aka build path) can be nasty to untangle. If including the project is not working (assuming it does build), you could try building a JAR of your pure-java project and copying (and including) that and all the other JARs into your android project.
The android SDK does certainly complicate the build environment. One way to find out whether the problem is (A) your android project setup) OR (B) the way the pure-java project is packaged) is to create a different pure java project and try including the first one, preferably using the -exact- same line of code that gives you the build error in the android project, if possible.

Eclipse Open declaration in Java project

In an Eclipse SVN project I have a problem; when I tried to open a declaration of one class, I got this error:
Problems opening an editor Reason : projectname does not exist.
Also refactoring does not work.
I have searched the web and tried all found solutions but nothing helped.
I have downloaded different Eclipse and Java versions and switched to a clean workspace but I still have the problem.
How can I solve my problem or which solution can I try?
Right Click on the project -> Properties -> Project Facets -> Click on the Configuration Link -> Click on Apply Button -> Click on OK button.
The above steps should set your project as a Java project.
If the project is a multi-module Maven project, then you need to import the child/module projects in addition to the parent/pom project. Then ensure that you only open source files via the child project. Java files opened this way will have the Refactor right-click menu item, and F3 Open Declaration etc. work properly. However if the Java file is opened from the parent project, these Eclipse functions will be missing.
It's quite frustrating when opening a file via a Search result, as the file will be found and listed in both the parent and child project. You need to ensure you only work with the file accessed via the child project.
I can't explain precisely what's going on with Eclipse, but it appears that Eclipse does not recognize the parent project as being Java projects, although it will correctly syntax-highlight the Java files whether they are accessed via the parent or child project.
You needed to to check it out as a Java Project, or preferably, its creator should have committed the .project and .classpath files for it.
I had this problem, too.
It looks like after a search-in-workspace Eclipse opened a wrong file.
I pressed Ctrl + Shift + H (Open Type in Hierarchy), typed in the class name, and opened it. The file opened in a new tab, and everything was navigable again.
The difference in icons is:
you can solve the problem by create a new java project.
when you check out the project,you should select the default option,
[Check out as a project configured using the new project wizard].
It might lack a builder. Check your PROJ_HOME/.project that should be like:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>the-one-user</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Ref:
FAQ How do I add a builder to a given project?
Are you importing a maven project? If you importing a maven project, a file will be viewed in few perspective, especially if you open the file via "Open Resource (Ctrl + Shift + R)".
Let's look at the example above, "servlet-filter" (mark as 1) imported as maven project, and maven perspective project created in 2. If you open a file in 1, your "Open Declaration F3" will no work, because it is not a valid maven project. It will work if you open the same file in 2.
Hope this helps.
You need to import it as a Java/Maven project. Once you are done importing the project do the following steps
Go to Project -> Select Properties -> Select Project References -> Select/Check all the
dependent/Child projects which you reference in the code base -> Apply
and Close
Once you are done, try to clean the whole project. This should solve the issue.
There is a bug in Eclipse (in Luna at least) for a very specific workflow, which might not be the answer to this specific question, but might be helpful for others.
If you perform an import into the src (meaning src folder has focus before performing import) folder from an extracted jar (as source code), and you place breakpoints in the nested *.java files in the nested packages, it won't hit those breakpoints. In order to get things to work, you must keep hitting F3 instead of using "Open Declaration" to get to the *.java file you want to set a breakpoint in, then add the breakpoint by double clicking in the far left margin. Then Run > Debug As > Java Application.
If the format of the project you checked out is of type Maven, you need to convert it to a Maven project first. It may be because the required classes haven't been generated yet. To do this, right-click on the project, click Configure, and then click Convert to Maven Project.
I had the same in Eclipse Luna on Debian 8.
I was missing the .classpath file when checking out from SVN.
I manually copied it and changed some path names, refreshed the project and it was working again.
It happens, when libraries are not available on class path of the project where F3 does not work.
Select any other project and try to use f3 for navigation. It will work.
The solution is to add JAR files in your libraries or add libraries in your project.
Try to use MAVEN project so that the problem can never occur.
I am using Eclipse Oxygen.2 (4.7.2). I don't see Project Facets after clicking project -> Properties. My solution is right click the project and click Configure, then select configure and detect nested projects, Eclipse will automatically generate the nested projects for you. May get some build errors in the new created projects, that should be easy to fix.
Go to Project properties,Select Project Facets then convert to project facets then click Ok then you will able to open editor.

Import source java project in Android project

Is it possible to import an existing Java project which is in my workspace, in an Android project which is also in my workspace? The Java project doesn't use stuff that is incompatible with Android. I could do it using a .JAR, but since I'm modifying the Java project a lot, I would like to import the whole source code and change it once in a while. Is it possible? Thanks
This is somewhat of a hack, but worked for my project:
Add both Android project and plain Java project to workspace
Go to properties for Android project -> Java Build Path -> Source
Click Link Source, in "Folder name", make sure to select the root directory of the plain Java project's src tree
Use a descriptive name for "Linked folder location", like "java_project_src".
Press Finish and compile
This setup will compile the plain-java code as Dalvik bytecode. In my case, I had pure java code with very limited dependencies, but wanted to share the pure java code with non-Android projects, instead of separately maintaining these files in every project.
I think you are talking about referencing other projects into the current project, if this is the case then you can do this by Right click on project, properties->Project Reference.
You can link another projects to your current project. Right click on your main project, choose Build Path -> Configure Build Path... In the dialog choose the Projects tab, then click Add.. button. Now choose the project you want to link and click OK. You'll now be able to use this project's sources inside your main project. Hope this helps.

Problem with linking to other project in Android Eclipse environment

I am developing an Android application as an Eclipse project that uses classes from another Eclipse Android project I already have. When I originally imported it (New Project > Android Project > From Existing Source), it had a bunch of errors, but when I did Project > Properties > Java Build Path and went to the projects tab and added the other project, the errors went away. Unfortunately, when I ran the application, I got the following error in DDMS:
java.lang.NoClassDefFoundError: [package name].Config
[stack trace]
Is there something I'm supposed to add to the manifest telling it to look at the other package? If so, where would that be added?
Thanks in advance
Found the answer. It turns out that if I add the compiled class files into a JAR file using Eclipse's export command and then link to external jars in the Java Build Path, it works without having to change the Android Manifest at all.
Try using the File -> Import...
Then go to "Existing projects into Workspace" and choose the root directory of the project.
The one where are the src,res... folders
Your second project should be a library. Open project properties, select the Android tab and check "is library".
In your main project, open its properties, select Android tab also and click on Add in the library section and add your second project.

Categories

Resources