How does Android Studio import Gradle projects - android

I've read lots of "how-to" guides to importing libraries into Android Studio (now the only supported Android SDK, hmmpf) yet none of them explain what's actually going on during an import.
I can see projects that have Gradle build files everywhere and some that have none, why is this?
Half the stuff I get on Git-hub to play around with takes me more time to successfully import than the time it's saved me by using. My question is, what is required to successfully import and use your average library (project, not JAR or AAR) in Android Studio without having to refactor half the stuff?
Eclipse would allow me to use one single project (folder structure, not jar) as an "external" library and I needn't define what needs compiling where and when.
Android Studio keeps making copies of the project library inside my project folder then proceeds to complain about a whole host of things whilst it pollutes my project space with incorrectly named folders and packages (See screenshots)
Here's and incomplete import where I only imported the "core" folder
because I don't need this other blokes extra stuff - but it's missing
some definitions.
Here's a complete import of the whole folder (as you can see, samples
and all) but it's polluted my project folder and I don't need the
majority of it, and there's all these individual references now to
dependencies and things I don't think I should need.
How do I properly import a whole library with it's dependencies
without it polluting my current project?
Thanks in Advance!
EDIT
Ok here's what I have in one case of a library for an example.
The root folder of the zipped library - lots of garbage I don't need
What do I need to do to use this library? If I import the whole folder using Android Studio's "Import module" feature, it just makes a mess.
If I import just the "Core" folder it semi-works, but lacks some dependencies.
Why are there 3 individual gradle.build files?
Why are the required components in separate folders?
Should I just manually copy an arbitrary number of folders to my new "Libraries" folder under the root of my project?
Importing via File->Import Module
So the "sample" "module" (library..) requires the "layout" and "core" modules, that makes sense. But why if they're required aren't they all in the one module, using the same gradle.build file?
Now I've done that, I get this "Error:(23, 0) Could not read script 'C:\Users\me\AndroidstudioProjects\MyApp\gradle\scripts\gradle-mvn-push.gradle' as it does not exist."
It does exist at C:\Users\me\place_the_module_was_imported_from\gradle\scripts\ though, but
why if it's needed, was it not copied? Is this just bad Gradle...'ing?
Again, thanks!

I think I've found what seems to be the closest answer to this.
10 Gradle Basics
The addition of Gradle is going to be your largest hurdle during your transition over to Android Studio. Here are the basics of what you need to know:
Your Android Studio project will have a settings.gradle for the whole project
The settings.gradle file includes references to all modules which are included in the project and is generally updated automatically when you import or create a new module
Each Android Studio module will have its own build.gradle file
If one module depends upon another module, you will need to add that dependency in the build.gradle under the dependencies section
If your Module requries a jar file, it must be listed in that Module's build.gradle file
You may add remote dependencies to your project by listing them in a module's build.gradle file
From time to time, you will need to manually edit these gradle files Read more about Gradle over here.
Problem is that dependencies show up as modules in the UI so I now have a polluted workspace full of single-use libraries.

You can add the library via gradle.build and the necessary dependencies will be neatly added to your project without "polluting" your project. An example is the Facebook SDK
dependencies {
...
compile 'com.facebook.android:facebook-android-sdk:3.23.0'
}
As you can see from the image, the only place to know the external libraries am using is in the libraries folder of my project:

Add .jar file in your project
1 - Create libs folder under the app folder.
2 - copy your .jar file
3 - paste in app->libs folder
4 - after Click on File menu and select Project Structer option and open it
5 - Open one window in left side Modules under App folder select that folder
6 - In your window Top display 5 Option menu in select Dependencies option
7 - Then Right side display "+" "-" Sign click on "+" sign
8 - then open small dialog and choice "File dependencies" option
9 - then open another dialog here navigate .jar file in your libs folder and select
10 - click on Ok button and Apply And ok
add module project
1 - Click on File menu and select Import Module option
2 - then open one window here navigate your module and Finish
3 - Click on File menu and select Project Structer option and open it
4 - Open one window in left side Modules under App folder select that folder
5 - In your window Top display 5 Option menu in select Dependencies option
6 - Then Right side display "+" "-" Sign click on "+" sign
5 - then open small dialog and choice "Module dependencies" option
6 - then open another dialog here Module and select
7 - click on Ok button and Apply And ok

