How to link a Library project in Android Studio - java

I recently downloaded the Android Studio and now I am trying to build my project for which I was using Eclipse before. The project also utilizes GoogleMap API.
And in the code where I am using it and importing the library
import com.google.android.gms.location.LocationClient;
it shows me error:
Error:(40, 39) error: cannot find symbol class LocationClient
I have the code for google-play-services_lib which I had linked with the project as library in eclipse and it was working fine.
But I don’t know how to do the same for Android Studio , please help.

Specifically for Google Play Services, first, install the "Google Repository", found in your SDK Manager.
Then, add a suitable dependency on com.google.android.gms:play-services to your dependencies closure:
apply plugin: 'com.android.application'
dependencies {
compile 'com.google.android.gms:play-services:6.1.71'
}
android {
// your project configuration goes here
}
There is a newer version, one that offers more modular dependencies, that you could explore, but I would start with 6.1.71 to get the basics working first before you optimize with the newer version.

Reverting to the old version of the Google Play Services Library, com.google.android.gms:play-services:6.1.71, will work but according to Google in version 6.5 of the library, LocationClient is Deprecated:
Deprecated clients - The ActivityRecognitionClient, LocationClient, and PlusClient classes are deprecated. If you used those APIs in your app and want to call Google Play services 6.5 or higher APIs, you must switch to the new programming model that utilizes GoogleApiClient. For more information about using GoogleApiClient, see Accessing Google APIs.
Use these APIs instead of the deprecated APIs:
If you were previously using ActivityRecognitionClient, call ActivityRecognition instead.
If you were previously using LocationClient, call the APIs in the com.google.android.gms.location package instead.
If you were previously using PlusClient, call the APIs in the com.google.android.gms.plus package instead.
Refer to this post if you'd like an example of using the new GoogleApiClient to retrieve location.
Source

Android Studio uses gradle build system, you should use dependencies instead of library projects.
Find build.gradle in your module directory and add this to the bottom
dependencies {
compile 'com.google.android.gms:play-services:6.5.87'
}
Explaination here!

Right click on project name then select to "Open Module Settings".
It will show a window with some tabs.
Go to "Dependencies" tab see if "play-service (com.google.android.gms:play-services:x.x.xx)" is added or not.
if not then click on '+' sign at the right top corner select the "module dependencies".
you will see the list of dependencies choose the "play-service (com.google.android.gms:play-services:x.x.xx)" and click on ok.
than on window apply and ok.
and yes don't forgot to sync the gradle otherwise the changes might not work.
Hope this should solve your probelm.

Related

Gradle Project Sync Failed - Android Studio 3.5.1

I should preface this by saying that I don't know much about programming or android app development at all. I am trying to get a program running to control my pool cover automatically. This program is fairly old, which I imagine is the reason for the issues. I have been following this guide to sign and bundle the APK. As soon as I clone the project from github, I am greeted with this:
ERROR: Could not determine the class-path for class com.android.tools.idea.gradle.project.sync.ng.SyncAction.
In addition to this, the "Generate Signed Bundle / APK button" is not available.
From what I can gather, this program uses Gradle 1.10.
Here is the complete output log that I got from android studio
I might have found a solution for your problem but being a project as old as this, you can have problems with library usages.
The reason you're seeing this error is, using info from this answer, because this project was written in Eclipse IDE. The answer says:
when Eclipse export a project to gradle, it use an old gradle plugin version that Android Studio doesn't support.
Following his instructions I changed my Gradle version to 2.3, but Android Studio showed some more errors asking me to upgrade my version to 3.3, as showed here in Project Structure (You can easily access this using Ctrl + Alt + Shift + S).
Notice that Android Studio also asked me to set my gradle plugin version to 2.3 on another error, that I did. You should be aware that I had to change my distributionUrl in gradle-wrapper.properties file, as you can see here
Lastly Android Studio demanded me to change buildToolsVersion because it couldn't make my app with original config. Also what gave me the most trouble was the runProgruard function that Studio said didn't exist. I found the solution in this answer. My app build.gradle ended like this.
PS: At EVERY step of changing this configurations I had to go to menu build->rebuild project and then use the gradle button "try again". Sometimes it might seems it is not working, but with patience you will get there.

