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')
}
Related
I'm trying to add this library to Android Studio locally using this tutorial on YouTube.
However, I get errors that I can't post here because they're too many (99+).
Can someone tell me step by step (in detail) how to succesfully add this library to my project and be able to edit it afterwards?
This is the main error I get when I try to import this library: `Unable to determine constructor argument #1: missing parameter of type Factory, or no service of type Factory.
My build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Here is your step-by-step guide:
Create an empty project or open your existing project where you want to add this library.
Clone the library project from the git or download the zip and extract it in some other directory.
Now, in Android Studio, go to File -> New -> Import Module.... Select the cloned/extracted library directory. Make sure the :placepicker module is selected for import. Then click Finish.
Now copy two files bintray.gradle and install.gradle from cloned/extracted project root directory to your own project root directory. Add this point your project structure should look like this:
Now open your project level build.gradle. Add ext.kotlin_version = '1.3.72' inside buildscript { and also add following dependencies:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
Overall, this will look like:
Finally, go to File -> Sync Project with Gradle Files. It will take some time to download the missing dependencies and you have now successfully integrated the library within your project which is fully editable.
Now to make this library work in your own app module, in the build.gradle file of your app module, add this inside dependencies section:
api project(':placepicker')
Or edit your gradle.build like in the picture:
Step by step description:
Then ceck out the git project to your machine.
Modify and build it with a new Version.
Add the dipendency as Jar from Your local filesystem in Android Studio.
Try this:
File > Project Structure > Dependencies Tab > Add module dependency (scope = compile)
Where the module dependency is the project library Android folder.
In build.gradle(Module.app) file add this.
implementation 'com.google.android.gms:play-services-places:17.0.0'
let me knew if it's work.
I have an Android closed source module that will be be used as a library in other projects. It contains external dependencies.
To publish it, I'm creating a Maven artifact with the following gradle task:
apply plugin: 'maven'
def coreAarFile = file('...build\\outputs\\aar\\android-sdk-release.aar')
artifacts {
archives coreAarFile
}
uploadArchives {
repositories.mavenDeployer {
repository(url: "file://.\\mvn-repo")
pom.project {
groupId 'a.blabla'
artifactId 'blabla-sdk'
version "1.0.0"
}
}
}
It generates the .aar file, the pom.xml, etc without problems.
Then I create a project that have a dependeny to my library declared. It works until it needs to access to the external dependencies, when throws a class not found exception.
How can I edit my gradle task to include external dependencies or at least a reference to them? They are published in mvnrepository.com and github.com.
I moved uploadArchives to the build.gradle of the module and removed the artifacts element. It works!
Thanks to CommonsWare for pointing to the right direction.
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
How can I reference, add, link, depend on java classes defined in a different project or library without copy / paste?
For:
Android Studio
IntelliJ IDEA
Android Studio
AndroidProjectRoot/settings.gradle
Before
include ':app'
After
include ':app', ':common'
project(':common').projectDir = new File('../common')
AndroidProjectRoot/app/build.gradle
Before
apply plugin: 'com.android.application'
android {
...
}
dependencies {
}
After
apply plugin: 'com.android.application'
android {
...
}
dependencies {
compile project(':common')
}
THEN...
Tools -> Android -> Sync Project with Gradle Files
Allows Android Studio project to reference an external project (at the same directory level as AndroidProjectRoot/, without making a copy of the Java Library inside your Android Project.
Library Project / Module
You'll need a basic build.gradle for the Library/Module. The following will suffice. The dependencies are just for example (only use as appropriate), in case your module is a bunch of objects being handled by a DAO like OrmLite.
Library Project Root/build.gradle
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
repositories {
mavenCentral()
}
dependencies {
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-jdbc:4.48'
}
IntelliJ IDEA
Reference Library as Module
File -> Project Structure
(Left Column) Select "Modules"
(Middle Column, top left) Click + button
Import Module
Select top-level directory that contains Java Library, e.g. common (see note below)
Click OK
Add Dependency so you can use import statements
(Middle Column, list of Modules) Select your main module (not the one you just added)
(Right rectangular area, bottom left) Click + button
Select 3 Module Dependency...
Select your added module (e.g. :common)
Click OK
Click OK (closes Project Structure)
IDEA should now rebuild gradle and add your library as a module to the project structure with a folder icon (with tiny blue square) beside the name
Note:
If your Library is structured like:
common/
common/build.gradle
common/src
common/src/java
common/src/java/main
common/src/java/main/com
common/src/java/main/com/your
common/src/java/main/com/your/package
common/src/java/main/com/your/package/YourClass.java
Further Reading
Mathias Hauser - Spring Boot JPA Multiple Projects
You need to import the pojo project as a module for the android project:
All you need to do is open one project, and then "Import Module", so you select the other "Project", this way they will be able to see each other resources and codes.
From there it would be as easy as using an import as long as the access modifiers allow you to do such import (public for example)
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'