Related

Renaming modules in Android Studio?

I picked the wrong name of a specific module which I imported to the Gradle project in Android Studio.
In this face I want to rename module Facebook1 to Facebook.
Is this possible in Gradle project and how to do it?
Android Studio >= 1.4.1
Right click the module -> Refactor -> Rename.
Enter the new name of the module, click OK.
Older versions of Android Studio
The following worked for me, tested in Android Studio (AS) 1.1 & 1.0.2, directly after importing a project.
Under the 'Android' tree view (ATV),
Right click the module name: app, Refactor -> Rename (to myApp, click ok)
Open settings.gradle
Change ':app' to ':myApp', and save
You will be prompted to 'Sync Now', do it (the prompt is a blue link above the file)
(Your project will disappear from ATV, but don't worry, just exit AS)
From a file manager (outside of Android Studio), open your project root directory, and rename the folder app to myApp
Re-open your project in AS, and you will see your module and Gradle scripts re-appear under ATV! :)
Congratulations - your module has now been renamed! (whew, time for some tea)
You can do it manually, with or without changing the folder structure (consider not change it for a minor impact on version controlling)
Changing folder name accordingly
1- open settings.gradle and change the name of your module (exemple from ':facebook1' to ':facebook')
2- change the folder name accordingly (from xxx/facebook1 to xxx/facebook)
3- If other Modules have dependencies on it: open the corresponding build.gradle and change dependency (from compile project(':facebook1') to compile project(':facebook') )
Without changing folder name
1- open settings.gradle and change the name of your module (exemple from ':facebook1' to ':facebook')
2- add a new line on "settings.gradle":
project(':facebook').projectDir = new File(settingsDir, '/facebook1')
3- If other Modules have dependencies on it: open the corresponding build.gradle and change dependency (from compile project(':facebook1') to compile project(':facebook') )
In the project view on the left in Android Studio,
right click on the module whose name you want to change
select 'refactor' -> 'rename'
choose the rename module option
follow step 1 - 2 and then rename the directory too
Android Studio 1.3.2,
1. switch Project view on the left in Android Studio
2. right click module which want to change -> Refactor -> Rename
3. do both "Rename directory" and "Rename module"
4. open settings.gradle, change module name
5. Build -> Clean Project
As of Android Studio 3.4.1
Rename module
In Project window, right-click module and select Refactor > Rename
Choose Rename module and press OK
Enter new module name and press OK
Important settings.gradle is updated automatically. However, you must manually update any build.gradle dependencies to reflect new module name:
dependencies {
// manually update this after renaming the module
implementation project(':newmodulename')
}
Rename package to reflect new module name
In Project window, right-click desired package and select Refactor > Rename
If there are other src sets with same package name (test or androidTest), a dialog will ask if you want to rename them as well. If so, select Rename package
Enter new package name and press Refactor
Select Do Refactor in the Refactoring Preview
Tip: Select File > Invalidate Caches / Restart to fix any unresolved resource references caused by renaming a module.
In AndroidStudio 2.2.2 renaming the module alone via refactor/rename worked for me. (32-bit linux Mint)
The rename dialog presents two options: "directory" or "module" (radio buttons so mutually exclusive, at least in one pass). I wasn't sure which to pick so I ended here looking for an answer. One answer said "do both" as two steps, another answer said "do module, then directory" as two steps. So I chose "module" first. To my surprise that was all that was needed, both module and directory were changed. settings.gradle was updated by the refactor/rename in the one step as well.
"gradle synce" and "clean build" were also triggered. Upon build to my tablet, no complaint about the name change.
just adding this answer in case someone else with 2.2.2 or later is unsure which to pick. Also to re-iterate what others have said, back up your project first.
Close Project and rename module folder oldName to newName then open Project and change module name include ':oldName' to include ':newName' in settings.gradle
This is working for me in Android Studio 1.2.1.1
Premise: I do NOT use the refactor function of Android Studio to rename the module
These are the steps I perform:
Rename the folder of the module (directly in the file system)
Modify the setting.gradle file accordingly (directly in the file system)
Open the project in Android Studio
That's all.
Different people seem to have success with different methods, but when I wanted to rename my project from:
com.example.myname.projectname
to
com.company.projectname
I tried everything and the only way I got it to work in the end was:
1) Exit Android Studio
2) back everything up! (find your project folder and make a copy of it)
3) Find and replace all instances of com.example.myname.projectname with com.company.projectname
Note that there are five places (actually it probably depends on your project, I had five) where you need to rename folders, as it creates subfolders called com\example\myname\projectname:
project\app\src\main\java
project\app\src\androidTest\java
project\app\build\generated\source\buildConfig\debug\
project\app\build\generated\source\buildConfig\test\
project\app\build\generated\source\r\debug\
Also, in project.idea\ there is a file called workspace.xml - in that file you'll need to find the text com\example\myname\projectname
Otherwise, do a search through the project directory to find "com.example.myname.projectname" and replace with "com.company.projectname"
4) Open Android Studio again. if the initial project directory name is the same (possibly if the project name itself is the same?) it will reopen if you had it open last time, otherwise you should be able to open as normal.
5) Go to File - Project Structure (CONTROL+ALT+SHIFT+S).
6) Under "Flavors", change the application ID to the new name.
7) Excit Android Studio and restart it.
8) Now you should be able to build your project.
9) Go have a drink, that was a lot more effort than it should have been!
One really important note, you can only have ASCII letters in your package name (you may be able to have numbers, but not as the first character). Java itself allows numbers to start a package name, but Android does not: Package names in Android studio when dev name starts with a number? I was trying to rename
My workaround (and from a look around the apps on my phone it seems to be what others are doing) is if you wanted a number at the start of either your company name or app name, write the word - instead of com.123company.55project perhaps use com.onetwothreecompany.fiftyfiveproject

