How can I use an application from another project as a library, when that application depends on a library that I am already using?
Here’s what I have:
MyProject
app
libraries
ExoPlayer
demo
library
Here's my current configuration as it pertains to this.
demo/build.gradle:
apply plugin: 'com.android.application'
...
dependencies {
compile project(':library')
}
library/build.gradle:
apply plugin: 'com.android.library'
MyProject/settings.gradle:
include ':libraries:ExoPlayer:library'
MyProject/app/build.gradle:
dependencies {
compile project(':libraries:ExoPlayer:library')
}
As you can see I’m using ExoPlayer library as a library (go figure) but I want to use demo application as a library as well (specifically for the DemoPlayer, I don't want the activities). But demo also has a dependency on library. What do I have to put in my gradle files to achieve this?
I tried to follow the library setup and apply it to demo but it broke the demo build:
Project with path ':library' could not be found in project ':libraries:ExoPlayer:demo'
I have tried following other similar threads such as this one but with no success.
I appreciate any help.
A long time ago I've use Exoplayer as a library project but it's not needed anymore : you can use gradle dependencies :
compile 'com.google.android.exoplayer:exoplayer:r1.4.2'
If you want to persist with library project, you will have to check your settings.gradle where all Android Studio referenced module are in. This file contain the name of module as you will use it after.
Example : (based on AndroidVuMeter)
settings.gradle
include ':app', ':vumeterlibrary'
project structure
Project/
app/
vumeterlibrary/
To use vumeterlibrary you will have to use compile project(':vumeterlibrary') in your app build.gradle
Related
I'm trying to create a project which includes an Android library and a Java library in Android Studio (3.1). The Java library depends on the Android library. Both are modules in my project like this:
MyProject
|-android
|-java
Both appear in settings.gradle:
include ':android', ':java'
And the Java library depends on the Android library like this:
java (build.gradle):
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':android')
}
...
android (build.gradle):
apply plugin: 'com.android.library'
...
When trying to sync the project I'm getting the following error:
Failed to resolve: project::android
Why?
P.S. The other way around (Android depending on Java) works just fine.
First let's try to fix the build error. Let's run ./gradlew build --stacktrace to see a more detailed output:
Caused by: org.gradle.internal.component.AmbiguousConfigurationSelectionException:
Cannot choose between the following configurations of project :androidLibrary:
debugApiElements
releaseApiElements
AGP is confused which variant to choose. After inspecting this answer, we can find how to overcome the issue:
implementation project(path: ':androidLibrary', configuration: 'default')
After trying to sync the project with that setup you'll see following output in console:
Ignoring dependency of module 'androidLibrary' on module 'javaLibrary'. Java modules cannot depend on Android modules
Thus, seems like what you try to do is not supported by the plugin.
Refer to similar question, where Nick Cardoso tries to clarify the case.
I am building android library project, which has a dependency on another internal library project.
I am wondering if there is a way to package a single AAR library, which already contains internal library inside it. I would like to share only 1 AAR library package to my application developers.
This is how my build.gradle files look currently, but currently they produce separate AAR files and both needs to be included in Application's build.gradle. As application is being built by another company, we need to share the final AAR file with them and not the complete library projects.
----- internalLib -------->>>>>>>>>>
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
----- externalLib --------
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile project(':internalLib')
}
There is no mechanism to combine library. It's a bit complicated as you probably want to control which dependencies get merged (for instance you probably don't want to include support-v4 in there). Also you'd need to merge the resources and Android manifest.
At this time there's no way to easily hack something, unless you are sure the resources have no conflicts between the two res folders (for instance you could have strings_a.xml in one lib and strings_b.xml in the other lib). This way you can just "merge" the two res folders by copying them both into the same location (as opposed to do a merge at the android res level).
For the Manifest it'd be more complicated, but doable with some custom code.
Providing a built-in mechanism for this is very low on our priority so don't expect it anytime soon.
For the sake you have to upload each library as separately on maven and use its implementation in parent library modules till the main library module. Only then when you publish your main library on maven will include your all child dependencies.
As far as we have only one option add aar as api dependency inside the module.
For that we have to generate aar file and publish it to Maven and make it accessible by another module and consume it in app.
https://developer.android.com/studio/projects/android-library
As mentioned above android developer document.
The library module with source code is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
If there anything else we can do, please let us know by jot down in the command section.
It is not supported
It is not recommended to include one library into another because it leads to a serious issues with managing versions and complexity of creating and supporting such solution.
You should stick to native approaches like dependency manager or rearchitect your codebase
[iOS Umbrella framework]
I am trying to add a library dependency from my Android Studio for below:
'com.google.android.gms:play-services-analytics:7.5.0'
However, I couldn't find the package.I even searched for it on search.maven.org but couldn't find it.However in the google docs it is mentioned :developers.google.com/android/guides/setup Can someone point the mistake I am doing?Meanwhile I acheived my task using this link
To make the Google Play services APIs available to your app:
Open the build.gradle file inside your application module directory.
Add a new build rule under dependencies for the latest version of play-services.
apply plugin: 'com.android.application'
dependencies {
compile 'com.google.android.gms:play-services:7.5.0'
compile 'com.google.android.gms:play-services-analytics:7.5.0'
}
Be sure you update this version number each time Google Play services is updated.
3.Save the changes and click Sync Project with Gradle Files in the toolbar.
I have imported one project into Android Studio but I got the error:
Could not find com.android.support:support-v4:19.1.0.
Where could I find this file? I have imported the project using Gradle.
I have the Android Studio version 0.5.7 the last android sdk and java 1.7u55.
Just add this code to you build.gradle file
dependencies {
compile 'com.android.support:support-v4:19.+'
}
and press Tools -> Android -> Sync Project with Gradle Files
Gradle will download necessary files by himself
It does not work for me either. It works with 19.0.1
But if (I use gradle) I do this in my build.gradle:
repositories {
def androidHome = System.getenv("ANDROID_HOME")
mavenCentral()
maven {
url "$androidHome/extras/android/m2repository/"
}
}
It finds the artifact.
From the SDK Manager, delete and re-install the Android Support Library 19.1 package.
I had this same problem with morning. I found the Jar file that I needed in /<MySdkFolder>/extras/android/support/ - in there are some sub folders with the different support libraries in them, so the last part of the path depends on which one that you want to use.
I just copied this into the lib folder of the project. I'm sure there is a more technical solution but it worked for me.
Following theory works at me:
Android Studio has problems importing support-v4:19.1.+ library when it comes through a transitive dependency.
Solution Adding support-v4 as own dependency and exclude this lib where it comes transitive. then i could not more see this import issue
Right clicking on the library and select the import as library option from the context menu works for me.
Try to go
Project Structure -> Dependencies -> Add : then select -> File
dependecies
then select the proper library
This artifact is available on google maven repository. So need to add following in the build.gradle:
allprojects {
repositories {
mavenLocal()
google()
}
}
I had a similar problem. This line to build.gradle works -->
implementation 'com.android.support:support v4:28.0.0'
I have a module with some POJO classes that is marked at gradle as apply plugin: 'java' .Is there a way to reuse it at another project ? Everything i tried failed . (I dont want to copy pasta it)
I was facing the same issue recently.
This is how I resolved the code redundancy problem:
Create a new Android Studio project 'libs' and add all my APIs in a 'library' module.
In build.gradle of your library module, add code to upload the artifact to local Maven repo.
`
apply plugin: 'maven'
group = 'com.<example>'
version = '1.0'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///Users/<myuser>/.m2/repository")
}
}
}
`
The archive is uploaded in your maven repo as aar file.
Use this aar file in any other project as a dependency.
Hope this helps.
Here are two other questions on the same matter.
How do I add a library project to Android Studio?
How to create a library project in Android Studio and an application project that uses the library project
Personally I didn't use any of the two provided methods.
I built my project as a JAR, added it to the 'libs' folder, right clicked on it and clicked 'Add as library' and then finally added the dependency in the gradle file like so:
dependencies {
compile files('libs/MyJAR.jar')
}