How to access LibraryExtension in custom gradle plugin - android

I would like to write a custom gradle plugin that manipulates either com.android.build.gradle.AppExtension or com.android.build.gradle.LibraryExtension. The basic form of the plugin is:
class AndroidLibrary : Plugin<Project> {
override fun apply(project: Project) {
var lib: LibraryExtension = project.extensions.getByName("android") as LibraryExtension
lib.minSdkVersion = "26"
}
}
The problem is that the class LibraryExtension cannot be resolved. That class is contained in "com.android.tools.build:gradle:7.1.2". Note, there is no problem accessing either of these classes in a gradle.build.kts script file. I just can't access either of those classes from within a custom plugin like seen above. I've tried adding a dependency to com.android.tools.build.gradle 7.1.2 in the build script, but that doesn't work. I also tried adding it as a buildscript dependency, and that didn't help either. I also got the
com.android.tools.build:gradle jar file and added it as a dependency, but that too didn't work.
It seems to me to be reasonable to expect a custom plugin of being able to manipulate the android build settings but I just can't find a way to resolve either LibraryExtension or AppExtension from within the custom plugin project.
If someone knows what I need to do to resolve those two classes, that would be greatly appreciated. I'm currently under the impression, that this is an architectural limitation of with android's gradle plugin and that I really can't access the "android" build section using LibraryExtension or AppExtension from a custom plugin and dsl. If that is the case, that too would be useful to know.

check this answer for your question, it is gonna solve your problem.

Initially I was building the plugin using IDEA. I could build the project by using a "gradlew build" command. I noticed that I would get the following message whenever I tried to sync the project:
This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.1.1 or newer
I had been ignoring that issue since I could successfully build the project using gradle commands. Here's a link the explains what the message means: https://intellij-support.jetbrains.com/hc/en-us/community/posts/4405168877202-This-version-of-the-Android-Support-plugin-for-IntelliJ-IDEA-or-Android-Studio-cannot-open-this-project-please-retry-with-version-2020-3-1-or-newer-
One of the suggestions to this problem was to build the project in Android Studio instead of waiting for IDEA to incorporate later android build. I did that and that indeed fixed that issue. The additional side effect was that I could now build the plugin. I was able to add dependencies for both gradleApi and to com.android.build.gradle and was able to resolve AppExtension of LibraryExtension. I had added the same dependencies I when I was using IDEA IDE. I had been customarily building my custom gradle plugin using IDEA. This was my first custom gradle plugin that I was going to manipulate an "android" build configuration.

Related

Class not resolved when implementing custom library in Gradle Android

