Creating an Android Test project in Eclipse - android

How do I create an Android Test project in Eclipse in the target project's /test directory?
Note: I'm just doing my own Q+A for people according to the Stackoverflow FAQ on answering your own question.

I am using Helios with the latest current version of Android (10.0.1)
The Android Testing fundamentals recommends you
place the tests in the same project folder as your src folder. Some people have recommended that you create a new test project in its own folder so it doesn't get packaged into the apk.
I looked inside my apk and I don't think the test stuff was included (but I could be wrong). I like having it in the same directory as its more convenient for revision control - you don't need to sync two projects each time.
Assume we want to create a test suite for the MyAndroidApp project
Goto File.. New.. Other.. Android...Android Test Project
Set "Test Project Name" to MyAndroidAppTest
For Test Target, choose "An existing Android project".. Use the Browse button to select MyAndroidApp
Unselect "Use default location". This changes the location to be inside the MyAndroidApp project under the tests directory
Click Finish
When it is being created, if you get a An internal error occurred during: "Refreshing workspace". error (or other errors) just ignore it. Click OK.
-Clean the MyAndroidApp project
Even though the project is in the <workspace path>/MyAndroidApp/tests directory it will appear as a normal Eclipse project in the package explorer.
Clean the MyAndroidAppTest project.
This worked for me.
EDIT
If you upload to SVN in Eclipse (by syncing the MyAndroidApp project, not the MyAndroidAppTest project) you need to do a little more (I've just done it now).
So when you update your other working copy it will pull down the /tests directory.
The MyAndroidAppTest project does not automatically show up in the Package Explorer.
So you need to go to File.. Import...Existing Projects into Workspace....
Then select the root directory by browsing to your <workspace path>/MyAndroidApp/tests directory and click Finish.

I found here a very detailed solution to this problem. It seems to be an ADT bug.
http://jonblack.org/2012/11/24/creating-an-android-test-project-within-a-project
Still, when I import the test project back into the workspace at Step 5 I get the following error message:
java.io.FileNotFoundException: C:\projects\MyApp\com.myapp.test-test\project.properties ( The system could not finde the file specified ).
I simply closed the error dialog and the test project appeared in my workspace at the same level as my android project, but the location was correctly set to MyApp/tests.
So I wrote a test, run it and it worked.

Related

Android Studio not recognising Gradle projects

No matter what projects I import they never work - Android Studio is always flagging this is not a Gradle build project.
I select VCS -> GIT and clone the repo without any problem. I then go through the wizard with no issues (I select create project from existing sources), creating my code base with a warning, below. When i try to run throws all sorts of errors.
Migrate Project to Gradle? This project does not use the Gradle build
system. We recommend that you migrate to using the Gradle build
system. More Information about migrating to Gradle.
Don't show this message again.
When I go to the link it makes no sense whatever to me, it says to create an arbitrary Gradle file and populate it with my references (I think), which is out of my understanding. I shouldnt have to do this as it says in the repository instructions that it is in fact a Gradle-based project.
The project I am trying to clone is
https://github.com/googlesamples/android-play-location.git
Can anybody tell me specifically how to import and run the following git repo in Android studio for example?
Ok from the start (as i dont know what other way works)
Go to Git master page (it has 'download to zip' or 'clone to desktop' button). use https://github.com/googlesamples/android-play-location as a test master page if you like to ensure you have no other issues i did not.
Click 'download zip'
Unzip the project
Close the current project in Android Studio -> File/Close Projects
A popup screen like this will appear, choose Import Project (Eclipse ADT, Gradle etc.)
You will be asked to select the project, select your unzipped project within the master folder, eg basicLocation
Select 'create project from existing resources' default
dont change project name (may cause issues)
Next through 'import project -> directories'
Next through project -> libraries
Next Through import projects -> modules and module dependencies
Next through import project ->SDK
Next through import project ->manifest.xmls
Android Studio starts. Ensure the configurations dropdown has a project in it called 'app' app
Select 'sync Gradle' icon (green circle)
May get language warning, ignore or accept restart, doesnt matter
At bottom it should now say 'Gradle executing tasks' after which it will run on device or emulator :)
Short answer: File > Open > the build.gradle file you just cloned with the repo
I just wanted to add this, because I had the same problem, and even if Fearghal´s answer and comments pointed me in the right direction, I think he may have added unnecesary extra steps, i.e. Download as ZIP, Close current project, etc.
Simply put, you VCS > Git Clone, and then open the build.gradle file. That´s it
This can happen for many reasons. I typically see it when I move the directory. The easiest way I have found is to close the project and then reopen it using "Import project (Gradle, Eclipse, ADT, etc.) menu item then selecting the folder. This will force it to reimport the project and fix any metadata/configs.
What worked for me was
On Android Studio
Open the settings.gradle file
include your project
include ':myproject'
Well, as Android Studio correctly says, https://github.com/googlesamples/android-play-location is not a Gradle project.
Any subdirectory of that repository is however.
They are different projects.
So clone that repository and import one of its subdirectory in Gradle.

