Referencing Existing Library Project into Main Project - android

I am trying to make use of Android Studio, currently using Eclipse.
I have exported my Library project for gradle, and then imported my exported project into Android Studio. This is successful, however I then can't find how I can go to my main applications project and reference the newly imported library. Everything I've found looking though Android Studio, always seems to be for creating a new library project, not referencing an existing project.
I am using Android Studio 0.4.0.
Thanks for any help you can provide.
UPDATE
I've looked at what #Grennis said, but I do not have a library section under the Project Structure Dialogue. Below is a screenshot of my project structure dialogue.
UPDATE 2
I've been trying to modify the gradle files manually, I've read that the library needs to be copied to my project and then reference it within the gradle files. I think this may be sort of working as I kept on getting errors stating that it couldn't find the library, however, I am no longer getting that error and I'm getting a different one instead.
The error is:
A problem occurred configuring project ':MysqlManager'.
Configuration with name 'default' not found.
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Below is my settings.gradle
include ':MysqlManager'
include ':Libraries:CritiMon'
Below is my build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile project(':Libraries:CritiMon')
// compile project(':Libraries:NavigationDrawerManager')
// compile project(':Libraries:BoardiesITSolutionsLib')
}
Below is a screenshot showing my directory structure

This question has been asked many times.
using facebook sdk in android studio
Import module to gradle project on android studio 0.4.0v
Import module or project as Library on Android Studio
How to import eclipse library project from github to android studio project?
Problems adding external library to Android Studio 0.3.6
Android studio import module button missing
Can't find import module option in project structure (Android Studio 0.3.4 - 0.5.2)
Cannot import module in Android Studio 0.3.5
The issue with the Configuration with name 'default' not found error has also been asked many times:
Android Studio Gradle Configuration with name 'default' not found
Problems trying to create gradle build
Configuration with name 'default' not found while building android project on gradle
build.gradle and project libs

Select your project and then in the menu select File -> Project Structure
In the "Libraries" section you can add existing libraries
Then in the "Modules" section, under the "Dependencies" tab, you can add those libraries to the project.

Related

Import Library Project with Android Studio 0.3.0