Errors in android.app.Activity.java

After setting up Android Studio, I tried to find out how few of the methods work but I can see that Activity class have got a lot of errors.
For example:
// Gives: cannot resolve symbol 'CallSuper'
android.annotation.CallSuper();
// Gives: cannot resolve method 'trackActivity(android.app.Activity)'
private final Object mInstanceTracker = StrictMode.trackActivity(this);
// Gives: cannot resolve symbol 'MainThread'
#MainThread
// **312 errors in android.app.Activity, 579 in android.view.View**
My application works fine though. How can i make those errors disappear and be able to see documentation of structures that cannot be resolved now?
Details on my Android Studio 2.1.3 configuration
Installed in Standalone SDK Manager:
Android SDK Tools
Android SDK Platform-tools
Android SDK Build-tools
SDK Platform and Sources for API 24
SDK Platform, Documentation, Google APIs and sources for API 23
Extras: Android Support Repository, Google Repository and Google USB Driver
Being precise, those aren't errors in the source code for Activity/View it's just that :
Your IDE can't find the class android.annotation.CallSuper in it's classpath.
It cannot resolve method trackActivity in class android.app.Activity.
This is because the SDK is subset of the actual Android platform. The SDK shipped to the developers a.k.a Public API utilizes a lot of platform features a.k.a. Internal APIs, hence you can't find them in your SDK 24 classpath.
So why are all these portions hidden from developers?
It's kept hidden from developers because most of its implementation varies from device to device, plus they wouldn't be required in 99.99% cases (metaphorical figure not actual stats). You might want to have a look here.
add in your build.gradle file:
dependencies {
compile 'com.android.support:support-annotations:22.2.0'
}
This simply means those classes arent on the classpath. When you run an app on a mobile phone or in the emulator the platform make sure the classes are all present.
... i tried to find out how few of the methods work
The easy way to look at Android source code is to just browse it online. For example here is the Activity class.
If you install the "Android SDK Search" plugin for Chrome, it will automatically add source links to the online Javadoc.
How can i make those errors disappear and be able to see documentation of structures that cannot be resolved now?
The only way to view live code without compile errors is to download and build the entire Android Open Source Project.

Android appcompat v7 error

