Creating a standalone library application is a common task in Eclipse + ADT.
I thought that this should be a simple task in Android Studio (1.2 or above), but after struggling with this problem for two days, I figured out that google did nothing about this and implementing a standalone library module is not as simple as I thought. So I decided to share my experiences with you.
To create a standalone and reusable library module in Android Studio:
1- Create a new project with no Activity.
2- New project's default module is named app. Right click on module and refactor/rename it to something like 'library'. Close Android Studio.
3- Open file explorer and rename module's folder from app to library.
4- Open .idea folder. There are some XML files there that have references to app folder. Replace app into library in these files.
5- Open module's gradle file (library/build.gradle) and change plugin to com.android.library. Then remove applicationId.
6- Open Android Studio. Rebuild module. If there is no error, we are done here.
7- Open application which is dependent on that module. Open settings.gradle and include library module as below:
include ':library'
project(':library').projectDir = new File('/Path/To/LibraryProject/library')
8- Open application's app module build.gradle file and add this line into dependencies section:
compile project(':library')
9- Rebuild your project. If everything is right, you will see library module in your project. You can edit library module from there and/or its project and more important: Now you have a standalone library module that you can reuse in multiple projects!
I hope google will make this process a lot easier in future releases of Android Studio!
EDIT:
I checked Android Studio version 1.4 and hopefully in this version we can omit steps 3 and 4.
How do I build a android studio project from github repository that only contains the source codes, no gradle build information. For example. I want to build a project from this repository https://github.com/googledrive/android-quickstart, but the android project could not be built correctly. Can I just add the build.gradle and expect the project to work?
This is a project in eclipse. Just download the zip file, and extract it. Then in android studio, import the existing Eclipse project by hitting File -> Import Project. Android Studio will just build you a AS project, including all the gradle files.
You can just create a new project with same package name as in the github project.
This will create a compilable android project/library. after that manually copy the code by downloading from github (download as zip) and then just copy the source folder.
There are three ways to do this that I know of officially.
1) Add project directly
Step 1:
Add project into following directory {App Root Folder}/Libraries/ (create libraries folder if doesn't exist)
Step 2:
Go to File/Project Structure/ under Dependencies tab click green add icon and select Module dependency and select the project you added to libraries.
2) Add project as a .jar/.aar
Step 1:
Either look for a the version of .jar/.aar you are looking for on web or compile project to .jar/.aar.
Step 2:
Follow Step one for previous way but instead select Library dependency
3) Add project from Maven
Step 1:
Find library on gradleplease.appspot.com
Step 2:
Add compile 'package name' under dependencies in your apps build.gradle
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.
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.
I am using Android studio and I want to add module to my project like
"action bar Sherlock" or jar files,
but when I opened the project structure there is no module or library in the menu :\
In intelij it's appearing:
So what's the problem? How can I add these modules in Android studio?
Update 19 March 2019
A new experience someone has just faced recently even though he/she did add a library module in app module, and include in Setting gradle as described below. One more thing worth trying is to make sure your app module and your library module have the same compileSdkVersion (which is in each its gradle)!
Please follow this link for more details.
Ref: Imported module in Android Studio can't find imported class
Original answer
Sometimes you use import module function, then the module does appear in Project mode but not in Android mode
So the thing works for me is to go to Setting gradle, add my module manually, and sync a gradle again:
First, create your Module. You can do that using the default Android Studio Module Wizard (File -> New -> New Module), select Android Library, give it a Name and a package name, and finish.
After that you will see that nothing happens. Open the side Gradle Projects Section (should be somewhere on the right panel)
You should see something like this:
Expand your module gradle and run the Run Configurations task.
After that refresh the content and refresh the project structure.
It should work now.
Do right mouse click on your project, then select 'Open Module Settings' - then you can add modules to your project..
Go to File->Project Structure-> Project Settings -> Modules.
Click on the green colored + and add new module. select Application module and set the content root to your project module.
Click next and then finish.
Reference
You need to add a gradle.settings file to your root project structure, after that when you "Open Module settings" you will the menu aligned to your gradle.settings. When importing a project to Android Studio, it doesn't create this file for you. Sometimes it's usually better to start a clean project and move your code there, it's usually easier to achieve.
This Might be help some:
To import module as library in your project.
File > New > Import Module
Select Valid path in Source Dir..
Tick on Import > Finish
Now Open Module setting:
Go to File > Project Structure > Modules
Modules > Dependency > click on Green Plus Sign.
Click on Module Dependency > locate module > and Implement your module.
if your module is not shown in "Choose Modules Window"
Follow the below step..
Open Settings.Gradle file
include ':app', 'Put your module name here' and sync project.
Follow Open Module Setting as above.
As for me issue was that the first line in the build.gradle file of the OpenCV library.
It was something like this:
apply plugin: 'com.android.application'
This refers to the fact that the imported OpenCV is an application and not a library. It exists for OpenCV above 4.1.0. So change it to:
Something like this:
apply plugin: 'com.android.library'.
You might get an error in ApplicationId, comment it out in the gradle file.
Open settings.gradle and add the module as below,
include ':app',':bottomnav'
here i have added my newly imported module ':bottomnav' separated with a comma. then Sync your project. your module will be visible to dependency.
Android Studio only displays those module, which are defined in the settings.gradle file of your application.
after defining the module in settings.gradle, you will be able to add the module as dependency of your application.
I had the same problem, after switching to a branch that did not have my module, then switching back.
Clean Project -> Rebuild Project did not work for me.
But File -> Invalidate caches/restart did work.
Check settings.gradle. You'll find this : include ':app'
Just add your own module name and it should look like this :
include ':app', ':yourModuleName'
Sync.
That's it.
Had similar issue when running version control on a project in Android Studio (0.4.2). When pulling it down to a new location and importing the modules, only the "Android SDK" were showing in the Project Structure.
I removed the .idea/ folder from the version control, by adding it to .gitignore file, and did a fresh pull and imported the modules. Now all the settings appeared correctly in the Project Settings and Platform Settings for the Project Structure.
Here's what I did to solve this problem
Close Android Studio
Quick Start -> Check out from Version Control
Please go to Module settings
and choose Modules from Project Settings
then you need to Select src and gen folders and marked them as Source folders by right-click on them and select Source
First You Have To Add Name Of Your Module In setting.gradle(Project Setting) File Like This..
include ':app', ':simple-crop-image-lib'
Then You Need To Compile This Module Into build.gradle(Module app) File Like This..
implementation project(':simple-crop-image-lib')
That's all for adding module now it will be appear in android section or project section as well.
If It's till did't appear rebuild or clean your project..
Although not similar to the question above,
In my case,
The module is not visible in the android tab,
I saw it in the project tab.
After deleting the .idea/workspace.xml file, (I guess there is a problem because the unloadlist list remains here?)
When I reopened the project, it worked fine.
Additionally,
When invalidate caches (clear VCS Log caches and indexes),
It shows up for new modules, not for previously deleted modules (this only worked when deleting workspace.xml).
If you moved the modules, modify your workspace.xml file and settings.gradle file to use the new paths of your module.
Otherwise you will need to try using the import module feature.
Make sure the directory name is lower case.
First and foremost, check that all the modules are listed in your settings.gradle file.
If they are not, add them.
If they are, change something/anything in the file so that the Sync Project prompt comes on. Sync and your project should compile fine; the modules you're looking for will appear in your project structure.
in my case somehow build.gradle file deleted under app i restore it