Adding external library in Android studio 0.3.6 - java

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

Related

Eclipse ADT appcompat... what is it?

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.

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 do I fix these missing libraries?

So these are the three missing libraries, that I still didn't manage to add to my imported project. I tried a lot of things and it simply won't work. I downloaded google play services with SDK manager and google-play-services_lib isn't there, while google-play-services is there, but I need both. As for CaptureActivity and PortraitQR, I tried adding both jars from the project dexedLibs but it didn't work. I also tried downloading this but I don't know how to add it to the project (I only know how to add single jars)
EDIT: Okay so I played around a little bit and I think I'm closer to a solution now. I manually added google play services lib and capture activity and this is how libraries look like now:
The only remaining problem is that now there is a red exclamation mark next to my project and when I go to build paths there is this red error:
I tried to look for captureactivity.jar but I can't find it. Can anyoe help me out here?
you should import them with new project->Android project from existing code-> then locate your projects path and also make sure you make them library project; right click on project-> properties-> android -> scroll to bottom and check 'is library' checkbox
and the path for google play services lib is =>
\extras\google\google_play_services
you can also find sample code on there
These three are library projects. Let's say you have a project in your IDE which is called "PortraitQR". It must be open before your project, Evanturist, can see it as a library project. And it's the same story for the other two library projects. If it doesn't work, try removing them in the dialog you've shown here and adding them again.
You should not use any .jar files for this to work. You should have these three projects open in your IDE.

jfeinsteins slide menu implementation java/ adding a library as a dependency

So I'm trying to implement jfeinsteins slide menu into my application, but before I do that I want to get a good demo, so I know how to implement it. Problem is that I'm stuck.
this is where I'm getting the library.
https://github.com/jfeinstein10/SlidingMenu
This is the project I'm trying to implement it in.
https://github.com/baruckis/Android-SlidingMenuImplementation
I did this part for the set up.
In Eclipse, just import the library as an Android library project. Project > Clean to generate the binaries you need, like R.java, etc.
,but I'm not sure I'm completely understanding this part.
Then, just add SlidingMenu as a dependency to your existing project and you're good to go!
how do I add slidingMenu as a dependency. Also, the error I'm receiving is SlidingMenu cannot be resolved to a variable, SlidingMenu cannot be resolved to a type.
Please can someone help me answer this!? Am I close?
To add a dependancy to an existing project, select the project containing your code (the one you want the dependancy to attach to), right click the project in the Package Explorer and select Properties.
In the Properties window, select Android and ensure that the library projects are added in the Library section. Click add to add an existing project in the workspace to the project as a dependancy.
It may also be worth checking dependancies in the Java Build Path too. Here you can ensure the correct .jar files are correctly assigned to your projects.
I was stuck at this place too. Please follow the below steps.
After downloading the library. In eclipse, go to New->project->android existing project
After downloading SlidingMenuImplementation, add this to eclipse similar to the way above.
I hope till here you have done. Next, right click on the SlidingMenuImplementation project --> properties --> Android --> Below the target window, you'll find references.
Add the Slidemenu library there. Once done clean the project. The errors are gone.
Please note, this supports only Android 4 and above. For lower versions use ActionBarSherlock.

Problems importing project into Android Studio regarding ActionBarSherlock

