How to have Android Studio generate R.java? - java

Today I've been struggling to get my Android Application to work in Android Studio. I've imported the project from an Eclipse project. However, the wrong R.java file is created.
Everywhere in the code the R is red: Cannot resolve symbol "R". And when hovering the import statement (import com.example.myproject.R;): Unused import statement. I do not know where the R.java is created or should be located, but here's my guess: $MODULE$/build/generated/source/r/debug/com/example/myproject/R.java.
That is, because the $MODULE/build/generated/source/r/release/com/example/myproject/R.java is being generated, but not the /debug/ version. If I copy the /release/ R.java file to the /debug/com/example/myproject/ directory, all errors solve themselves. But how can I make Android Studio to always generated the /debug/ R.java file?
The myproject.iml file shows:
<facet type="android" name="Android">
<configuration>
<option name="SELECTED_BUILD_VARIANT" value="debug" />
.... etc..
So why is the R.java in the release folder generated?

In my case, the
<facet type="java-gradle" name="Java-Gradle">
part is strangely missing in my .iml file
...
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":listpopupmenu" />
</configuration>
</facet>
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
<facet type="android" name="Android">
...
after adding that part, it works as expected.

Related

IntelliJ doesn't create jar properly

when I try to build my .jar (as described here) I get a MANIFEST.MF and a xml file inside the artifacts folder that's in the .idea folder. However I have no artifact folder with a .jar file inside the out folder. That's where the .jar file should be. This build process had worked earlier and I have no idea what I messed up.
I really need to build a jar and I used the IntelliJ GUI creator so I can't simply build a jar with an other program because it uses some compiled classes from a IntelliJ library, can I?
The xml file inside the artifacts folder:
<component name="ArtifactManager">
<artifact type="jar" name="Zimmerbelegung_BwInf36:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Zimmerbelegung_BwInf36_jar</output-path>
<root id="archive" name="Zimmerbelegung_BwInf36.jar">
<element id="directory" name="META-INF">
<element id="file-copy" path="$PROJECT_DIR$/META-INF/MANIFEST.MF" />
</element>
<element id="module-output" name="Zimmerbelegung_BwInf36" />
</root>
</artifact>
</component>
My project structure looks like this:
IntelliJ didn't automatically build the artifact.
So I had to build it manually: (Build > Build Artifacts...)

Intellij -Project structure settings (spring facet config) - where are they stored?

Have a project that I am migrating over from netbeans to intellij.
Intellij recognises the spring config files and gives the warning "please configure/setup spring facet for modules". This requires manually adding these under project structure settings.
I don't want the other team members to all have to do this manually, so I wish to check these settings into git, but according to that, there are no changed files in the project directory (I have standard ignores added to .gitignore). I have also tried searches of file contents for the config file names etc and that turns up nothing too.
So my question is where are the settings stored? In a file that is typically on the ignored list or in a location external to the project directory?
Essentially all the IDEA related configuration for the project is under .idea folder, for example, all the project workspace iteams you can find in the .idea/worksapce.xml, include the spring features you mentioned.
But this is not a good idea to keep those things in your VCs, the .idea folder is ignore default as in the configuration items will store you local env related like the full path to your local gradle, e.g.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="C:/DevTools/gradle-2.14.1" />
<option name="gradleJvm" value="#JAVA_HOME" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
<option name="useAutoImport" value="true" />
<option name="myModules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>
Here are some files under the .idea folder for my proejct
Update:
For the spring facets, it's under the module .iml file, like
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration>
<fileset id="fileset" name="Spring Application Context" removed="false">
<file>file://$MODULE_DIR$/src/main/resources/META-INF/spring/integration-beans.xml</file>
</fileset>
</configuration>
</facet>
</component>

How to integrate Amazon Game Circle using Ant?

