How to install my own Java library? - android

I created a Java class for helping me to use some common debuggin stuffs, for example to get the type of value, I have a method call typeOf()
Help help = new Help();
String s = "Something";
help.alert(help.typeOf(s));
above is a sample code in my class
Now every time I start a project, I need copy the class file to my project, so I want to know is there any way I can install this in my OS, then can import it to any of project whenever I needed.
In C you can move the header file to a location, then you can #include it whenever you want

If you using gradle, you can learn it from Building your own Android library but need to publish your library to jCenter or Maven Central.
Fortunately, you can make the library as a local artifact using maven plugin in the library. Read more at Deploying an Artifact to the Local Cache in Gradle
In your project, you need to add mavenLocal() to the repositories in your root build.gradle:
repositories {
mavenCentral()
mavenLocal()
}
And add the dependencies to your project build.gradle:
dependencies {
...
compile 'com.your.library:x.y.z'
...
}
But if your project is shared in your local network, you need to use Repository management like Artifactory by JFrog or Nexus Repository Manager by Sonatype.

Related

Gradle Android repositories url issue

I have a library module where I used a dependency which is based on a private maven repository, I can use this dependency in my library and everything works well.
The problem occurs when I try to use the library module inside my application, the build is failing, for some reason is looking to get this dependency from a different maven repository (in this case, my personal one, which is available only in my app)
I've switched from implementation to api and viceversa, just so check if this might solve the issue, but it is not.
If I add the maven repository url also in my app, everything works well, but I don't what that. Is this the expected behaviour?
Thanks!
Yes, repositories declared in one subproject are not shared with other subprojects. Other than with Maven, repositories are also not taken from a dependency POM file or otherwise inherited from a dependency. On this topic, the Gradle docs state the following:
Strict limitation to declared repositories
Maven POM metadata can reference additional repositories. These will be ignored by Gradle, which will only use the repositories declared in the build itself.
However, you can probably centralize the repository declaration in your top-level settings.gradle(.kts) file:
dependencyResolutionManagement {
repositories {
// TODO Configure the shared repository here.
}
}
You can find more details on this centralization in the Gradle docs.

How to implement implementations in android without error?

I want to add AdView to my activity and I click on download and then it automatically implements the necessary library. But then I get the following error:
Error 1
I get the same error when I try to add GridLayout to my library.
Here is my gradle file: gradle file
I already tried to set the Global Gradle settings to offline work but it doesn't help.
I have android studio 3.4.2
This is a problem that bothers me for a while now and there seems no solution to this. Is it possible that these things are not available for androidx yet? Thank you for your help!
From the documentation:
To make the Google Play services APIs available to your app:
Open the build.gradle file inside your application module directory.
Note: Android Studio projects contain a top-level build.gradle file and a build.gradle file for each module. Be sure to edit the file for your application module. See Building Your Project with Gradle for more information about Gradle.
Add a new build rule under dependencies for the latest version of play-services, using one of the APIs listed below.
Ensure that your top-level build.gradle contains a reference to the google() repo or to maven { url "https://maven.google.com" }.
Save the changes, and click Sync Project with Gradle Files in the toolbar.
You can now begin developing features with the Google Play services APIs.
This is often a common mistake. If you have multiple repositories in your gradle, make sure that maven google is at the top of the list.
eg.
repositories {
maven { url "https://maven.google.com" }
...
}
This package:
// https://mvnrepository.com/artifact/com.google.android.gms/play-services-ads
implementation "com.google.android.gms:play-services-ads:18.1.1"
is either available from repository google() or from repository mavenCentral():
repositories {
google()
mavenCentral()
...
}
see the quick-start.

where to put Drive API Client Library for Java to use in my android project

I encounter error on compilation android application like this
so I found that I need to download
Drive API Client Library for Java
what I wondering is what file I want to include in my project and where to put it in.
Try adding the maven central repository to your build.gradle, so it automatically gets downloaded from there on build.
repositories {
mavenCentral()
}
dependencies{
....
}
https://developers.google.com/api-client-library/java/apis/drive/v2#add-library-to-your-project

Android vs. Maven Internal Repositories

Say I need to use some proprietary jars in my android library. I want my library to be conveniently available from Maven Central, but I can't just put dependencies there due to legal issues.
I figured it's possible to use Internal Repository to host dependencies so they would be resolved automatically.
I've used Github repo, just like this one, and declared it in library pom.xml
However Gradle doesn't seem to be resolving this dependencies. If I manually declare my repository in main build.gradle everything works fine. Am I doing something wrong here, or android gradle plugind just don't support internal repositories?
It's discouraged in the Maven community to have repository declarations in published POMs, and Gradle won't honor them. Instead, downstream builds will have to declare the internal repository in one way or another (which shouldn't be a big deal).
If you (only) publish POMs for proprietary dependencies to Maven Central (which is a common solution to this problem at least if you own the dependencies), downstream builds will need to declare the internal repository as follows:
repositories {
maven {
url "https://repo1.maven.org/maven2"
artifactUrls "https://some.internal.repo"
}
}
If you don't publish proprietary dependencies to Maven Central at all (not even POMs), downstream builds will have to declare the internal repository as another regular Maven repository:
repositories {
mavenCentral()
maven {
url "https://some.internal.repo"
}
}
Note however that last time I checked, one of the rules of publishing to Maven Central was that dependencies needed to be available from Maven Central as well.
PS: Whether you are publishing a Java or Android library shouldn't matter here.

Gradle dependencies cache in project directory

I have some dependencies from Maven Central Repository in my project. So I thought what if any dependency will be removed from Maven Central and other repositories (GitHub) and I will not be able to find this dependency somewhere and build my project?
I want to add local dependency cache to git repository of my project, so anyone could clone this repository and build my project offline. May be in Cocoapods way. How can I do this?
You already have a local maven repo, add mavenLocal() under project's repositories section. I doubt if any dependency from maven or other public hosted repos could be removed but the best way to prevent yourself from this is to host own repo - artifactory for instance. Other way is to add all the dependencies to version control system and configure gradle to read dependencies from flat file repository. Here is the whole chapter on repositories in gradle docs.
In an enterprise environment you would use a repository manager like Archiva, Artifactory or Nexus. You can configure these tools to behave as a proxy for public repositories so that all needed artifacts are cached on demand.
These tools are installed to a server so that every developer can access them and the build is set up to only have this repository so that all artifacts are cached if they aren't already available in the cache.
There's a page at Codehaus comparing the above mentioned tools.
If a repository manager is too much for you, you have to manage the local repository by yourselves.

Categories

Resources