res\ and AndroidManifest.xml not added to my .apk file

Due to a change of computers, I had to do a new installation of Eclipse and the Android SDK. I copied over my work environment (directory) and voila, everything works fine when compiling and running my app in the emulator.
However, when creating a distribution package, the generated .apk file is just about +half the size of the previous versions+ and produces the +"There was a problem parsing the package"+ eror message when trying to install it on a phone.
On a closer check, I found that the following files are not added to the .apk file:
res/ folder
AndroidManifest.xml
resources.arsc
There are no error messages in the Error log in Eclipse and I have already disabled Proguard (just to sort it out as a possible cause). Any idea?
I'd try two things...
First, the obvious would be to open the project and then use Project -> Clean.
Second (if the above doesn't fix it), close the project then right-click on it in the Package Explorer and select Delete - this won't delete the project files (unless you select the checkbox which you obviously don't want to do). When the project is deleted go to File -> New -> Project. Select "Android Project From Existing Code" then select the project folder as the source.

Error in Android Dependencies , while integrating FB-SDK 3.0

I have tried all the threads related to errors while integrating FB-SDK but was not successful , any answers are welcomed .
My Java Compiler is set to 1.6.
I get this error while integration the Facebook SDK to my App
it is giving me the error at that marked place as
path\facebook-android-sdk-3.0.2\facebook\bin(missing)
i get this error after added this by clicking OK .
Thanks in Advance .
Before moving on, I recommend you to get latest Facebook SDK, because there are some important changes between yours (3.0.2) and 3.0.
Here's what I do to import Facebook SDK
1- Download the latest SDK, extract it to a directory, i.e. X. (~/X)
2- Open Eclipse, New Project -> Android Project From Existing Code, then browse and choose ~/X/facebook-android-sdk-3.0 as root directory
3- Select only "facebook" project and click finish
4- Right click to facebook project -> properties. Choose Compiler Compliance level as 1.6 from Java Compiler section
5- Create new android project, and set it's compliance level 1.6 as well. Also in Android section of Project Properties, Click add button, and choose facebook project (that we imported in Step 3)
6- Finally, Project -> Clean. For me this works like a charm all the time.
Have you checked if you have the facebook jar inside your fb-sdk/bin folder? If it's not there, do a clean and build procedure first in your fb-sdk folder. It will produce a facebook.jar file on your bin folder.
After that, then, you may already do another clean and build on your main project folder.

Missing AndroidManifest.xml when importing an old Android project into Eclipse

I have an Android project developed on Eclipse (GNU/Linux) that I last touched half a year ago. I am trying to import the project into Eclipse 3.6 on Windows (with ADT installed) installed using File -> Import Project in Eclipse. When the project is imported, I see the following error twice on the console:
[2010-12-10 02:17:12 -
com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper]
Unable to read C:\Program
Files\Android\android-sdk-windows\AndroidManifest.xml:
java.io.FileNotFoundException:
C:\Program
Files\Android\android-sdk-windows\AndroidManifest.xml
(The system cannot find the file
specified)
Why is Eclipse looking for AndroidManifest.xml on the Android SDK path? The file actually seems to be in the project's directory. How do I fix this problem and get the project to compile?
A simple solution is to either reimport the AndroidManifest.xml file or make a change to the file and save it. This worked for me.
If you see an error about AndroidManifest.xml, or some problems
related to an Android zip file, right click on the project and select
Android Tools > Fix Project Properties. (The project is looking in the
wrong location for the library file, this will fix it for you.)
from: http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
The way you are importing the Android project into Eclipse is wrong. The Correct way of doing is File -> New Android Project. In the Contents Tab in "New Android Project", select "Create project from existing source" and choose the Build Target. That should fix your problem.
It appears that this error is produced because Eclipse thinks the default location for new Android projects is the Android SDK path. Even if the project location is changed, the error fails to be resolved, so the trick is to change the project location before Eclipse is aware of the condition generating the error.
To circumvent this quirk I imported the old project with the following steps:
File -> New -> Android Project
Un-check 'Use default location' and browse to project root directory.
Click 'Create project from existing source'
It's important to do step (2) before (3) otherwise the error persists and prevents running the project.
I had the same problem, all of the above did not work. I cleaned the project and it worked.
Instead of using File ---> New Android Project --> "Create project from existing sources", which will result in the error your are seeing, choose "Create project from existing sample" and choose it from the drop down. This will work.
Experienced JAVA developers tend to go with the first option which is normally there and works for classic Eclipse projects. But this is not your typical project and I bet the Google developers put this special case in the wizard to accommodate the differences.
I had the same problem. I was trying to compile the JakeWharton view page sample, so I checked out the GIT in a directory in a different folder then the workspace which caused the problem.
Eclipse was maintaining 2 folders:
1. A new workspace folder was made under the workspace directory, which eclipse checks for libraries and others source code including the AndroidManifest.xml file
2. The existing folder which was not under the Root workspace
To fix the problem after importing I had to manually move the files in the new workspace folder created by eclipse.
I met this problem when using Facebook SDK, now I solve it by doing this:
Close the project;
Copy "AndroidManifest.xml" file to the project's root path;
Open the project and refresh it, it's Done!
this worked for me by the way:
I changed the project name to the exact name of the project that I am importing.
Eclipse seems somewhat fragile in its naming conventions. One of the causes for this error is a difference between the project name and the folder name in the workspace. I imported a zip file for a project named "HelloDialogs" into a workspace folder named "HelloDialog". This caused the "AndroidManifest.xml file missing" error. Once I renamed the folder correctly, everything worked fine.
Because of the multiple different answers here, I thouhgt I'd add yet another one that worked for me, as I had exactly the same issue when first working with Phonegap android dev tools.
So I found (as mentioned by Gintautas in comments to the accepted answer), that I had to create a new project using the phonegap 'create' script, then when importing the project into eclipse. the only way to get this to work successfully was if the project was originally created in some temp folder somewhere other than the place I actually want to work with it.
So I created a project in windows like this in a cmd window...
c:\phonegap\phonegap-2.6.0\lib\android\bin>create.bat c:\temp\android_boilerplates\test app.test test
Then I imported the project in eclipse like this...
File > New > Project
Within the new project wizard select: "Android Project from Existing Code"
Click "Next"
Now navigate to the temp location of your project and set that to the root directory, check the project you want and check "Copy projects into workspace" as the example below...
Click "Finish"
And that's it, Eclipse should copy all your project files into your workspace and there shouldn't be any errors in your project (fingers crossed).
Your project should look something like this...
Hope that helps, it took me an age to work out why Eclipse didn't like creating a new project from existing code when the new project was being setup in the same place as the existing code. This isn't a problem for other languages I've used, so it was a little weird, but understandable as Eclipse (I'm guessing) seems to want to overwrite certain files.
Eclipse randomly decided to make another folder instead of the one that i had specified, but doesnt have any resources or data or layout etc in it..
it has some conflicts i guess..
anyway, a noob approach to this was, copying the original file to some other location(i put it to desktop)..
now create new project-create existing project, select this one from desktop, n VOILA.. its all fine.. :D
I am a little late to this game but I caused this same problem by generating the initial application into the exact location I was going to work on it. That is to say I put it directly into my current workspace. I then did File, Import, Existing Android Code Into Workspace. The import process blanked out my android.xml file.
I tried above solutions and had no joy. I then generated the initial application into a temp directory and imported from there. This worked for me.
The only way to import a project into eclipse workspace is to create an empty project and then drag and drop all the folders and files into this project. Why is this so is beyond me.
The answer from Raunak is wrong.
I found a .classpath file in the root directory of my Android project. I opened it and updated the file paths in it, and it seemed to fix the problem for me.
I had the problem when I tried to update an old project with recent code from the SVN. I had the Manifest.xml exported to my local file system as backup and deleted it. I've then reverted my complete project to the most recent version from the SVN and then it worked :)
This is what I had to do to get this to work. Fortunately I had backups of earlier "project".
1) Import the project as usual.
2) If the errors with empty xml etc. comes up, close the project.
3) Go to the original project if you have backup.
4) Copy all of the projects root files and directories and paste it over where Eclipse expects the projects root directories and files to be.
5) Open the project and refresh. Make "Clean" if necessary and you're done!
Always make backups because Eclipse f--kups! I learned this the hard way when suddenly my Android project refused to work because of empty manifest-file.
Sometimes if you automatically fix imports in MainActivity IDE imports android.R class instead of one that will be generated for the project. Remove the import and recompile.
when importing project from another workspace add existing project in workspace. and tick to the copy projects in workspace.then check the api level and supporting library from project.properties files.then rebuilt project and clean it.. it is works for me.
2017 Solution: Much Easier and tested solution is to remove your application folder from elipse project only then Import it again and the problem will be fixed immediately!