I have an Android game that is compiled using Ant and I try to integrate it
with Amazon Game Circle. My project and GameCircleSDK look similar in
structure: both contain jni/, libs/, res/ and other standard directories
for Android. To integrate Game Circle, Amazon's documentation only touches Eclipse and instructs to
include Game Circle in the workspace and add it as a dependency.
What is the equivalent for this in Ant? I would like to keep Game Circle out
of my source tree and still compile against it, get all JARs and resources.
The build.xml and the rest of the build files I currently use are the default ones generated by android create project. I'm posting it here for reference (stripped down the default comments):
<?xml version="1.0" encoding="UTF-8"?>
<project name="Game" default="help">
<property file="local.properties" />
<property file="ant.properties" />
<property environment="env" />
<condition property="sdk.dir" value="${env.ANDROID_HOME}">
<isset property="env.ANDROID_HOME" />
</condition>
<loadproperties srcFile="project.properties" />
<fail
message="sdk.dir is missing."
unless="sdk.dir"
/>
<import file="custom_rules.xml" optional="true" />
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
Here's how you do it:
Add the the SDK as a reference in project.properties. The path must be relative.
android.library.reference.1=../../relative/path/to/gamecircle
Add a build.xml to GameCircle by running android update lib-project -p /path/to/gamecircle.

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

While running junit test in eclipse I am getting this Exception:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
I've added junit.jar library file.
I've tried different versions of junit.jar: 4.4, 4.8, etc.
How do I fix this Exception?
Add hamcrest-all-X.X.jar to your classpath.
Latest version as of Feb 2015 is 1.3:
http://code.google.com/p/hamcrest/downloads/detail?name=hamcrest-all-1.3.jar&can=2&q=
According to the JUnit GitHub team website (https://github.com/junit-team/junit/wiki/Download-and-Install), junit.jar and hamcrest-core.jar are both needed in the classpath when using JUnit 4.11.
Here is the Maven dependency block for including junit and hamcrest.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>
<!-- Needed by junit -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
A few steps you have to follow:
Right click on the project.
Choose Build Path Then from its menu choose Add Libraries.
Choose JUnit then click Next.
Choose JUnit4 then Finish.
Works for me: IntelliJ IDEA 13.1.1, JUnit4, Java 6
I changed the file in project path: [PROJECT_NAME].iml
Replaced:
<library>
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
By:
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-library-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
So the final .iml file is:
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-library-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
P.S.: save the file and don't let to IntelliJ Idea reload it. Just once.
You need junit-dep.jar because the junit.jar has a copy of old Hamcrest classes.
Just in case there's anyone here using netbeans and has the same problem, all you have to do is
Right click on TestLibraries
Click on Add Library
Select JUnit and click add library
Repeat the process but this time click on Hamcrest and the click add library
This should solve the problem
This problem is because of your classpath miss hamcrest-core-1.3.jar. To resolve this add hamcrest-core-1.3.jar as you add junit-4.XX.jar into your classpath.
At first, I encounter this problem too, but after I refer to the official site and add hamcrest-core-1.3.jar into classpath with command line, it works properly finally.
javac -d ../../../../bin/ -cp ~/libs/junit-4.12.jar:/home/limxtop/projects/algorithms/bin MaxHeapTest.java
java -cp ../../../../bin/:/home/limxtop/libs/junit-4.12.jar:/home/limxtop/libs/hamcrest-core-1.3.jar org.junit.runner.JUnitCore com.limxtop.heap.MaxHeapTest
You need to add the hamcrest-core JAR to the classpath as described here: https://github.com/junit-team/junit4/wiki/Download-and-Install
As a general rule, always make sure hamcrest is before any other testing libraries on the classpath, as many such libraries include hamcrest classes and may therefore conflict with the hamcrest version you're using. This will resolve most problems of the type you're describing.
the simplest way of solving the problem to begin with is copying latest version of hamcrest-code.jar into your CLASSPATH that is the file you store other .jar files needed for compilation and running of your application.
that could be e.g.: C:/ant/lib
It sounds like a classpath issue, so there are a few different ways to go about it. Where does org/hamcret/SelfDescribing come from? Is that your class or in a different jar?
Try going to your project Build Path and on the Libraries tab, add a Library. You should be able to choose JUnit to your project. This is a little bit different than just having the JUnit jar file In your project.
In your Run Configuration for the JUnit test, check the Classpath. You could probably fix this by adding making sure your Classpath can see that SelfDescribing class there. The Run option in Eclipse has a different set of options for the JUnit options.
If this problem arise in a RCP project it can be because JUnit has been explicitly imported.
Check the editor for your plugin.xml under Dependencies tab, remove the org.junit from the Imported Packages and add org.junit to the Required Plug-ins.
The problem is when you set up eclipse to point to JRE instead of JDK. JRE has junit4.jar in the lib/ext folder, but not hamcrest.jar :) So the solution is to check installed JREs in Eclipse, remove the existing one and create a new one pointing to your JDK.
This happens when you run Ant via command line. The implicit user dependencies are added in the classpath at the end and take precedence over the project-added classpath. Run Ant with -nouserlib flag. The implicit dependencies would be excluded from the classpath.
There is a better answer to solve this problem.
add dependency
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
The hamcrest-core-1.3.jar available on maven repository is deprecated.
Download working hamcrest-core-1.3.jar from official Junit4 github link .
If you want to download from maven repository, use latest hamcrest-XX.jar.
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
I had the same problem, the solution is to add in build path/plugin the jar org.hamcrest.core_1xx, you can find it in eclipse/plugins.
A few steps you have to follow:
Right click on the project.
Choose Build Path & then from its menu choose Add Libraries.
Choose JUnit then click Next.
Choose JUnit4 then Finish.
This works for me...
"java.lang.SecurityException: class" org.hamcrest.Matchers "'s signer information does not match signer information of other classes in the same package"
Do it:
Right-click on your package
click on Build Path -> Configure Build Path
Click on the Libraries tab
Remove JUnit
Apply and close
Ready.
Try adding the jar files manually or try with force update with the latest hamcrest.jar