I've just updated my Android Studio version to 0.3.0, and I still can't import a simple library project into my app project.
Even with the new Module Visual Interface. (You dont have to edit build.gradle - ???)
My Project Structure:
+JsonReaderproject
+idea
+gradle
+JsonReader
-build
-src
-everything else...
+External Libraries
These are my steps:
File>Project Structure (here I don't find any "Import Module"), I find that option when
select +JsonReaderProject main folder then View>Open Module Settings. Ok, then I click on >Import New Module, after doing the import, I select my +JsonReader folder in the same window, click on the right "Plus" button, Module Dependency, select my imported module and then Apply, OK.
With these steps I can reference the library in code, and use it, but it doesn't compile.
In your build.gradle (under JsonReader), make sure to add your module dependencies :
dependencies {
compile project(":YourImportedModule")
}
This "dependencies" tag must be in your "android" tag. Like this :
android {
compileSdkVersion 18
buildToolsVersion "18.1.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 18
}
dependencies {
compile project(":YourImportedModule")
}
Make sure that in your settings.gradle (at the root of the project), all your imported module must be in it, like that :
include ': JsonReader:', ':YourImportedModule', ':YourImportedModule1'

Android Studio IDE unable to refer to classes from a library module

I'm using Android Studio 0.2.2
I've imported an existing eclipse project, which has the classic eclipse ADT project
structure and added a gradle build script into it.
Let's name it ProjectA
Then I created a module in it, which has the new Android Studio structure,
And added it as library in File>Project Structure dialog.
Let's name it LibA
The problem is this - ProjectA is somewhy lacking the classpath of LibA and therefore the IDE's java parser fails to see classes from LibA, therefore I'm getting a lot of errors on
the IDE's side.
Gradle of course, compiles everything ok because the build process is no longer related to the IDE as it were in Eclipse.
Anyone got any pointers in the matter?
Thanks!
If you are trying to add a library, go to
File -> Project Structure -> Libraries -> click + then add the jar.
Once it has been added, it will ask you for the module which is dependent on jar. Select ProjectA from the list. That should link the library to Module.
After setup dependencies in build.gradle:
try "Tools -> Android -> Sync Project with Gradle Files"
Set up you configs like this:
/settings.gradle
include ':LibA', ':progect'
/project/build.gradle
apply plugin: 'android'
dependencies {
compile project(':LibA')
}
android {
compileSdkVersion 18
buildToolsVersion "18"
defaultConfig {
minSdkVersion 7
targetSdkVersion 18
}
}

Convert existing project to library project in Android Studio

How can I convert an existing Android project into an Android library project in Android Studio? In Eclipse, that is possible.
Actually, I want to convert an old Android project into an Android library project so that I can use existing code of that Android project to build a new Android project with minor changes in Android Studio.
In your module's build.gradle file (not the root project, if you use modules!), simply replace:
apply plugin: 'com.android.application'
// or, if you're on an old version
apply plugin: 'android' // note: this one is deprecated
...with:
apply plugin: 'com.android.library'
// or, if you're on an old version
apply plugin: 'android-library' // note: this one is deprecated
Note that recently, 'android' has changed to 'com.android.application', while 'android-library' has been changed to 'com.android.library'. Avoid using the old names in new projects.
After updating your build.gradle file, you should Sync Project with Gradle files (which is in the toolbar), as not doing it might result in errors and things not working correctly.
Android Studio will then update some files to indicate that the module is now a library; as this will be added into your .iml file:
<option name="LIBRARY_PROJECT" value="true" />
As you might already know, you will not be able to run your (now) library project -- you will need to include it into an app project.
If you're using Android Studio 1.0 and you are getting “Library projects cannot set applicationId”, make sure you do not have applicationId in your Gradle build file.
Looking at this document
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup
I think all you have to do is add this to your build.gradle file,
Creating a Library Project
apply plugin: 'android-library'
From the link
Creating a Library Project
A Library project is very similar to a regular Android project with a few differences.
Since building libraries is different than building applications, a different plugin is used. Internally both plugins share most of the same code and they are both provided by the same com.android.tools.build.gradle jar.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 15
}
Changing to Library
Goto android project, where you need to change as Library module.
In build.gradle(:app) file,
change this line to
plugins {
id 'com.android.application'
}
to
plugins {
id 'com.android.library'
}
Delete the line for the applicationId in same build.gradle(:app) file
defaultConfig {
applicationId "com.project.example" //remove this line
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Now Sync Project with Gradle Files.
open project in file explorer,open project.properties and try changing android.library=true in project.properties
This is a late response, but I've been trying to do the same thing. None of the above seem to have done the job for me, but I found this that I think works:
Right click on the project name -> Mark Directory As (at bottom) -> Sources Root
I don't know the difference between Resources Root and Sources Root, and a bit of googleing to turn up the answer, but hopefully that's right. I just know a Library isn't supposed to build an apk, and after setting this option, it's not able to so I'm assuming it works.
If anybody else knows more than me, please say so!
If you make it with command line, like chanakya propose, you have to update it with:
android update lib-project \
--target <target_ID> \
--path path/to/your/project
see: http://developer.android.com/tools/projects/projects-cmdline.html#ReferencingLibraryProject
That work for eclipse, but not for android-studio since that update the build.xml.

Android Studio with Google Play Services

I'm trying to test Google Play Services with the new Android Studio.
I have a project with a dependency to the google_play_services.jar.
But when I try to Rebuild the project I get the following errors:
Information:[TstGP3-TstGP3] Crunching PNG Files in source dir: C:\Users\ans\AndroidStudioProjects\TstGP3\TstGP3\src\main\res
Information:[TstGP3-TstGP3] To destination dir: C:\Users\ans\AndroidStudioProjects\TstGP3\build\classes\res-cache\TstGP3-TstGP3
Information:Compilation completed with 2 errors and 0 warnings in 2 sec
Information:2 errors
Information:0 warnings
C:\Users\ans\.AndroidStudioPreview\system\compiler\tstgp3.3f17bd41\.generated\Android_BuildConfig_Generator\TstGP3-TstGP3.74fc5b25\production\com\example\tstgp3\BuildConfig.java
Error:Error:line (4)error: duplicate class: com.example.tstgp3.BuildConfig
C:\Users\ans\.AndroidStudioPreview\system\compiler\tstgp3.3f17bd41\.generated\aapt\TstGP3-TstGP3.74fc5b25\production\com\example\tstgp3\R.java
Error:Error:line (10)error: duplicate class: com.example.tstgp3.R
It seems that it has two BuildConfig files and also two R classes. How can I resolve the issue?
EDIT:
I have noticed that the compiler compiles two R.java files: the one that is in my project folder and another one that is located in the folder %USERPROFILE%.AndroidStudioPreview
So, I tried to exclude this "Preview" folder in the compiler settings and now it's working.
This issue only occurs after I have started to use Google Play Services classes in my project.
I will appreciate if someone can explain the reason behind this problem.
All those answers are wrong, since the release of gradle plugin v0.4.2 the setup of google play services under android studio is straight forward. You don't need to import any jar or add any project library nor add any new module under android studio. What you have to do is to add the correct dependencies into the build.gradle file. Please take a look to those links: Gradle plugin v0.4.2 update, New Build System, and this sample
The Correct way to do so is as follows:
First of all you have to launch the sdk manager and download and install the following files located under "extras": Android support repository, Google play services, Google repository.
Restart android studio and open the build gradle file. You must modify your build.gradle file to look like this under dependencies:
dependencies {
compile 'com.google.android.gms:play-services:6.5.87'
}
And finally syncronise your project (the button to the left of the AVD manager).
Since version 6.5 you can include the complete library (very large) or just the modules that you need (Best Option). I.e if you only need Google Maps and Analytics you can replace the previous example with the following one:
dependencies {
compile 'com.google.android.gms:play-services-base:6.5.87'
compile 'com.google.android.gms:play-services-maps:6.5.87'
}
You can find the complete dependency list here
Some side notes:
Use the latest play services library version. If it's an old version, android studio will highlight it. As of today (February 5th is 6.5.87) but you can check the latest version at Gradle Please
After a major update of Android Studio, clean an rebuild your project by following the next instructions as suggested in the comments by #user123321
cd to your project folder
./gradlew clean
./gradlew build
Go to File -> Project Structure
Select 'Project Settings'
Select 'Dependencies' Tab
Click '+' and select '1.Library Dependencies'
Search for : com.google.android.gms:play-services
Select the latest version and click 'OK'
Voila! No need to fight with Gradle :)
EDITED: This guy really brought it home and has a good little tutorial
http://instantiatorgratification.blogspot.com/2013/05/google-play-services-with-android-studio.html
one side note: I had played around so much that I needed to do a gradlew clean to get it to run succesfully
If you have imported your project or are working from the Sample Maps application located in \extras\google\google_play_services\samples\maps check out this tutorial.
https://stackoverflow.com/a/16598478/2414698
If you are creating a new project from scratch then note Xav's comments on that same post. He describes that Android Studio uses a different compiler and that you have to modify the build.gradle file manually. I did this with success. I copied
google-play-services.jar
google-play-services.jar.properties
into my lib directory and added the following to my build.gradle file
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/google-play-services.jar')
}
Also, if this is a new project check out this post, too.
https://stackoverflow.com/a/16671865/2414698
Most of these answers only address compile-time dependencies, but you'll find a host of NoClassDef exceptions at runtime. That's because you need more than the google-play-services.jar. It references resources that are part of the library project, and those are not included correctly if you only have the jar.
What worked best for me was to first get the project setup correctly in eclipse. Have your project structured so that it includes both your app and the library, as described here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup
Then export your app project from eclipse, and import into Android Studio as described here: http://developer.android.com/sdk/installing/migrate.html. Make sure to export both your app project and the google play services library project. When importing it will detect the library project and import it as a module. I just accepted all defaults during the project import process.
Google Play services Integration in Android studio.
Step 1:
SDK manager->Tools Update this
1.Google play services
2.Android Support Repository
Step 2:
chance in build.gradle
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:4.0.+'
}
Step 3:
android.manifest.xml
<uses-sdk
android:minSdkVersion="8" />
Step 4:
Sync project file with grandle.
wait for few minute.
Step 5:
File->Project Structure find error with red bulb images,click on go to add dependencies select your app module.
Save
Please put comment if you have require help.
Happy coding.
None of the above solution worked for me. Not sure if it is specific to my setup or new release.
I am using Android Studio Beta 0.8.9 and I was not getting any com.google.android.gms:play-service in the library list on following this instruction:
Go to File -> Project Structure -> Select Project Settings -> Select 'Dependencies' Tab Click '+' -> 1.Library Dependencies -> Select com.google.android.gms:play-services:+
I had already done this:
First of all you have to launch the sdk manager and download and install the following files located under "extras": Android support repository, Google play services, Google repository.
What resolved it was to add from SDK Manager, "Google play services for Froyo" then repeating the first step.
Did not understand the reason properly but this worked.
PS: I just observed that even now when I search for play-services this does not come, but when I directly scroll and look through the list it is right there.
Follow this article -> http://developer.android.com/google/play-services/setup.html
You should to choose Using Android Studio
Example Gradle file:
Note: Open the build.gradle file inside your application module
directory.
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "{applicationId}"
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:6.1.+'
}
You can find latest version of Google Play Services here: https://developer.android.com/google/play-services/index.html
I've got this working after doing the following:
Have the google-play-services-lib as a module (note difference between module and project), then add the google-play-services.jar to your models "libs" directory.
After that add the jar to your build path through making a library or add the jar to another library. I generally have a single IDEA library which I add all my /libs/*.jar files to.
Add the library to your modules build path through Module Settings (F4).
Add the google-play-services-lib as a module dependency to your project.
Done!
Note: This doesn't give me the runtime exceptions either, it works.
Open your project build.gradle file and add below line under dependencies module.
dependencies {
compile 'com.google.android.gms:play-services:7.0.0'
}
The below will be add Google Analytics and Maps if you don't want to integrate full library
dependencies {
compile 'com.google.android.gms:play-services-analytics:7.0.0'
compile 'com.google.android.gms:play-services-maps:7.0.0'
}
In my case google-play-services_lib are integrate as module (External Libs) for Google map & GCM in my project.
Now, these time require to implement Google Places Autocomplete API but problem is that's code are new and my libs are old so some class not found:
following these steps...
1> Update Google play service into SDK Manager
2> select new .jar file of google play service (Sdk/extras/google/google_play_services/libproject/google-play-services_lib/libs) replace with old one
i got success...!!!
I copied the play libs files from the google-play-services_lib to my project libs directory:
google-play-services.jar
google-play-services.jar.properties.
Then selected them, right-click, "Add as libraries".

Add support library to Android Studio project

I just installed the new Android Studio and I'm looking for a way to import the support library for Android.
Where is the option for that? In Eclipse that are just two clicks. I googled for it but found nothing. Surely it is too new.
=============UPDATE=============
Since Android Studio introduce a new build system: Gradle. Android developers can now use a simple, declarative DSL to have access to a single, authoritative build that powers both the Android Studio IDE and builds from the command-line.
Edit your build.gradle like this:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 18
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.+'
}
NOTES: Use + in compile 'com.android.support:support-v4:21.+' so that gradle can always use the newest version.
==========DEPRECATED==========
Because Android Studio is based on IntelliJ IDEA, so the procedure is just same like on IntelliJ IDEA 12 CE
1.Open Project Structure (Press F4 on PC and Command+; on MAC) on your project).
2.Select Modules on the left pane.
3.Choose your project and you will see Dependencies TAB above the third Column.
4.Click on the plus sign in the bottom. Then a tree-based directory chooser dialog will pop up, navigate to your folder containing android-support-v4.jar, press OK.
5.Press OK.
I no longer work on Android project for a while.
Although the below provides some clue to how an android studio project can be configured, but I can't guarantee it works flawlessly.
In principle, IntelliJ respects the build file and will try to use it to configure the IDE project. It's not true in the other way round, IDE changes normally will not affect the build file.
Since most Android projects are built by Gradle,
it's always a good idea to understand this tool.
I'd suggest referring to #skyfishjy's answer, as it seems to be more updated than this one.
The below is not updated
Although android studio is based on IntelliJ IDEA, at the same time it relies on gradle to build your apk. As of 0.2.3, these two doesn't play nicely in term of configuring from GUI.
As a result, in addition to use the GUI to setup dependencies, it will also require you to edit the build.gradle file manually.
Assuming you have a Test Project > Test structure.
The build.gradle file you're looking for is located at TestProject/Test/build.gradle
Look for the dependencies section, and make sure you have
compile 'com.android.support:support-v4:13.0.+'
Below is an example.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
You can also add 3rd party libraries from the maven repository
compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4'
The above snippet will add gson 2.2.4 for you.
In my experiment, it seems that adding the gradle will also setup correct IntelliJ dependencies for you.
This is way more simpler with Maven dependency feature:
Open File -> Project Structure... menu.
Select Modules in the left pane, choose your project's main module in the middle pane and open Dependencies tab in the right pane.
Click the plus sign in the right panel and select "Maven dependency" from the list. A Maven dependency dialog will pop up.
Enter "support-v4" into the search field and click the icon with magnifying glass.
Select "com.google.android:support-v4:r7#jar" from the drop-down list.
Click "OK".
Clean and rebuild your project.
Hope this will help!
You can simply download the library which you want to include and copy it to libs folder of your project. Then select that file (in my case it was android-support-v4 library) right click on it and select "Add as Library"
In Android Studio 1.0, this worked for me :-
Open the build.gradle (Module : app) file and paste this (at the end) :-
dependencies {
compile "com.android.support:appcompat-v7:21.0.+"
}
Note that this dependencies is different from the dependencies inside buildscript in build.gradle (Project)
When you edit the gradle file, a message shows that you must sync the file. Press "Sync now"
Source : https://developer.android.com/tools/support-library/setup.html#add-library
Android no longer downloading the libraries from the SDK manager, it has to be accessed through Google's Maven repository.
You will have to do something similar to this in your build.gradle file:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
dependencies {
...
compile "com.android.support:support-core-utils:27.0.2"
}
Find more details about the setting up process here and about the different support library revisions here.
AndroidX[About]
implementation 'androidx.appcompat:appcompat:1.0.2'

Categories

Resources