What steps need to be performed to add someone else's project (library) to yours in Android Studio? It is necessary that I could turn from my project to classes from another project. I already registered in build.gradle.
2 answers
Firstly, if the library is of high quality, then the author of the library, as a rule, gives instructions on how to connect it to your project - they should be followed first.
Android Studio itself allows you to connect three types of libraries to your project:
from the Maven repository
library as a compiled .jar file
source library
To perform this work, there is a graphical interface in Android Studio, but old-school coders can do the same work by writing the necessary commands to the gradle
configs manually.
Open the project structure window ( File -> Project Structure
) and go to the main project module - the Modules section on the left, click on the module name of your application (in the screenshot this is the app ). In the window that opens, go to the Dependencies
tab. Here you can manage connected dependencies: add new ones, delete unnecessary ones and move them around the hierarchy.
In order to add a dependency, click on the плюсик
on the right and see three points - consider them in more detail:
- Library dependency - add library from external repository
This item requires, as a rule, the least amount of effort. Usually, by default, in the gradle
config of the entire project, JCenter
used as an external repository and all self-respecting developers put their work there, but you can register other repositories with libraries in that config if you need it.
When selecting item 1, a search window appears in which you can type the name of the required library and, if it is present in the repository, you will see it. Pressing the OK
button will do all the dirty work of connecting the library and after the gradle
necessary manipulations for downloading and importing it will be available in your project.
If you do not trust the tools of Android Studio and want to manually add the library from the repository yourself, you need to register in the dependencies{}
section of the build.gradle
your application a link to the required library:
dependencies { compile 'com.mikepenz:materialdrawer:2.9.8' }
If you make any changes to the gradle
configs, a warning appears about the need to synchronize the project - when all changes are made, click the Sync Now
link in the upper right to apply your changes to the gradle
.
- File dependency - add a library from a local compiled .jar file
Before you start importing the local library as a .jar file, you must copy the library file itself into the /libs
folder of your project.
When selecting item 2, a standard file selection dialog will appear, in which you need to specify the actual library file itself.
When you click the OK
button, gradle
will perform some import manipulations, which will take some time, after which the library will be available in your project.
In order to manually do the same thing - to add a library based on the .jar file, in the dependencies{}
section of the build.gradle
application build.gradle
you need to register the following:
dependencies { compile files('libs/realm.jar') }
In order to connect all the libraries from the /libs/
folder at once, you need to do this:
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') }
- Module dependency - add a library from source
Before importing the library from source codes, you need to download the source codes themselves and unpack them to a directory.
For the least problems:
source codes should be
gradle-проектом
Android Studio/IntelliJ IDEA
gradle-проектом
The names of the main module and the plugin library module should not be the same.
the source is a library, not an application (in the
build.gradle
of what you connect is specifiedapply plugin: 'com.android.library'
) - you can fix this yourself in thegradle
config of what you connect.The minimum
SDK
versions of your project and library should be the same - you can fix it yourself in the gradlegradle
project or library.BuildTools
versions of the project and library must match (recommended), or both versions must be installed in yourSDK
(not recommended) . The best solution is to always use the latest version ofBuildTools
To begin with, we import the necessary library into our project as a module. Click File -> New -> Import Module
, specify the path to the root folder with unzipped sources, and follow the other instructions of the import wizard.
After that, selecting item 3 (see the first screenshot), the module selection window appears, clicking the right one and pressing the OK
button will work on importing into your project, after which the library will be available in your project.
Adding a library based on source codes manually is a bit of a chore and I will not describe it. Connoisseurs of the manual configuration of the project will enjoy, having done the "sacrament" on their own, and the rest without any need.
- Now everything is clear. So the library I needed was simply in the Maven repository. :) - compl
- @compl This is the same as manually registering a
compile <библиотека>
in thedependencies {}
build.gradle
module'sbuild.gradle
file, only through the GUI. Just in this case, the studio itself adds these lines to the config - if you look at it, you will see them there. - pavlofff
https://github.com/mikepenz/MaterialDrawer#minimal-setup
It's (theoretically) a one-liner: D. This will create an empty drawer.
new Drawer().withActivity(this).build()
Activity with Toolbar
Drawer.Result result = new Drawer() .withActivity(this) .withToolbar(toolbar) .addDrawerItems( new PrimaryDrawerItem().withName(R.string.drawer_item_home), new DividerDrawerItem(), new SecondaryDrawerItem().withName(R.string.drawer_item_settings) ) .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) { // do something with the clicked item :D } }) .build(); //use the result object to get different views of the drawer or modify it's data //some sample calls result.setSelectionByIdentifier(1); result.openDrawer(); result.closeDrawer(); result.isDrawerOpen(); result.addItem(..); ..
Well, everything is written there ...
- Thank. But that was not the problem. I did not want the studio to import
Drawer
. And I thought that all this library should be added to my project somehow. And, by the way, how did the studio import this library? She scans the whole computer or what? (the folder with the library is just on the desktop) :) - compl - @compl When you import a library through the
gradle
dependenciesgradle
it is downloaded to the cache folder of thegradle
itself. By default, this is the directory:C:\Users\<User Name>\.gradle\caches\modules-2\
and the studio does not scan the entire computer,gradle
stores the local link to the imported library in its configs. - pavlofff - For example in
eclipse
: Project> Libs> Add lib ... And you specify the path. - user26699
compile('com.mikepenz:materialdrawer:2.9.8@aar') { transitive = true }
- compl