Conditionally include resource in Android (Eclipse) based on project

I am building an Android app in Eclipse and have two similar apps based on the same code base, a 'London' one and a 'UK' one. To build each app, I just re-name the main package and change a static int in the Application class. The app uses the value of this int to display the right UI, restrict user behaviour, etc.
I have two icon files, one for each app:
res/drawable-hdpi/icon_london.png
res/drawable-hdpi/icon_uk.png
Is there any way to conditionally use the correct icon file for the Application and Activities depending upon some sort of project configuration setting? Otherwise, the project maintenance is increased as I would have to change my manifest each time the code base changes, e.g.
<application
android:icon="#drawable/icon_london"
...
>
<activity
android:name="com.company.MainActivity"
android:logo="#drawable/icon_london"
>
</activity>
...and so on, for all the activities.
I had a similar issue, and ended up in building my custon Ant script to build the apps. You can run macros or Regular Expressions to assign one resource or another.
EDIT:
First, add build.xml to the project:
Open up a command prompt and navigate to the directory of your project:
android update project --path
Then, you can override the existing build.xml, for something like the following.
NOTE: This Ant script is just an example, I have not tested it:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Example" default="help">
<property file="local.properties" />
<property file="ant.properties" />
<loadproperties srcFile="project.properties" />
<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
unless="sdk.dir"/>
<!-- IMPORT ANT?S BUILD.XML -->
<import file="${sdk.dir}/tools/ant/build.xml" />
<property name="app.icon" value="${icon}" />
<property name="icon.file" location="res/drawable/icon.png" />
<target name="test-release">
<antcall target="test-pre-release" />
<antcall target="release" />
</target>
<target name="test-pre-release">
<copy file="${app.icon}" tofile="${icon.file}" overwrite="true"/>
</target>
</project>
Then, to build this project with a custom icon, open up the command prompt and go to the project directory:
CALL ant -f build.xml test-release -Dicon=path/to/your/icon.png
As said, this is a very basic example. To build a good script, you'll have to learn a bit of Ant syntax, but it's not difficult.

Categories