How to import external library in android studio?

I struggle to import library 'PageSlidingTapStrip'(https://github.com/astuetz/PagerSlidingTabStrip).
I found this question(How to import eclipse library project from github to android studio project?), and I follow this solution.
cf)I'm using win 8.1 , android studio version 0.3.6
First, build new project like this condition.
Second, go to File -> New Module click, create new module as android library and make condition like this.( Unchecked create custom launcher icon, activity )
And this part I got a first question. Which version should I choice version about 'Minimum required SDK' , 'Target SDK', 'Compile with'? Just Follow project version? (image show default value when I create this window first.)
Anyway, I progress under condition ' Minimum required SDK : API 9 / Target SDK : API 19 / Compile with : API 19 '
Third, delete files under 'Project/PageSlidingTabStrip/src/main/' and copy folder&file under external library 'res', 'src', 'AndroidManifest.xml' and move into 'Project/PageSlidingTabStrip/src/main/' then renamed src to java.
And then project & folder become like this. (what I followed question as , told me copy and move file 'ic_launcer-web.png' apart from 'res', 'src', 'manifest' , but I can't find that file)
Fourth, go to 'File -> Project setting -> Module -> click project module -> '+' button -> select Module dependency' then apply.
After that, I face this error and cannot resolve R.
And I can't go next step, can't finish import library.
What should I do?
Is there any other solution?
Thanks for seeing long question. I hope to get a solution!
I also use the PagerSlidingTabStrip Project in my app and everything works fine.
I'm not sure if it's already possible to add a library project via the IDE (-> without any problems).
Please try to add the library project by editing the gradle files like this:
first delete your module PagerSlidingTabStrip
then create a folder in your root project directory (-> NewOne) named 'libs'
copy the complete folder 'library' of the PagerSlidingTabStrip project on git into 'libs'
rename the folder 'library' that you just copied to e.g. "PagerSlidingTabStripLibrary" (just to avoid confusion) -> after the renaming you should have the path: "NewOne/libs/PagerSlidingTabStripLibrary"
now add this library in your settings.gradle with the following command:
include ':libs:PagerSlidingTabStripLibrary'
go to your build.gradle file of your AppProject aps and add the following line to your 'dependencies':
compile project(':libs:PagerSlidingTabStripLibrary')
at least you have to sync your gradle files: Tools -> Android -> Sync Project with Gradle Files
Please try this. If you get errors please post the log file.
For update up to Android Studio 1.2.1.1
There are basically three types of dependencies which we have to add in project of android Studio
Add Normal Dependencies like Support files
Right click on project->open module settings
Select dependencies->Now press right "+" icon button ->Select
library Dependencies
Enter Name and Search
Select file and press ok button.
Add Git Hub Dependencies
Find the dependencies of git repository for example compile 'com.jakewharton:butterknife:6.1.0' from https://github.com/JakeWharton/butterknife
A. open build.gradle(module:app) file
B. Add lines compile 'com.jakewharton:butterknife:6.1.0'
OR
Follow as describe above in Normal Dependencies Step by just enter dependencies in search bar
Add Jar files Dependencies
Right click on project->open module settings
press left "+" icon (insert new Module) button -> Select import Jar or .AAR package->browse Jar file and finish
Now select dependencies-> press right "+" icon button ->Select module Dependencies->select jar files->press ok button.
For what it's worth I had this problem and eventually solved it by consistently using the same sdk version & build tools across projects.

How to import eclipse library project from github to android studio project?

I want to use HoloCircullarProgressBar as a library project in my android studio project.
I tried to do it by copying into pre-created "library" folder in my project and then to add it to project in "Project Structure". But it's not working somehow.
Could anyone give a very specific step-by-step tutorial on how to do that in android studio 0.3.6?
If you're importing a library as source code into a Gradle-based project, then at the moment there's no super-easy way to do it (sorry, it's on the to-do list, see https://code.google.com/p/android/issues/detail?id=62122) so you'll have to author your own build file for the library. Actually, it might be easier to use the New Module wizard to set up the build file and directory structure, then you can trim it down and copy the files over. This set of steps should get you up and running. It seems like a lot of steps but it should hopefully go pretty quick.
From the File menu, choose New Module...
From the wizard that comes up, choose Android Library
From the next page of the wizard, give it the module name HoloCircularProgressBar, and uncheck the options for Create custom launcher icon and Create activity.
Click Finish on the wizard.
It should add the new module to your project, so you'll end up with something like this:
Delete everything inside the src/main folder.
Now copy AndroidManfiest.xml, ic_launcher-web.png, res, and src from the HoloCircularProgressBar source into the src/main folder.
Rename the src folder that you just copied into src/main to java.
The New Module wizard left some things in the build.gradle file in your HoloCircularProgresBar module/directory (make sure you're editing that one, not the one for your main app module) we don't need. Remove the dependencies block and the release block.
At this point you should hopefully be able to build successfully. Now if you want to actually use the module:
Go to File > Project Structure... > Modules > Your main module > Dependencies.
Click on the + button to add a dependency, choose Module dependency, and select HoloCircularProgressBar from the list.
Now import statements and usages of the library should work, and you should be good to go.

Project management IntelliJ IDEA / Android Studio

I'm just migrating to Android Studio, and I'm a bit confused about how to manage my projects / modules.
For example, in Eclipse I would have 3 projects:
Library project (has a couple of jar libraries)
App project 1 (uses the Library project, plus some other jar libraries)
App project 2 (also uses the Library project, plus some other jar libraries; does not use App project 1)
So if I understand correctly, I would create 2 projects in Android Studio, one for AP1, one for AP2, and in both projects I would add the Library project as a module?
When I do that, I suddenly get two Facets in the Library module called Android and Android2. What does this mean?
Also, if I go a bit further, and add multiple libraries as modules, I get (in the Libraries tab under project settings) multiple libraries, libs, libs1, libs2. These may contain double jars, which AS complains about.
If anyone could clear some stuff up, that would be great. Thanks!
You can see a Module in Intellij as a Project in Eclipse.
And a Project in Intellij is the sum of modules that make your project run.
When you create a module from existing sources, Intellij will add it's module.iml configuration file to the specified location. Then all the configuration you have done on this Library module will be re-usable when importing this module in another Intellij project.
I know this post is quite old, but still I found it when facing similar problems as Niek. The structure of my project is similar to Niek's, with a Library, a "Full" app and a "Lite" app. The following describes what I did migrate from Eclipse Luna to Android Studio 1.0.2, with Android SDK 21.0.3.
If this can help someone popping on that subject, It would make me a happy man!
Create a Android Studio new project
Select the "app" module, hit F4 to reach Module Settings
Select "app" in the module settings and click the "-" button on top to remove it
Manually delete the "app" folder in your project folder
In the now empy project, go to "File" menu, select "Import module"
Import your first dependency (i.e. customLibrary from the Eclipse project structure below) by selecting the folder from your Eclipse workspace
do "F4" on the imported library, go to "Dependencies" tab, and add Google modules (i.e. appcompat-v7 from below)
Do another "Import Module", selecting the "OwnLibrary" folder from your Eclipse workspace
Fix dependencies in "Project Structure" by adding Google modules (here support-v4)
Repeat the "Import Module" operation for App_1 and App_2
Clean Project; it should rebuild and be ready.
If you have minSDKVersion issues, modify your different manifests. I found some answers about that Here and There
Eclipse Project structure
-MyProject-
+--OwnLibrary
| +--support-v4
| +--customLibrary
| +--appcompat-v7
+-App_1
| +--OwnLibrary
|-App_2
+--OwnLibrary
ADDENDUM:
When importing modules, if you changed the module name, there will be a few extra steps, i.e.:
From that 8th point, after importing the OwnLibrary
"F4" on your library, select the "almost duplicate" folder, delete it by pressing "-" button on the top left.
Go to "Dependencies", remove the dependency with the name of the folder you removed.
Add a new dependency with the new folder name.
Manually delete the folder you removed from your project.
Thomas.

Eclipse Android project, how to reference library within workspace?

I followed some steps I found here (can't find the URL right now, sorry) to convert my Android project in Eclipse to a layout where 99.9% of my code is in a library project, and then I have 2 other shell projects under the same workspace that are mostly just the AndroidManifest.xml files, and a few resource files. This was done so I can support 2 builds of the same project, with just some minor text/icon changes between the 2. The application name is also different so I can publish both on the Android Market at the same time.
Ever since I did this, about every 10 times I compile, maybe once every day or two, I get "dalvik error 1" and something about "Access already exists" (Access being the name of the first Java unit in my library project).
To "work around" the issue I go in to the Java Build path for my stub-project that I am trying to build, and remove the JAR file from my main library from the libraries tab. Then I can build without the error.
Then a while later (maybe 1 or 2 days) I'll get an error about missing classes when I compile my stub-project (not my library). So I'll go back to the Java Build path and put the reference to the JAR file back in, and all is good again for 1 or 2 days, then I'm back to the same error as before.
Is this just a known issue and something I need to do, or can I resolve by a restructure of my projects/workspaces? Currently I have:
Lib Project - only has 2 libs on build path: Android 2.1 and com.android.ide.eclipse.adt.LIBRARIES
First stub project that uses above lib - has the same 2 libs as above project, plus sometimes I use "Add JAR" to include the JAR from the above project's bin\ folder.
2nd stub project - same libs as first stub project
Should I reference the JAR from my "lib project" using one of the other tabs under build path options? Maybe the "Project" tab instead, or the "Source" tab? I don't currently have it under any of those other areas.
When I get in to the weird state, doing a "Clean project" also doesn't help, I've tried that several times and open/close the IDE between cleaning, to no resolve.
At this point we are in the final testing stages, so my normal daily task is:
Make a minor update (bug fix) in the LIB project
Use the publish wizard to export both projects and update Android Market and other places we keep the APK files
So I'd like those steps to stay simple, without having to open/close multiple workspaces or go through a lot of build steps if possible.
The classical Eclipse/Java way
Add a (workspace) library
Right click the project you want to insert in.
Click Properties.
Select Java Build Path.
Select the Libraries tab.
Now, it depends how you compile your library.
So either do an Add JARs... or an Add Class Folder.
Choose the external variant to use an external source.
That's it.
Add a workspace project
Right click the project you want to insert in.
Click Properties.
Select Java Build Path.
Select the Projects tab.
Click Add... on the right side and you are done.
The additional Android way
Two steps are necessary:
Mark the project you want to use as library project
Reference the marked project
Mark the library project
Right click your project and select Properties.
Select Android on the left and tick the checkbox IsLibrary. Done.
Reference the marked project
Right click your project and select Properties.
Select Android on the left and Add... your marked project. It will be added to the list and is ready to use. Now you are able to access all classes and ressources (e.g. drawables, strings) from the referenced, marked project. Awesome, eh? :)

Categories

Resources