Eclipse ADT appcompat... what is it? - java

I've spent all afternoon getting absolutely nowhere with this.
I've downloaded Eclipse, downloaded the SDK, installed the updates, but every new Android project I create something's wrong.
Firstly, it would not generate the R.java file, at all, now it does but there's a separate project it's created automatically called appcompat_v7. I don't know what this is, but it's causing problems with any other new project.
This is the error a normal project produces:
The container 'Android Dependencies' references non existing library '/home/omar/workspace/appcompat_v7/bin/appcompat_v7.jar'
I have absolutely no idea how to fix this. What is causing this?
EDIT
It appear this is only with KitKat, every other API platform doesn't produce ANY source files at all.... any idea how to combat this?

It's a support library which presumably your project refers to. You will need to build it as a library project in your workspace. This process is described here Support Library Setup under Adding libraries with resources.
You will find the project you need to copy in your SDK in the folder:
\yourSDKlocation\tools\android-sdk-windows4.4\extras\android\support\v7\appcompat
(It's no use just copying a jar, you must build it as a library project.)

I had this problem when I moved a project to a different laptop. I solved it like this:
If appcompat_v7 is not available in Eclipse:
From the File menu, choose New then Project.
Next, choose Android and Android Project from existing Code, then click next
Browse to find your appcompat_v7 project folder
Make sure there’s a check mark next it in the ‘Projects to Import’ list
If the appcompat_v7 project folder is not currently in your workspace, select ‘Copy projects into workspace’, then click Finish
Now that the library project is available in your workspace, it can be added to a project:
Choose your project from the Project Explorer and open the project properties (on a mac it’s in the Project menu)
Click on Android in the left list
Next to the Library list, click the Add button
Choose appcompat_v7 from the list
You should be good to go now.

Related

Adding external library in Android studio 0.3.6

So I am fairly new to Java, and I am trying to add this library to my project. The problem is that Android studio 0.3.6 doesn't have a simple way of doing that and all the answers I searched either reference an older version of Android Studio, or describe how to import an external project (source code, not jar file).
After reading a little, I got to the conclusion that manually adding the jar file would be the best way (manual copy/paste and gradle edits) but as I said, I'm fairly new to this technology and don't know where to place the file nor what lines I need to add to the gradle files.
Can someone help me?
UPDATE 1:
I finally made the IDE recognize the .jar file (I get autocomplete and class recognition). The new problem is that I get the following error when compiling: Gradle: package com.google.gson does not exist. Here are the steps I took to import the library:
Creat a folder called libs in the main directory (src/main/libs should be the result)
Copy the .jar file in that directory
add the following line to the dependencies section in the build.gradle file in your project: compile files('libs/gson-2.2.4.jar'). It should look something like this now:
dependencies {
compile 'com.android.support:support-v13:+'
compile files('libs/gson-2.2.4.jar')
}
Recompile the project (not sure if necessary, but I did it)
Right click on the libs folder and select "Add as Library"
Since the GSON library is available in MavenCentral, there's an easy way to add it that avoids having to download an archive file and save it in your project.
Go to Project Structure > Modules > Your module name > Dependencies and click on the + button to add a new dependency. Choose Maven dependency from the list:
You'll get a dialog box where you can enter search terms or the fully-qualified Maven coordinate string. Since GSON is a common library for Android developers to use, it's actually given in this dialog as an example, with the fully-qualified name. You can type it in:
Hit OK on both dialogs and you should be good to go.
With these Maven dependencies, the build system will automatically download the library and cache it if hasn't done so already; it takes care of that for you.
If you had a library that wasn't available on MavenCentral, you could save the archive in a libs folder in your project, and from that module dependencies dialog, add a File dependency instead of a Maven dependency to take care of it.
If you edit your build.gradle file by hand, you need to click on the "Sync Project with Gradle Files" button in the toolbar to force Android Studio to pick up the changes and update your project. If you go through the Project Structure dialog, that's unnecessary.
There are lots of conflicting answers to this issue in Stack Overflow because the functionality for this is in flux as the necessary features are implemented; it has been really broken before. These instructions should work properly for 0.3.6, and things will get a little easier in 0.3.7 and later.
I had the same issue. The new version of Android Studio (0.3.6) removed some necessary features to add an existing library to a project using the IDE. So you have to do this manually.
Adding the library into the build folder "<project>\App\build\libs\" will break the project on "menu > build > clear project / rebuild project".
Updated solution
My solution is to generate a new folder inside "<project>\<app name>\src\main\libs\" and add the library here. Now you have to change your "<project>\<app name>\build.gradle" by adding the following (my example shows the value for android-support library:
dependencies {
compile 'com.android.support:support-v4:13.0.0'
compile 'com.android.support:support-v13:13.0.0'
compile files('libs/gson-2.2.4.jar')
}
Now select the library in "project View" by right click and select "Add as library... > level > Global library". This will fix an import com.google.gson.Gson; issue.
Maybe you still cannot build. In this case you shall check you project module settings and see if there is an error for Gson dependency. I let Android Studio fix this issue by hitting a "small red bulb icon > add dependency" in the lower right corner of module settings dialog. Now it does not show me no errors anymore on build.
Now we have only one remaining problem: The project does lose the library reference on project close. So we have to add the library on open again. Maybe this is an issue of Android Studio 0.3.6. Mario filed a bug report.
BTW: I upvoted this question because I searched without success for a working solution in the internet. I think beginners will always fail to work with the Android developer tutorials of Google when they are forced to deal with the support library.
Update / Recommendation
Unfortunately I did not get AS 0.3.6 working properly. There are to many issues - at least when adding another module with different namespace. So I switched to the origin IDE: IntelliJ IDEA 12 community Edition. It's free and works for me. I did all the stuff in 2 hours which need days using broken Android Studio. I have no idea what forces Google to build its own IDE based on IntelliJ IDEA without additional benefits / noticeable features when the latter works like a charm.
Running Android Studio 0.4.0
Solved the problem of importing jar by
Project Structure > Modules > Dependencies > Add Files
Browse to the location of jar file and select it
For those like manual editing
Open app/build.gradle
dependencies {
compile files('src/main/libs/xxx.jar')
}
I posted the same to
importing jar libraries into android-studio
putting a duplicate here just in case you stumble into this post instead
Click on ProjectName->Libs folder.Paste that jar file into that folder.
Just refresh the project.You are done.
Using Android Studio 0.8.2, I had to do the following (supposing the library you're trying to add is called MyExternalLib):
In the "app/libs" folder on the hard disk, create a sub-folder "MyExternalLib", and copy the external library into that folder.
In the file "app/build.gradle", inside the block named "dependencies", add the line compile project('libs:MyExternalLib')
In the file "settings.gradle", add the line include ':app:libs:MyExternalLib'
Click the button "Sync Project with Gradle Files"
Create a new library module
It is good development practice to group functionality that you may reuse in other apps inside a library module. To create a library module inside the BuildSystemExample project:
Click File and select New Module.
On the window that appears, select Android Library and click Next.
Leave the default module name (lib) unchanged and click Next.
Select Blank Activity and click Next.
Type "LibActivity1" on the Activity Name field and click Finish.
The project now contains two modules, app and lib, with one activity in each module.
https://developer.android.com/sdk/installing/studio-build.html

Moving Android Eclipse Project to network drive

I've been working on my local windows system to develop an android app, all working fine in Eclipse. In order to implement some git versioning in a neat and tidy way alongside all my web-app based projects, I'm trying to move my project to a Linux box, accessible via a samba share over the local network. The samba all works fine.
Can someone guide me or point me to a guide to move a project which also includes class dependencies? Im fairly new to Android, Eclipse and Java so please forgive this novice. Thank you.
I've found this video-tutorial very helpful: http://www.youtube.com/watch?v=uVO0_UdQ_Wg
What I've done:
exported project (inc dependencies) as general archive file
Switched workspaces to the network drive.
Imported existing project from archive file
The errors I'm getting are:
SherlockActivity cannot be resolved to a type (SherlockActivity is a class dependency project)
Project has no project.properties file! Edit the project properties to set one.
Here is what I would do:
Keep Eclipse work space in local disk.
Copy Android project from Eclipse work space to Samba share (assuming the drive name is X), X:\repo\myproject.
Here, if necessary, delete Eclipse generated folders and files like bin, gen, .project, .settings.
Pull fresh copy of ActionBarSherlock to X:\repo\ActionBarSherlock.
Delete myproject and ActionBarSherlock from Eclipse (via Package Explorer), probably by checking "Delete project contents on disk (cannot be undone)" as I already have copies on Samba drive.
Create new "Android Project from Existing Code" and choose X:\repo\ActionBarSherlock\actionbarsherlock as a root directory.
Here, leave "Copy projects into workspace" unchecked.
Create another new "Android Project from Existing Code" and choose X:\repo\myproject as a root directory.
Here, again, leave "Copy projects into workspace" unchecked.
(Probably, myproject will fail to build with "SherlockActivity cannot be resolved to a type" error) Right click on myproject and choose "Properties" menu, go to "Android" tab and add actionbarsherlock from "Add library" field.

Android dependancies configuration

I am going through the Android app tutorial here:
http://developer.android.com/training/basics/actionbar/styling.html
As part of what I am doing I added in the appcompat library as part of adding in the themes. Not sure how I got to this point but when I try to run the app I get the error :
"The container 'Android Dependencies' references non existing library 'D:\eclipse-bundle\sdk\extras\android\support\v7\appcompat\bin\android-support-v7-appcompat.jar'"
The library DOES exist, just not at this location. It is located at:
D:\eclipse-bundle\sdk\extras\android\support\v7\appcompat\libs\android-support-v7-appcompat.jar
There does not appear to be any way to edit this property. How do you fix this problem? what is causing it?
Thanks in advance for any help.
Make sure you follow this step to add .jar files.
In the new library project, expand the libs/ folder, right-click
each .jar file and select Build Path > Add to Build Path. For
example, when creating the the v7 appcompat project, add both the
android-support-v4.jar and android-support-v7-appcompat.jar files to
the build path.
I had the same issue so I CMD+Q'd Eclipse and re-opened it.... magically worked. I guess "turning it off an on again" works sometimes :D
You can select your project on eclipse, then right-click...
A menu should appear, you can click on "properties".
A flying screen should appear, you can click on "Java Build Path"
Another flying screen should appear, you can click on the tab "Libraries"
This tab actually hold references to what is in the "libs" folder, and you can delete your old lib and insert your new lib (with the correct path) there.
Would not recommend any of it, actually... Please just import the "support lib project" into your eclipse workspace and make your project reference the imported project as a "lib project".
That is way more handy, and will actually give you a better flexibility for doing more stuff with compatibility lib.
And also, since it is on your workspace, you are the one that compiles it. No future lib reference problems \o/

The import android.support cannot be resolved

I am trying to run the code provided HERE
I downloaded the code from their Github and imported into Android SDK, but it shows error at the lines
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
in multiple files. However if I check the Android SDK Manager, the Android Support Library as well as the Android Support Repository in the Extras folder are both installed. I can also see the support folder and it's contents in the SDK_INSTALL\sdk\extras\android\support directory. It also has the v4 folder which also contains the android-support-v4.jar and the src folder. THen why is it still showing the error, how do I resolve it and how do I get that sample code running? THe sample code is for an RSS reader app, if that's relevant.
Follow these steps:
For Eclipse:
Go to your Project's Properties
Navigate to the Java Build Path
Then go to the Libraries tab. There click the Add External JARs Button on the Right pane.
Select the android-support-v4.jar file, usually the path for the Jar file is :
YOUR_DRIVE\android-sdks\extras\android\support\v4\android-support-v4.jar
After adding android-support-v4.jar Library, navigate to the Order and Export tab and put check mark on the android-support-v4 Library file.
After doing the above, Clean the Project and Build it.
Problem Solved.
For Android Studio:
Short Version:
Add the following line to your build.gradle file:
implementation 'com.android.support:support-v4:YOUR_TARGET_VERSION'
Long Version:
Go to File -> Project Structure
Go to "Dependencies" Tab -> Click on the Plus sign -> Go to "Library dependency"
Select the support library "support-v4 (com.android.support:support-v4:YOUR_TARGET_VERSION)"
Navigate to your "build.gradle" inside your App Directory and double check if your desired Android Support Library has been added to your dependencies.
Rebuild your project and now everything should work.
Further reading regarding this Question:
Support Library - Android Dev
Recent Support Library Revisions
Support Library Packages
What is an Android Support Library?
How do the Android Support Libraries work?
andorid-support-v4.jar is an external jar file that you have to import into your project.
This is how you do it in Android Studio:
Go to File -> Project Structure
Go to "Dependencies" Tab -> Click on the Plus sign -> Go to "Library dependency"
Select the support library "support-v4 (com.android.support:support-v4:23.0.1)"
Now to go your "build.gradle" file in your app and make sure the android support library has been added to your dependencies. Alternatively, you could've also just typed compile 'com.android.support:support-v4:23.0.1' directly into your dependencies{} instead of doing it through the GUI.
Rebuild your project and now everything should work.
Another way to solve the issue:
If you are using the support library, you need to add the appcompat lib to the project. This link shows how to add the support lib to your project.
Assuming you have added the support lib earlier but you are getting the mentioned issue, you can follow the steps below to fix that.
Right click on the project and navigate to Build Path > Configure Build Path.
On the left side of the window, select Android. You will see something like this:
You can notice that no library is referenced at the moment. Now click on the Add button shown at the bottom-right side. You will see a pop up window as shown below.
Select the appcompat lib and press OK. (Note: The lib will be shown if you have added them as mentioned earlier). Now you will see the following window:
Press OK. That's it. The lib is now added to your project (notice the red mark) and the errors relating inclusion of support lib must be gone.
For me they were appearing when i transferred code manually to another laptop. Just do
File>Invalidate Cache/Restart
click on 'Invalidate Cache and Restart' and your are done.
I followed the instructions above by Gene in Android Studio 1.5.1 but it added this to my build.gradle file:
compile 'platforms:android:android-support-v4:23.1.1'
so I changed it to:
compile 'com.android.support:support-v4:23.1.1'
And it started working.
This issue may also occur if you have multiple versions of the same support library android-support-v4.jar. If your project is using other library projects that contain different-2 versions of the support library. To resolve the issue keep the same version of support library at each place.
I have resolved it by deleting android-support-v4.jar from my Project. Because appcompat_v7 already have a copy of it.
If you have already import appcompat_v7 but still the problem doesn't solve. then try it.
This is very easy step to import any 3rd party lib or jar file into your project
Copy android-support-v4.jar file from
your_drive\android-sdks\extras\android\support\v4\android-support-v4.jar
or copy from your existing project's bin folder.
or any third party .jar file
paste copied jar file into lib folder
right click on this jar file and then click on build Path->Add to
Build Path
even still you are getting error in your project then Clean the
Project and Build it.
Android Studio 2.2.3
Linux Mint 18.1
Inside your 'project view' open Gradle Scripts -> build.gradle(Module:app) and put your mouse pointer inside the word dependencies.
Click on the light bulb and click "add library dependency" and for me all the libraries I wanted were listed there.
example libraries that came up for me:
compile 'com.android.support:gridlayout-v7:25.1.0'
compile 'com.android.support:support-v13:25.1.0'
I am now looking to add android support by default in Gradles default configuration.

How to include a Java project in an Android project?

I am using Eclipse to develop an Android board game.
I have developed the UI in an Android project.
On the other hand, I have developed the AI in a regular Java project, because I wanted to be able to test it without all the constraints of the Android emulator (I didn't find any other way to run the code using my Windows JVM).
Now comes the times when I want to 'join' both projects (which work fine independently), that is to use the AI Java classes from the UI.
This is what I have tried:
In my Android project Properties > Projects References, I ticked the Java project.
This allows me to build without error the Android project (which instantiates an AI object).
But it fails at runtime with this error:
Could not find class 'my.package.AI', referenced from method my.otherpackage.UI.onStart
What would be the correct way to include the AI Java Project into my Android Project ?
(NB: I still want to be able to develop and test the AI as a regular Java app, so I think using a jar or copying all the sources to the Android Project would not suit my needs)
EDIT:
I was hoping that the new ADT v17 would solve the problem, but it didn't. I have tried virtually every option available to include a project in my Android project:
Adding or linking a source folder
Adding a Project (Java Build Path > Projects tab)
Adding a Class Folder or a Library (Java Build Path > Library tab)
Ticking all the previously imported projets/libraries as Exported
The only way that allows compiling and running without error is adding the JAR of the Java Project to the Android project build path.
Projects References only add the dependent project source code (your AI project) as a soft (perhaps weak is a more accurate word) reference, your AI project is not added into you Android Project Build path. so the actual ai.jar is not exported into the final apk when Eclipse build your app.
You should add AI project into Android project build path:
Right-click on your android project, select Build Path - Configure Build Path, in Projects tab (Required projects on the build path), add your AI project here.
Then in Order and Export tab (Build class path order and exported entries), tick your newly added AI project appeared int the list.
Step 2 is probably optional, this should add AI project as a reference in Android project and export the ai.jar to final.apk when Eclipse build your Android project.
Update from ADT 17.0.0:
Android Dev Team just release SDK r17 with ADT 17.0.0, which claims to handle this use cases properly now:
Eclipse specific changes
The dynamic classpath container called “Library Projects” has been renamed to “Android Dependencies” as it now contains more than just Library Projects.
The container will now also be populated with Java-only projects that are referenced by Library Projects. If those Java projects also reference other Java projects and/or jar files they will be added automatically (jar files referenced through user libraries are supported as well).
Important: this only happens if the references are set to be exported in the referencing project. Note that this is not the default when adding a project or jar file to a project build path.
Library Projects (and the content of their libs/*.jar files) is always exported. This change only impacts Java-only projects and their own jar files.
Again, duplicates (both projects and jar files) are detected and removed.
More in this link.
Edit: After much testing, reading and found my solution. Problem is (as of this writing) that you can't reference another project from an Android project, although it works fine for a normal Java application project.
Workaround for me (Windows with NTFS filesystem):
In a prompt (admin rights) make a symbolic folder link using mklink /D command pointing to your source project folders, and refresh in Eclipse as needed.
The obvious limitation is you cant use the same package names in source project and target project, and then there is the problem with libs in the common project.
example:
cd \java\workspace\AndroidProject\src\your\package
mklink /D common c:\java\workspace\CommonProject\src\your\package\common
Clarification: You can reference another project from an Android project to make it compile, however when you run, the referenced classes from the included project are not put in the apk classes.dex (verified with dex decompiler).
Using ADT 21.0.0 64bit on Windows 7 64bit. I tried adding project to build path. Ticked in the Order and Export, played with order. I read the official solution, the detailed explanation of that solution, exported as JAR, added to libs. Did not work, while the source was without errors. Read in the comments that the solution didn't work for everyone.
Was fed up with it and tried a simple but somewhat messier solution: linked the Java project's source to the Android project. That did the trick. Hope that this will help others too.
Don't add it to the project references area, instead go into the Android pane (in the Project Properties), scroll down to the bottom, and add it under Libraries.
This sometimes works but sometimes produces bizarre Eclipse issues (not sure why), in which case copying the jar directly into the project seems to work best.

Categories