how to add library to android studio not the single project - android-studio

I want to add library to my android studio, the only way that i know is through the gradle.build file like:
implementation 'com.android.volley:volley:1.1.1'
the problem here is that the volley library is added to the current project only, and if i created an new project i will have to write this line of code again and sync project, is there's any way to add the library to android studio itself so that any time i open new project i have no need to write this line of code and sync project?

You need to create one common build file where you put all library dependencies and common dependencies build file is used app build file or other build file as apply plugin.
Ex: apply plugin: 'common_build.gradle'

Related

Library dependency in Android Studio

Is there a way to have library dependencies in Android Studio as conveniently as in Eclipse?
I have two libraries, the second one depends on the first one, and an app which depends on both libraries.
In Eclipse I had all the projects in one workspace and the dependencies defined. When a change was made in one library, that change was immediately available in the app's project.
In Android Studio, I included the generated aar files of the libraries in the app's project, which created a copy of the aar file in the app's project. When I make a change in a library, I have to regenerate the aar file and copy it to the correct folder of the app project.
There has to be a better way, right?

Android Studio referencing classes in an AAR published to local maven repo

I have a problem in Android Studio (3.1.2) referencing classes in an AAR published to my local maven repo.
I have successfully published an AAR file to my local maven repo and included it in a project that needs to use the AAR following this article:
https://medium.com/#kuassivi/tip-work-with-third-party-projects-locally-with-gradle-961d6c9efb02
Basically I have added a reference to mavenLocal() in the top level project in the repositories sections of buildscript and all projects. I have also added a reference to the AAR in the app gradle build (i.e. implementation groupid:artifactid:version). I have not done anything more than this.
If I do a gradle rebuild within Android Studio, everything compiles fine but if I edit a file that references classes in the AAR, the IDE cannot resolve the references.
When I do find class (i.e. cmd o), the IDE correctly finds the class within the classes.jar of the AAR and decompiles it.
I am guessing that this is happening partly because it cannot pick up the source jar that I published alongside the aar file to the local maven repo.
How do I get the IDE to pick up the source files and reference the classes in the AAR correctly? I would prefer to do this via a change in the gradle scripts rather than via an IDE wizard, if that is possible.
After more digging, I have discovered that the use of the maven repo is irrelevant because it is picking up the AAR file from the gradle cache in ~/.gradle/caches/transforms-1/files-1.1 not from the maven local repo. I can also include the sources files using "implementation com.company:artifact-name:0.8:sources". There is a good information here:
https://www.alonsoruibal.com/my-gradle-tips-and-tricks/
However Android Studio still cannot recognise the imports and though compilation works, the imports are marked in red in the editor. I guess there must be some magic with gradle that I am missing.
I put together a simple example on github here:
https://github.com/jmc420/kotlin_examples
In the multiproject folder there are 2 separate projects, one called "nested" which contains an android library project called "multiprojectlibrary" which "nested" references via a grade include.
The "multipleprojectlibrary" uses the maven publish plugin to publish the aar to mavenLocal via the command gradle publishToMavenLocal.
In the "independent" project "multipleprojectlibrary" is a dependency which is read from mavenLocal.
The "nested" project works reliably.
Using mavenLocal in the "independent" project seems very unreliable. Sometimes the "independent" project correctly uses the mavenLocal "multipleprojectlibrary" but when you make an update and publish again, the "independent" project starts using a copy of "multipleprojectlibrary" in .gradle/caches/transforms-1/files-1.1.
There seem to be 2 issues. One is that the "New Project" menu option of Android Studio only allows you to make an app; you can only make an Android Library via the "New Module", which puts the library as a sub project of an app. An option to create a "New Library" would solve this.
The other issue is that it is not clear how the maven local repo and the gradle cache interact. They seem to get out of sync and sync gradle does not seem to sync the gradle cache with the mavenLocal repo.

Building tess-two into a project using Android Studio's gradle build

There are answers about getting the tess-two project integrated into an Android project within Android Studio, but many are out-dated and none used the current capabilities defined here:
Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI)
What specific steps would be required to make tess-two functionality a part of the resulting APK, using functionality within Android Studio, rather than external file placement, manipulation and command line tools? So taking the building, creation of *.so files into the IDE. Specifically using just Android Studio's integrated build system (Gradle) as described here.
1. Start a new project
2. Import Tess-Two into the project
2b. Add any needed plug-ins
3. Add code in the main activity to get native functionality
4. Configure Android Studio build so that native functionality is available
This is where the specifics are required
4. Configure Android Studio build for native functionality
4a. Link C++ Project with Gradle
First, check the tess-two project for build files. You may select CMakeLists.txt or Android.mk files. Both are currently supported.
In this case, I used ndk-build, which seemed like a good bet for integrating the native code.
For more information, see Android Studio documentation
4b. Manage Long Commands
In Windows, errors may be encountered if the command length grows too large. To prevent problems, use LOCAL_SHORT_COMMANDS AND APP_SHORT_COMMANDS in the Android.mk file.
The "e=87" error is what you are avoiding by doing this:
For more on that topic, see stackoverflow question about error 87.
4c. Add Module Dependency for tess-two
In File > Project Structure > Dependencies use the + to add the tess-two dependency:
4d. Build the Project and check .apk file for .so files
The build, which takes a long time, should complete now. Validate that the .apk file contains the .so files, created during the build. With the tess-two libraries in an static initializer, run the project on your Android device:

Trying to convert projects that werent built using Gradle

I have tried to clone the following repository on GitHub using Android Studio, https://github.com/AlexKang/favr.git, but I have the error:
Before this I selected build project using Gradle and then accepted anything it wanted to install. I am having similar problems with, https://github.com/mb16/RemindEm.git, where I get that
Each time I selected "create project from existing sources" and then accepted everything it suggested.
What am I doing wrong?
The projects have eclipse .classpath files in the folder. I'm guessing you should be able to import the project into eclipse.
As an alternative to using Eclipse you might want to try using a feature of IntelliJ IDEA (Android Studio's parent project). File > New > Project from Existing Sources... Then select the .project file for the import

How to create Gradle build for existing IDEA project?

I have a Groovy project in Intellij IDEA 13.1.5 that I want to build with Gradle. So I have installed Gradle itself and enabled the Gradle plugin in IDEA.
But how can I now build the project (or better: start to write the Gradle build file) from within IDEA? I have created a build.gradle file in the project's module, which is recognized by IDEA correctly as Gradle file, according to the icon next to the file name.
Beside that I don't know how to continue from there. IDEA documentation says I should open the Gradle Tools window, but I can find it in View --> Tool Windows.
Any ideas?
The best way to accomplish this while still being in the same directory is by going to File -> New -> Module From Existing Sources.. and then selecting your build.gradle.
You can create a Gradle Run configuration. To get all IntelliJ Gradle features, you need to import the project via Gradle (File->Import Project, then point it to build.gradle).

Resources