I have created an android library and published it in Jitpack.
Currently, my build is getting success in Jitpack. but when I add the implementation URL in my project build Gradle gets success but when I try to use library classes it does not get resolved
In the external library tab located in the project structure, it does not have the library which I have implemented so might be .jar file is not getting generated.
Help me If you have any solution or any way to identify this issue.
If I understand correctly you say that the gradle build or 'project sync' after a change in the gradle files works fine. But you cannot use any of the code from your library.
Can you verify that your library is loaded in the External Libraries?
It would be something with your repository name in it and ends with '#aar' (eg. com.github.nickname:libraryname:version#aar)
If you cannot find it, please check if your library module build.gradle has the line below somewhere at the top of the file.
apply plugin: 'com.android.library'
The issue was the Repository was private so we need to provide an authentication token. The reference link is given below
https://jitpack.io/docs/PRIVATE/

How to know if there's a dependency update using Gradle Version Catalog

My project is using now Version Catalog for all the Gradle modules, and now using the type safe declaration of dependencies in the build.gradle file I don't get any suggestions from the IDE when there's an update of a specific dependency.
What would be the best approach to know if there's an update of a dependency, instead of checking manually one by one?
My recommendation is to use the gradle-versions-plugin or this extension of it (https://plugins.gradle.org/plugin/se.ascp.gradle.gradle-versions-filter). Both add an additional gradle task dependencyUpdates to your project that will tell you which version updates are available.
Also works fine in combination with the new versions catalog feature you mentioned. I am using it too.

Gradle Kotlin DSL: Inspection "Newer Library Versions Available" not working

I have converted my Gradle build scripts to the Kotlin DSL. Since I was starting with a small new project, everything went according to plan. When referencing more and more dependencies I wanted to put their version numbers in the script as constants, especially for those versions which are used in several places.
In my app/build.gradle.kts I have basically the following:
dependencies {
implementation("androidx.appcompat:appcompat:1.0.0")
...
}
Android Studio inspections tell me, that I should upgrade to 1.1.0. I changed it to
val appCompat = "1.0.0"
dependencies {
implementation("androidx.appcompat:appcompat:$appCompat")
...
}
but now I do not get that inspection hint anymore.
I compared my Kotlin script to what I find in the Sunflower reference project and found it to be working there. So, I experimented with defining extra-values with
extra.apply {
set("appCompat", "1.0.0")
}
implementation("androidx.appcompat:appcompat:${extra["appCompat"]}")
but got no inspection hint either.
To me, it seems that the inspection is broken using the Kotlin DSL. Do you agree or do you have a working setup for this?
In my environment inspection is broken also. So I used from third-party plugin and run it task in some interval for checking available update and manage it.
More details: https://github.com/jmfayard/gradle-dependencies-plugins

Kotlin class NoClassDefFoundError crash

I have an existing Android Project that uses the following libs:
AutoValue
Dagger2
RxJava
Retrolambda
I am trying to add Kotlin Support so that I can slowly migrate the project over to Kotlin.
Here is what I have done.
Added Kotlin dependency.
Converted one of the classes over to Kt class and moved over to src/main/kotlin/..package..
Added kotlin in source set.
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
When I do a clean build and deploy the app, I get NoClassDefFoundError exception. However, If I deploy again it works just fine.
Any suggestions? I do not have any annotation in the class I converted so I did not apply kapt plugin.
Note: I am using latest kotlin 1.0.4. Also I have instant run disabled.
Go to Files > Settings and turn off completely Instant Run, I mean all checkboxes should be unchecked.
Clean and rebuild project.
Then it should work
Edit: As you said that Instant is disabled - using protip check your configuration and update Gradle and Android Studio if you're not using the latest.
According to this issue, changing Gradle plugin version from 2.10 to 2.14.1 may help.
Protip:
Use combination Ctrl+Shift+A to find commands like
Configure Kotlin in Project
Configure Kotlin Updates
Convert Java File to Kotlin
first you need to configure you android studio with kotlin , for this need to add kotlin plugin in android studio, and then need to add dependencies and then convert java classes to kotlin please have look this video.
https://www.youtube.com/watch?v=DN2EDdycZ3c
Suffix the Kotlin classes with kt when calling them from Java.
Otherwise add #file:JvmName("Utils") before the package in the Kotlin class if you must call it in java with whatever custom name you wish like Utils in this case.
more details can be found here

android studio cannot resolve import org.json.JSONObject

I used libgdx to build my project and I am having issues using the JSONObject class. When I add import org.json.JSONObject, it says it cannot resolve. How do I add that library to my project?
Here is what I have tried without success:
I downloaded the json-simple-1.1.jar and put it in core/build/libs folder. Could not use import.org.json.simple.JSONObject either.
When browsing the tree in Android Studio as "Packages" dropdown, I can see the classes I want under "Core -> Libraries -> org -> json" but I cannot add them to my project. I get a "cannot refractor, class is in a jar file" error.
Is there something I have not tried yet to solve this issue? I feel I will run into this again as I try to use other external libraries.
Thank you.
If you are using LibGdx, it does not come with a Gradle setup, making multi-project builds streamlined.
You need to add implementation 'org.json:json:<version>' to the dependencies block of your top-level build.gradle.
To find the latest version, see http://mvnrepository.com/artifact/org.json/json/
For example, using the latest version at the time of writing:
dependencies {
implementation 'org.json:json:20200518'
}

Categories

Resources