Is anyone else having problems importing a project with ActionBarSherlock?
I have a total of 100 errors and 17 warnings. This worked perfectly in Eclipse. I followed the steps to create a Gradle build file. There were no import errors until I tried to build the Project.
I also tried re-downloading ABS fresh and replace into my project.
Were there known issues with ABS and IntelliJ? (which of course Android Studio is now based)
Here are a few errors I'm seeing:
java: ...
Workspace/ActionBarSherlock/src/com/actionbarsherlock/app/SherlockFragment.java:4: cannot find symbol
symbol : class Fragment
location: package android.support.v4.app
java: ... Workspace/ActionBarSherlock/src/com/actionbarsherlock/app/SherlockListFragment.java:4: cannot find symbol
symbol : class ListFragment
location: package android.support.v4.app
java: ...
Workspace/ActionBarSherlock/src/com/actionbarsherlock/widget/SuggestionsAdapter.java:33:
package android.support.v4.widget does not exist
Any help really appreciated
Edit: Seems there are no issues using Standard IntelliJ IDEA. Many guides online for setting it up with ABS. Also as Jake mentioned, he's actually been developing the thing in IntelliJ
Here is an example guide:
http://android-wtf.com/2012/09/how-to-configure-actionbarsherlock-with-intellij-idea/
However, I'm still unsure why its not working in Android Studio
Edit2: solution in answer below. In Short: (I downloaded abs latest version, extracted, deleted the old version of abs from my project, then file > import module... to import actionbarsherlock directory into my existing project. Nb, in my particular case I had an issue with junit compilation error and needed to delete \test\junit\ )
Seems there's a lot of general issues on importing modules to Android Studio, not just ActionBarSherlock, this answer might also address those. (However the last steps relating to junit are particular to abs)
The steps below allowed me to get ActionBarSherlock running with no issues.
1) Download latest ABS here: http://actionbarsherlock.com/
2) Extract ABS you should have a directory in there called "actionbarsherlock". Copy that to your Android Studio workspace. and rename to ActionBarSherlock (changed casing) -- > I now have my Android Studio Projects under \*documents*\Android Workspace\ As opposed to \Eclipse Workspace\
So you should now have something like :
\*documents*\Android Workspace\ActionBarSherlock\
Along with your main project maybe:
\*documents*\Android Workspace\TestProject\
3) Open Android Studio load your TestProject then goto File> Import Module... Now navigate to ActionBarSherlock under \*documents*\Android Workspace\ActionBarSherlock\
Click Ok and next all the way to finish. It will ask if you want to set a dependancy to the new Module (or at least mine did) click OK
4) at this point when compiling I was getting errors in \ActionBarSherlock\test\ complaining about Junit. I simply deleted the \Test\ directory from my ABS Module under project view. Right click \test\ > Delete.... You could also include the junit jar file but I don't think its necessary
5) you should now be able to compile without errors
Hopefully that helps someone.
Essentially though, I needed to re-download a completely fresh ABS, expunge my old project's ABS (which was probably quite old) then Import Module...
This works for me..
first, followed an answer provided by wired00
and I made some changes from here.
I made a build.gradle file in ActionBarSherlock library project by Generate Gradle build files from Eclipse.
edit build.gradle(for ActioBarSherlock library project) , Add a line in dependencies {}
compile files('libs/android-support-v4.jar')
edit build.gradle(for my project), delete compile files('libs/android-support-v4.jar') and add compile project(':abs') abs should be ActionBarSherlock library project name.
edit settings.gradle, add ,':abs'
delete android-support-v4.jar files except the one in the ActionBarSherlock library project
I encountered similar issue, but due to a separate problem, I can't download the latest ActionBarShelock to fix the problem as wired00 described.
If you need to stick with your existing ABS version, see if this works for you:
Click File->Project Structure.
You should see 'ActionBarSherlock' in the module list, click it.
Click the 'Dependencies' tab, very likely you do not have android support library v4 in there.
Click the '+' button at the bottom, and add android-support-v4 as a dependency. If you imported the project from Eclipse, Android Studio may have already imported the support JAR file as a library and you can use it directly. Otherwise, you may need to use the 'jars or directories' option and select the support jar file from a directory.
If your main module or other modules are also using the support library, you may need to change the setting 'Compile' to 'Provided' if you get 'DEX Already Added' errors.
You should be able to compile your ABS mobule now. If you get 'hamcrest' or 'junit' compilation errors, you may delete the 'test' folder in your ABS project as wired00 mentioned.
i also faced the same issue. i even follow the steps similar to importing ABS Library. But i was still getting the same error. Finally i solved this issue.
Solution: After following the steps similar for importing ABS Library,
Re-compile your project (Right click on your project and click on 'Compile Module "YourApp" ')
Re-compile the Library Project (Right click on Library project and click on 'Compile Module "library" ')
That's it..

Categories