Using Gradle plugin from composite build in root project buildscript - android

I use Gradle Kotlin DSL and in my project I have separate build-dependencies gradle module which is included in settings.gradle.kts like so:
pluginManagement {
repositories {
google()
jcenter()
gradlePluginPortal()
}
}
includeBuild("build-dependencies")
This module contains empty plugin implementation and Deps object. Its build.gradle.kts below:
repositories {
google()
jcenter()
gradlePluginPortal()
}
plugins {
`kotlin-dsl`
`java-gradle-plugin`
}
gradlePlugin {
plugins.register("com.example.dependencies") {
id = "com.example.dependencies"
implementationClass = "com.example.dependencies.DependenciesPlugin"
}
}
Deps object:
object Deps {
const val buildTools = "com.android.tools.build:gradle:4.1.1"
// more dependencies
}
I want to use this Deps object in all modules by applying the plugin in build.gradle.kts files.
plugins {
id("com.example.dependencies")
}
And it works fine, I can import and use Deps object.
But there is a problem when I want to use it in root projects build.gradle.kts and more precisely in buildscript like so:
buildscript {
repositories {
google()
jcenter()
}
plugins {
id("com.example.dependencies")
}
dependencies {
classpath(com.example.dependencies.Deps.buildTools) // It doesn't work
}
}
Is there any way to use a custom plugin inside buildscript like this or could you suggest something to replace this approach?

I also wonder this. However, I think this is the difference between includeBuild and buildSrc.

Related

Packaging Library with another library aar in Kotlin(build.gradle.kts)

I am creating a library project (lets say B) and I have a dependency for another library (let's say A) which I have in the form of an aar (a.aar) file.
When I build b.aar should be packaged in a way that the a.aar will be bundled inside b.aar
Main Project is not controlled by us, so I do not have control to get a.aar while building the main project and they are looking for a way to bundle a.aar while we build b.aar.
Below is the b build.gradle.kts file.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.android.library")
kotlin("android")
id("gradle-****-android-module")
id("maven-publish")
}
group = "com.****.******"
version = "0.1-SNAPSHOT"
android {
compileSdk = 33
defaultConfig {
minSdk = 23
targetSdk = 33
consumerProguardFiles("proguard-rules.pro")
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
}
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.4.1")
androidTestImplementation("androidx.test:runner:1.4.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0-alpha06")
testImplementation("junit:junit:4.12")
implementation("com.google.mlkit:image-labeling:17.0.7")
implementation("com.google.android.gms:play-services-mlkit-text-recognition:18.0.2")
implementation("com.google.android.gms:play-services-mlkit-image-labeling:16.0.8")
implementation("com.example.servicelibrary:servicelibrary:1.0")
}
And in my repositories section, I'm trying to load the a.aar file from local maven.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
val agpVersion: String by project
val repositoryUrl = "file:${rootProject.projectDir.absolutePath}/repository"
repositories {
maven(url = repositoryUrl)
mavenLocal()
google()
mavenCentral()
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
dependencies {
val pmcModuleVersion: String by project
val kotlinVersion: String by project
classpath("com.android.tools.build:gradle:$agpVersion")
classpath(kotlin("gradle-plugin", version = "$kotlinVersion"))
classpath("com.****.gradle.plugins:gradle-****-android-module:$pmcModuleVersion")
}
}
allprojects {
val repositoryUrl = "file:${rootProject.projectDir.absolutePath}/repository"
buildscript {
repositories {
mavenLocal()
google()
mavenCentral()
maven(url = repositoryUrl)
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
}
repositories {
mavenLocal()
google()
mavenCentral()
maven(url = repositoryUrl)
maven {
url = uri("file://home/*******/.m2/repository/")
}
}
}
tasks {
val clean by registering(Delete::class) {
delete(builder)
}
}
Any inputs will be really helpful
In order to bundle LibraryA while building LibraryB, you will need to include LibraryA as a dependency in the build.gradle file of LibraryB.
Follow the steps mentioned below-
Paste ‘LibraryA’ inside the android’s project directory outside the app folder.
In the app level gradle i.e in 'build.gradle(:app)' file of LibraryB, add the following line to the dependencies section:
implementation project(':libraryA')
Make sure that the libraryA module is included in the 'settings.gradle' file of the project
include ':libraryA' //below include ':app'
If their are modules in LibraryA (Eg. Firebase or OneSignalConfig etc) and is mandatory to use then add the module's implementation also in the step 2 -
implementation project(':LibraryA:<module_name>') //add below implementation project(':libraryA')
example - implementation project(':LibraryA:Firebase')
Now try to build and run the app
IN CASE OF ANY DEPENDENCY ERROR WHILE BUILDING/RUNNING THE APP, TRY BELOW STEP
If using latest android structure then Add below code in 'settings.gradle' inside dependencyResolutionManagement{} and inside pluginManagement{}
repositories {
..
maven{url 'https://jitpack.io'} //add this below mavenCentral()
}
If you are using old android structure, then in project level gradle i.e in 'build.gradle(project_name)' add inside allprojects{}-
repositories {
..
maven{url 'https://jitpack.io'} //add below jcenter() and google()
}

How to configure gradle to retrieve all dependencies and plugins from a Nexus Repository

We have a React Native App and we have to download all the dependecies fo my App from the Company Nexus repository.
We have a maven gruop repository with lot of repository inside. One of them is a maven proxy repository that point to: https://plugins.gradle.org/m2/b
We configured our settings.grandle like this:
pluginManagement {
repositories {
maven { url 'URL_OF_NEXUS_GROUP_MAVEN_REPO' }
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
maven { url 'URL_OF_NEXUS_GROUP_MAVEN_REPO' }
}
}
We are able to download all dependecies from our repo but we encounter only a problem during build and compile: one of our dependencies in package.json is react-native-keychain and it has this build.gradle:
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'com.adarshr:gradle-test-logger-plugin:2.0.0'
}
}
apply plugin: 'com.android.library'
apply plugin: "com.adarshr.test-logger"
Why this setting is not overwritten like the others by our settings.gradle?
Thank you
I've solved deleting the changes in settings.gradle ad creating a init.gradle in HOME/.gradle with this contents:
allprojects {
ext.RepoConfigurator = {
maven {
url = uri('URL_OF_NEXUS_GROUP_MAVEN_REPO')
}
}
buildscript.repositories RepoConfigurator
repositories RepoConfigurator
}
apply plugin: EnterpriseRepositoryPlugin
class EnterpriseRepositoryPlugin implements Plugin<Gradle> {
void apply(Gradle gradle) {
gradle.settingsEvaluated { settings ->
settings.pluginManagement {
repositories {
maven { url 'URL_OF_NEXUS_GROUP_MAVEN_REPO' }
}
}
}
}
}

