Library dependency in Android Studio - 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?

Related

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.

Incorporate 3rd Party Native Libs into Android Studio project

I am trying to setup an NDK library module structured very similar to the gen-libs module in the Android-NDK sample project. It identifies what I want in an Android NDK library module to allow me to incorporate 3rd party Native Libs:
src/main/cpp
CMakeLists.txt
Android manifest
build.gradle
I am NOT looking to add C or C++ source files to my app module.
Instead, I am looking to create an Android-NDK library module within Android Studio, so that I can incorporate 3rd Party Native Libs.
However, I am Unable to easily reproduce the structure of the gen-libs module in Android-NDK sample hello-libs.
More specifically, Android Studio does not provide any option to create a "New => Module => Android Library => Add NDK/C++ support"
I suppose I can recreate this NDK module manually, but before I do that, I would like to ask the community if, for a brand new project, is there any easy way to auto-create the gen-libs module structure from hello-libs beside manual method?
Feel free to use your copy/paste skills to produce the project manually. The wizard won't do it for you, at least for now.
You can also create an app project and switch it to library by changing one line in build.gradle.
You can also create a library module, right click on this module, and choose 'Link C++ Project with Gradle' from the popup menu (or from File menu).

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:

What are .iml files in Android Studio?

What are .iml files in Android Studio project? I read that it is configuration file for modules. I do not understand how it works, and can't I just use gradle scripts to integrate with external modules that you add to your project.
Also, most of the time AS generates them, so I cannot control project behaviour. If I have a team that works in different IDEs like Eclipse and AS, is it possible to setup my project so it's IDE agnostic?
I don't fully understand how this system works.
What are iml files in Android Studio project?
A Google search on iml file turns up:
IML is a module file created by IntelliJ IDEA, an IDE used to develop Java applications. It stores information about a development module, which may be a Java, Plugin, Android, or Maven component; saves the module paths, dependencies, and other settings.
(from this page)
why not to use gradle scripts to integrate with external modules that you add to your project.
You do "use gradle scripts to integrate with external modules", or your own modules.
However, Gradle is not IntelliJ IDEA's native project model — that is separate, held in .iml files and the metadata in .idea/ directories. In Android Studio, that stuff is largely generated out of the Gradle build scripts, which is why you are sometimes prompted to "sync project with Gradle files" when you change files like build.gradle. This is also why you don't bother putting .iml files or .idea/ in version control, as their contents will be regenerated.
If I have a team that work in different IDE's like Eclipse and AS how to make project IDE agnostic?
To a large extent, you can't.
You are welcome to have an Android project that uses the Eclipse-style directory structure (e.g., resources and manifest in the project root directory). You can teach Gradle, via build.gradle, how to find files in that structure. However, other metadata (compileSdkVersion, dependencies, etc.) will not be nearly as easily replicated.
Other alternatives include:
Move everybody over to another build system, like Maven, that is equally integrated (or not, depending upon your perspective) to both Eclipse and Android Studio
Hope that Andmore takes off soon, so that perhaps you can have an Eclipse IDE that can build Android projects from Gradle build scripts
Have everyone use one IDE
Add .idea and *.iml to .gitignore, you don't need those files to successfully import and compile the project.
They are project files, that hold the module information and meta data.
Just add *.iml to .gitignore.
In Android Studio: Press CTRL + F9 to rebuild your project. The missing *.iml files will be generated.
Those files are created and used by Android Studio editor.
You don't need to check in those files to version control.
Git uses .gitignore file, that contains list of files and directories, to know the list of files and directories that don't need to be checked in.
Android studio automatically creates .gitingnore files listing all files and directories which don't need to be checked in to any version control.

Sharing own module across projects Android Studio

I'm struggling badly with moving from Eclipse to Android Studio.
Basically, I get that an Android Studio project is more like a workspace and a module more like a project...
However, in the Android Studio start page you can only create projects, so how do you share a module (i.e. a project in eclipse terms) across projects?
Basically, I have a number of apps that use a shared library I've created, in Eclipse all I do is flag it as a library and in each project simply link to it.
I have absolutely no idea how to do this in Android Studio. The examples for creating modules seem to take you as far as creating a module for no real purpose other than to use it within one app.
I first imported my library as a project in android studio, but that proved pointless, thinking that was how to do it because I want it kept separate in my version control system.
I then created a temporary module inside my app, but then it stores it within the project and in my other apps I cannot find a way to import the modules, so I really don't see what the point of a module is when it's embedded in a project and can't be separated or referenced anywhere else.
Thanks for any help.
Consider your library project name common-lib
Open build.gradle of the projects to which you want to add library add add the following
dependencies {
compile project(':common-lib')
}
and sync gradle

Resources