Being new to Android developent I followed the simplest of tutorials, built a new android project, accepting all default settings (built it for kitkat). To my dismay I have an un-planned project - appcompat_v7, along with the errors:
The container 'Android Dependencies' references non existing library
'C:\Users...\workspace\appcompat_v7\bin\appcompat_v7.jar'
and twice the following
The project cannot be built until build path errors are resolved
Is there a quick way to fix these?
Is this a sign of how difficult, and bugged with unpleasant surprises learning Android is going to be?
(Hope it will not be similar to learning IOS 6 years ago...)
The appcompat_v7 library is added by default to an Android project. You will most likely need it for any demo projects you start making.
To correctly add this library, follow these steps:
For Android Studio:
1. Ensure that you have the Android Support Repository installed in your SDK Manager:
2. In your build.gradle file, include the following implementation statement
implementation 'com.android.support:appcompat-v7:+'
within the dependency bracket.
3. Perform a Gradle sync with the Sync Project button.
~LEGACY ANSWER~:
For Eclipse:
The trick is, you need to clean & build the appcompat_v7 project. Go to
Project -> select Clean -> select the project.
After doing this, if the project does not get built automatically, right click on the project in the package explorer and select Build Project. Now the .jar file will be generated in the project's bin folder. After that, clean & build all projects that reference appcompat_v7.
Now the library should be correctly referenced by all projects that need it.
Note also that:
You must have the latest versions of SDK Tools / Build Tools
/ Platform Tools. If you try this and it doesn't work, then go to
the Android SDK Manager and make sure you have the latest versions of
the required tools.
In case your project only requires API level 14 (Ice Cream Sandwich)
& above, select API level 14 for "minimum required SDK" in the
project wizard when you create a new project. Now the appcompat_v7
library will not be required for this project, and the application
will use the native ActionBar class which is present in AOSP builds
from ICS onwards.
Further References:
1. How to add Android Support v7 libraries in eclipse.
2. Android actionbar how to add supporting library v7 appcompat for Eclipse.
3. android-support-v7-appcompat library project won't work.
4. Difference between android-support-v7-appcompat and android-support-v4.
The accepted answer worked for me but only after I'd installed the latest Java JDK (which was a solution to the problem detailed here Android - "Parsing Data for android-21 failed")
Need to update your SDK manager
click ur sdk manager icon in the ecilipse then update android support Repository in the extras
then
go to File->import->existing project
abt bundle->sdk->extra->android->support->v7 to workspace
after import v7 to workspace go to lib folder choose both jar and right click and choose build to workspace then again right click v7 project go to java build path choose the checkbox and click apply and ok..
if u have updated sdk then follow this steps
step 1:
Right click ur project then go to
property->choose java build path
step 2:
click Add External jar
Step 3:
Browse to ur adt bundle go to ur workspace choose the folder
android-support-v7-appcompat -> lib -> appcompat_v7.jar
Appcompat_7 error solved!
Hi, im a complete beginner, i solved my problem by go to SDK manager update/install all the new android sdk build tools (as im not sure which is which i should update/install so i update/install all the build tools. after updated/installed all the new build tools i closed my eclipse and relaunch the eclipse again. if you see any error again? go to the top bar click PROJECT and choose CLEAN. and run your application again and i think it will work fine..
it worked for me when i changed project build tarjet to API 21 for the android_support_v7_appcompact project
I had the same problem and I tried all of mentionded above advices and much more from, but no one helped me to solve my issue. My solution was creating a progect with minimum required SDK API 14: Android 4.0 (IceCreamSandwich). Doing this way Eclipse will not create appcompat_v7 project.
Also I found another, better, solution:
Go to Android SDK Manager and install Android 5.0.1 (API 21) or newer. If you don't use the emulator you can install only SDK Platform and Google APIs;
Go to appcompat_v7 -> Properties -> Android and check Android 5.0.1 in the Project Build Target;
Then go to Java Build Path -> Order and Export and check Android 5.0.1;
Do the same procedure for your project.
Strange fix but this worked for me ( My minimum SDK for my project is 10 - can't use 14)
Right-click on the "appcompat_v7" project and go to
-> Properties -> Android.
Your target build should already be set at the latest build (in my case Android 5.0.1). Click any other android package (apart from its current target) and click "Apply". Click "Ok" to close the box.
Re-open the Properties box by the same method but this time check the latest build (in my case Android 5.0.1) as the Project Build Target. Click "Apply", then "Ok" to close the box.
Clean the appcompat_v7 project. You'll probably need to clean/build all other projects that reference this library.
This solved it for me.
I was having with same Issue and fed up but finally I got the answer . I resolved this by Copy appcompat library E:\adt-bundle-windows-x86_64-20140702\sdk\extras\android\support\v7\appcompat and paste appcompat Library in the the Project which I want to Import . I close the Eclipse and reopen. But One thing I want to consider before these stuff I updated Android Support Repository and Android Support Library as well.
I solved it by downloading the file manually somewhere on the internet (be careful) and putting it into the needed \appcompat_v7\bin\ folder before creating a new Android project. After that, also my R.java file was generated successfully.
Honestly, no other way seemed to fix it. Could not obtain the file via the SDK manager.

How to import 3rd party libraries

I found some cool android libraries the other day and decided to try some. But I'm having trouble correctly importing the library.
This is the URL of the library : https://github.com/dmytrodanylyk/android-process-button
I first tried importing the library to eclipse (and move the files in java directory to src directory and set the project as library) and importing the sample to eclipse and set it to use the library project (Properties->Android->Libraries). But it didn't work. The layout files said it failed to instantiate [custom widget class].
The I tried importing the .jar file to libs directory (and update the java build path) but it didn't work either. It showed errors in the java files too.
I then tried copying all the java and layout files to the sample project directory and it worked. But I'm guessing that's not the way to work with 3rd party libraries.
I first thought it's some error with the library but all the other libraries I tried to import to my projects faced the same problem.
Can someone walk me through how to correctly import a 3rd party library to my android project?
The best option you can do is to use gradle as your dependency manager.
The library you have posted is using Gradle so you can link to this library in this way:
dependencies {
compile 'com.github.dmytrodanylyk.android-process-button:library:0.0.7'
}
And voilá! You have your library ready to use in your app :D
I'll let you a couple of useful links to use Gradle properly:
Mark Allison's tutorial about Gradle It will explain step by step how Gradle works (keep in mind that is using an outdated version of Android Gradle plugin, you have to adapt the version to the current one which is 0.10)
Official Developer Docs about Gradle In here you can find another step by step tutorial to configure and use Gradle (this one is more updated).
You can use Gradle directly in Android Studio (Intellij) if you don't mind to change your main editor.
If you want to stick around with Eclipse then this stackoverflow link may be helpful!
EDIT:
Oh! And if you want to search already Gradlized libraries you can navigate to Gradle Please!
I see the library uses gradle. So if you use gradle for dependency management or Android Studio (which uses gradle by default) importing will be a breeze. The installation instruction for gradle is even available at the github project site.
dependencies {
compile 'com.github.dmytrodanylyk.android-process-button:library:0.0.7'
}

BaseGameUtils\bin missing when implementing Google Play Game Services leaderboard

I am trying to implement the Google Play Game Services leaderboard found here. I imported the library BaseGameUtils, made it a library and added it to my main project. But there is a little red X and a red ! on my projects (see below picture).
Also, it says that the C:\Users\lyonsmg\Downloads\android-samples-master\BaseGameUtils\bin is missing in my Android Dependencies for my main project, BibleTriviaLite (see below picture).
What am I doing wrong? I was able to import the google-play-services_lib correctly but not the BaseGameUtils. How can I fix this?
Check the project properties for BaseGameUtils. Go to Project Properties | Android and check that it's targeting a valid Android target (there should be exactly one target checked) that's API level >= 8.
Check that BaseGameUtils is referencing google-play-services_lib as a dependency. To do this, go to Project Properties | Android and look at the References section. If there is nothing there, add a reference to google-play-services_lib.
Check that both google-play-services_lib and BaseGameUtils are set up as library projects. Go to Project Settings | Android and verify that both have the "Is Library" checkbox checked.
Clean and rebuild everything.
Hope this helps!
I was having the same problem and I think it was because you haven't added the google-play-service.jar to the BaseGameUtils project. I did this in eclipse by finding an error in the project, I used GamesClient in BaseGameActivity.java on the method getGamesClient() and hovered my mouse over GamesClient until the dropdown box appeared. I then clicked fix project setup. On the popup window click add google-play-service.jar (the 2nd one in the list) then click ok. This fixed the error for me. Hope it helps.
For me the problem was the google doc, imho. If i import a project , set it as lib, and add it as a reference to my code/project I would have never thought I would have to add it to compile path manually as well ( of course i want to compile it , no?) . Everything was set like above, I retested three times. Didn't work. BaseGameUtils would'n find the reference of com.google...gsm. Only if I manually add google play service library project to java build path under projects for the project BaseGameUtils it would a compile. (Eclipse 4.3.0)
from the short instructions by Google's github doc
If you're using Eclipse...
Start Eclipse
Import the desired sample (Project | Import | Android | Existing Android Source)
Import the Google Play Services library project (available for download through the SDK manager). Make sure that the sample is REFERENCING the library project (Project Properties | Android | References)
Import libraries/BaseGameUtils AS A LIBRARY Make sure that the sample is REFERENCING the library project (Project Properties | Android | References)
Now jump to the Modify IDs, compile and run section and continue to follow the instructions there.

Categories