How do I publish subproject to Maven with subprojects buildscript gradle version?

I want to publish some subproject to maven by using maven-publish plugin. The current Gradle script looks like this.
apply plugin: 'maven-publish'
group "com.huawei.quickapp"
version "1.0-SNAPSHOT"
publishing {
publications {
publishTask(MavenPublication) {
}
}
repositories {
maven {
url = uri("${rootDir}/local_repo/repos/")
}
}
}
}
static Boolean publishIfNeeded(taskName){
println("name = " + taskName)
def list = ["baselibrary", "corelibrary"]
if (taskName in list) {
return true
}
return false
}
I just want two libraries,core and base to be published. When I submitted the code to the pipeline, the CI suggested that the NDK version did not match, but there was no NDK-related configuration in my code. The upgrade is necessary because only versions greater than 3.6 support the use of From Components.Debug or some other build variable in gradle script.I tried configuring the build script for Subproject separately like this:
buildscript {
ext.kotlin_version = "1.4.21"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.5.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
subprojects {
buildscript {
ext.kotlin_version = "1.4.21"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
}
}
}
but it still not work. I got this error
Could not get unknown property 'Debug' for SoftwareComponentInternal set of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer.
Is there a way to publish a submodule using the buildScripte of a submodule? thx.

How to resolved include module Gradle 6.x?

I'm using Gradle 6.6 and have a project include 3 module: app, utils and config; and gradles file
-app
-utils
-config
---Versions.kt
-build.gradle.kts
-settings.gradle.kts
Versions.kt
class Versions {
val kotlin = "1.3.72"
}
settings.gradle.kts
include("app", "utils", "config")
build.gradle.kts
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath(kotlin(module = "gradle-plugin", version = Versions.kotlin))
}
}
allprojects {
repositories {
google()
jcenter()
}
}
This problem is Versions.kotlin in build.gradle.kts file Unresolved reference, although I include config module in settings.gradle.kts
If you understand my problem, please help me, thanks so much!

Could not find method kotlin() for arguments [gradle-plugin, 1.3.20]

I've copied exactly what the kotlin gradle docs say to implement kotlin gradle plugin, however it's returning the following error:
Could not find method kotlin() for arguments [gradle-plugin, 1.3.20] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
my gradle
buildscript {
ext.kotlin_version = '1.3.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath(kotlin("gradle-plugin", version = "1.3.20"))
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
// ext {
// navigationVersion = '28.0.0'
// }
plugins {
kotlin("<...>")
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Any idea?
build.gradle can be written in Groovy or Kotlin. You're typing your build.gradle in Groovy (not Kotlin), but you have copied the Kotlin code. You should select the Groovy tab on the documentation:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.3.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20"
}
}
Here: https://kotlinlang.org/docs/reference/using-gradle.html#targeting-android
You can also find a decent example by creating an empty Android and Kotlin project.

Categories

Resources