How to start an android project with downloaded sample code

I'm pretty new to android, and just finished setup my environment and reading some tutorials. Then I got sdk samples from the web. Okay, what I wanna ask you is that is there a way to start a sample as a project in the Eclipse? I mean like clicking a project file in c# or a solution file.
Here is my folder which I unzipped the code,
\android-sdk-windows\samples\android-8\NotePad
There are three folders and one file in the folder, \res, \src, \tests, and AndroidManifest.xml
Or, do I need to make a new android project and import (or add files? maybe) them?
Thanks in advance,
yokyo
#sgarman
I don't think that's a very good way of doing it. Sadly the 'Create Project from existing sample' feature you describe functions in such a way that when it creates a project from a sample it leaves you editing the source code in the actual SDK sample itself instead of a copy of the source imported into your Eclipse workspace.
This is problematic for a number of reasons including:
Once you've edited the new project you no longer have the original sample to refer to, which is the whole point of the samples in the first place.
If you want to hack a sample in several different ways you will want several copies of the sample, but again once you've edited the original sample you no longer have the original sample to make a copy from.
If the SDK is ever patched then when you update it you may end up overwriting your code.
In short it makes far more sense to treat the SDK as a readonly reference and not treat it as a sandpit in which to do your own messing around. So imo the best way to create an Eclipse project from a sample is do take a copy of the sample and put it somewhere else.
If you want the sample to not exist in your actual Eclipse workspace directory then this is very easy. Just copy the sample to a new location and inside Eclipse with your workspace open do New->Android Project and 'Create Project From Existing Source'.
If on the other hand you want to make the project inside your Eclipse workspace directory then there is a problem which is that if you just copy the sample code inside your workspace folder and do 'New->Android Project' and 'Create Project From Existing Source' for me at least it fails with the error "Invalid project description: c:\Users\usernamme\blah\blah\projectname overlaps the location of another project projectname". I don't know why, if you create a project from sample code using 'New->Java Project' it works just fine so I suspect there is a problem with the Android Project Eclipse plugin that is causing this to fail.
There is a way to get around this which is to first copy the sample code to a temporary location on disk that is outside of the workspace directory. Then use New->Android Project and 'Create Project From Existing Source' which turns the temporary copy of the sample code into a project but leaves it orphaned outside the workspace directory. Then delete the project from the workspace (but without deleting the project from disk). Then use Import->Existing Projects Into Workspace with the 'Copy Projects Into Workspace' checkbox ticked to copy the project into the workspace directory, before finally deleting the project from the temporary location.
But ultimately I decided to structure my workspace in such a way that projects are not inside the workspace folder like this:
Eclipse Workspaces
\
Android Projects
\
Workspace
Project 1
Project 2
Java Projects
\
Workspace
Project 1
Project 2
Other Projects
\
Workspace
Project 1
Project 2
In this layout the Android Projects, Java Projects and Other Projects directories are conceptually my workspaces but in reality in each case it is the nested Workspace folder which is the actual Eclipse workspace. This enables me to keep my projects contained within their respective pseudo-workspaces while not being inside their actual Eclipse workspace folder which neatly gets around the problem of not being able to easily create Android projects if the project directory is inside the Eclipse workspace directory.
Sorry that's all a bit long winded, but it's taken me ages to work out how to get this to work efficiently and I figure it might help someone.
From eclipse go to: File > New >
Project...
Select the Android Folder and pick
Android Project
Then in the Contents section at the
top click the "Create project from
existing sample" radio button. Once
you pick a target from the bottom
the drop down will become selectable
and your good to go.
Copy the downloaded project into your workspace
i.e space allocated for android project
Open the Eclipse
Choose File---> Import ---> Android --> Existing Android code into workspace
Choose Next
Click Browse
choose the foldername (downloaded)
click finish
If you are using ADT 20 then it's quite easy. Select New > Other...>Android Sample Project > [Select the platform from which you would like to pick the sample] > You must see the available samples, select the one you wish to open,you will see that in your workspace.
This may not be a definitive answer because I'm relatively new to Android as well (and come from more of a .Net background too).
When you create a new Workspace in Eclipse, you'll see a new folder created with the name of your workspace and a .metadata folder inside of it. I don't think there's a "workspace file" per se, so you don't double click anything to bring it back up. You just open to that workspace (by telling the Eclipse dialog what path to open).
As far as importing existing projects into a workspace, copy the whole folder into your workspace and click File - Import... - General - Existing Projects into Workspace. Click the Browse button to select the Root directory and it should default to the Workspace's main folder. Click OK and the project should show up in the dialog. Select it and click Finish
The code samples don't come with eclipse project files out of the box. There are a number of ways to create them but the easiest is probably this. From eclipse (with the android plugin installed), File -> New -> Project, Android Project, Next, select the "Create project from existing source" radio button and specify the location of the existing source. The project wizard should then find your manifest and fill out the rest of the information it needs automatically.
http://developer.android.com/samples/index.html
It states here:
"Using Android Studio
Unpack the downloaded project package.
In Android Studio, chose File > Import Project and select the root folder of the unpacked project.
Android Studio may ask you to choose the type of project you are importing. If this is the case, make sure to choose Import project from external model and select the Gradle option."
I hope this helps you.

Categories

Resources