How to access extra properties from project build.gradle in modules - android

So I'm trying to setup Maven Publish in my library which has several modules in it.
I am following this tutorial since the whole process is imo not that well documented on the Android Documentation. However, I am stuck at the point Customizing POM File Generation — Basics. I added the external info in my project build.gradle and try to add the publishing information in the modules build.gradle. When I try to sync Gradle I get the following error:
A problem occurred configuring project ':projectname'.
> Failed to notify project evaluation listener.
> Cannot get property 'pomGroupID' on extra properties extension as it does not exist
> Cannot get property 'pomGroupID' on extra properties extension as it does not exist
The code in my build.gradle files looks like this:
Project:
buildscript {
// POM information for publishing the library
ext {
pomVersion = '4.23'
pomGroupID = "com.randomname.stackoverflowquestion"
}
....
Module:
project.afterEvaluate {
publishing {
publications {
maven(MavenPublication) {
groupId project.ext.pomGroupID
artifactId project.name
version project.ext.pomVersion
artifact(bundleReleaseAar)
}
}
}
}
Any idea on what could be the problem in my case?

The problem was very simple. Only had to change
project.ext.pomVersion
to
project.pomVersion

Related

Android multi module libraries

How can I build all Android modules in project and expose those to maven repository without creating huge gradle script? There are no clear instructions about modularity in Android projects with artefacts generated by gradle maven plugin. It would be awesome to use built in task.
In few steps:
Create a common gradle file in root directory, such as config.gradle and put this fragment:
ext.packageName = "com.abc.example"
ext.mavenPublisher = {
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
afterEvaluate {
publishing {
publications {
maven(MavenPublication) {
from components.release
artifact androidSourcesJar
groupId = packageName
artifactId = findProperty('moduleName')
version = getVersion()
}
}
}
}
}
In root gradle.properties add property version=1.0.0.
In library module build.gradle add:
apply from: "${rootProject.projectDir}/config.gradle"
with mavenPublisher
Also in library module add gradle.properties with:
moduleName=name
Type in console from root project directory: gradle clean build publishToMavenLocal.
In section from components.release you can change release type to debug or another one build type. Also you can add version to each module if you want, but probably some release plugin configuration is required.
Working example at this link.

Module library Jar not shown in External Libraries in Android Studio

I am having trouble getting a jar library to show up under External Libraries in Android Studio. I am trying to add javadoc for this library, and the only method I've found online is to right click on the library in External Libraries and select Library Properties....
The project structure is a tree of many modules:
rootsdk /
main.jar
main-javadoc.jar
plugins /
plugin1 /
build.gradle
...
plugin2 /
build.gradle
...
...
The dependency is declared in the build.gradle files like:
compileOnly files('../../main.jar')
If I open up the individual directories plugin1, then the dependency shows up in External Libraries correctly. But if I open up the rootsdk project, it does not appear. All of the modules are listed and compilable from the root project, and I can use classes defined in the library just fine, but it does not appear under External Libraries, so I cannot add the javadoc for it.
The strange thing is some of the plugins use other libraries, but defined differently:
repositories {
flatDir {
dirs 'libs'
}
}
...
implementation(name: 'core-debug', ext: 'aar')
And these libraries show up under External Libraries as expected.
Is there something missing to force main.jar to show up under External Libraries, or is this a bug in AS?
It's a silly thing, but if you make it an Ivy or Maven repository, it should work. files don't. Both of the below solutions should support -sources and -javadoc suffixes. I think IDEA only implemented artifact resolution from repositories, and didn't think about direct file references.
Ivy
repositories {
def repoRoot = file(rootProject.projectDir)
ivy {
name = "local libs"
url = repoRoot.toURI()
patternLayout {
artifact("[module](-[classifier]).[ext]")
}
metadataSources {
artifact()
}
}
}
dependencies {
// `local` group and version `0` are just a hack so Gradle dependency notation can be used.
implementation("local:main:0")
implementation("local:core-debug:0#aar")
}
Maven
You can do something similar to above, but the repo structure is less flexible. You need to move the .jar/.aar files around. I recommend creating a folder for them (even if there's one right now). In the example I called it libs.
repositories {
def repoRoot = file(rootProject.projectDir.resolve("libs"))
exclusiveContent {
// Work around
// > Could not GET 'https://repo.gradle.org/gradle/libs-releases-local/local/main/0/main-0.pom'. Received status code 409 from server: Conflict
// by not allowing Gradle to contact other repositories for "local" files.
filter {
includeGroup("local")
}
forRepository {
maven {
name = "local libs"
url = repoRoot.toURI()
metadataSources {
artifact()
}
}
}
}
}
dependencies {
// `local` group and version `0` are just a hack so Gradle dependency notation can be used.
implementation("local:main:0")
implementation("local:core-debug:0#aar")
}
The error message will tell you where to put it, for example:
* What went wrong:
Execution failed for task ':...'.
> Could not resolve all files for configuration ':...'.
> Could not find local:main:0.
Searched in the following locations:
- file:/.../libs/local/main/0/main-0.jar
Required by:
project :...
> Could not find local:core-debug:0.
Searched in the following locations:
- file:/.../libs/local/core-debug/0/core-debug-0.aar
Required by:
project :...
Note that while this is more complex, it's also more flexible, because you can hand-write or download POM files and therefor include transitive dependencies for the local JAR files, just remove the metadataSources block if you have POM XML files for your artifacts.
metadataSources
This magic is worth a mention (docs):
metadataSources {
artifact()
}
it tells Gradle, that there's no metadata (POM or Ivy XML file) associated with the artifacts in the repository, that there's only artifacts exists. Without this, it would fail by looking for metadata.

JitPack: Failed to resolve subrepo

I'm using jitpack to in my gradle as follows:
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
And in my github repo I have a sub-module called authentication
I need to use only this module in my project, so I added this to my module's build.gradle as mentioned here:
compile 'com.github.MotassemJa.MoAuth:authentication:0.0.5'
But I keep getting
Error:(23, 13) Failed to resolve: com.github.MotassemJa.MoAuth:authentication:0.0.5
As you can see at https://jitpack.io/com/github/MotassemJa/MoAuth/0.0.5/build.log, 0.0.5 was built from commit ebb529c949bf7d40815c815d051d45407e8f7f71 which is 0.0.5~2 and there the module was named moauth2.0.
It seems you moved the tag and thus the release after it was compiled and cached by JitPack.
It is always a very bad idea to modify releases after they are done instead of releasing a new version.
I don't know whether you can make JitPack forget the cache and rebuild your application, but maybe it would be best to move the 0.0.5 release back to where it was and create a 0.0.6 release.
You might be able to make JitPack re-build your app if you log into jitpack.io with GitHub and delete the existing build. The FAQ are not too clear on that. It could be that this only works for failed builds.

System cannot find specified file Gradle Android

I have downloaded a project from here
I have android studio, Git, Gradle installed on my system.
After I import the project , while gradle is syncing it says me
" Gradle Signal-Android-master project refresh failed "
Error : CreateProcesses error=2 , The system cannot find the file specified "
I have googled alot but could n't find anything helpful.
Which file ? it doesn't state anything , I have seen the IDE logs as well but nothing there, any help in this regard ?
Thanks
If you are trying to get github dependency in you project. First you have to specify the repository in build.gradle and then add that dependency in you project.
What you can do for git repository
Step 1. Add the JitPack repository to your build file Add it in your build.gradle at the end of repositories:
https://jitpack.io
repositories {
// ...
maven { url "https://jitpack.io" }
Step 2. Add the dependency in the form
dependencies {
compile 'com.github.User:Repo:Tag'
}

Collect dependencies with gradle android plugin

Is it possible to collect all dependencies into one place as jar-files in Gradle Android project?
I tried to use: Gradle: collect application dependencies
But Gradle returns error:
Could not find property 'testRuntime' on configuration container.
Well testRuntime is a configuration that's seems not to be available in your project. to collect all configurations in a single folder you can do something like this:
task collectJars(type: Copy){
into "$buildDir/output/lib"
configurations.all{ config ->
from config
}
}

Categories

Resources