How do I add KSP plugin to android project gradle? - android

My aim is to add the KSP plugin to gradle as stated in the question heading.
just go through the line below each //from quickstart comments and you'll see all the error origins.
To add the dependencies I referred the Kotlin KSP quickstart documentation.
Now, the code might not be an exact replica of the one from the docs because that didn't work and so, I attempted some failed changes to try and make it work.
build.gradle(app/module) -
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
//from quickstart
id 'org.jetbrains.kotlin.jvm'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.cryptile"
minSdk 26
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
implementation 'com.google.android.material:material:1.5.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
//what I want to implement
ksp "androidx.room:room-compiler:$room_version"
//from quickstart
implementation 'com.google.devtools.ksp:symbol-processing-api:1.6.10-1.0.2'
}
build.gradle(project) -
//from quickstart
buildscript {
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'
}
}
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
//from quickstart
id 'org.jetbrains.kotlin.jvm' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
settings.gradle -
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "CRYPTILE"
include ':app'
Any help is appriciated.

KSP guides failed to mention where to reference your processor. Use META-INF resource directory to reference your processor processor in your processormodule:
src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
In this file you reference your ProcessorProvider:
com.my.package.processor.MySymbolProcessorProvider
This is important step in order your processor provider be recognized and later instantiate the actual processor.
If your dependencies setup are not in place please read further here https://medium.com/#nikoladespotoski/hands-on-google-ksp-c0508d623863

Related

Gradle Could not find com.github.PhilJay:MPAndroidChart:v3.1.0

I have a problem I want to add com.github.PhilJay:MPAndroidChart:v3.1.0. implementation class but gradle reports that it does not find it despite the fact that I added the repositories and the class implementation in both gradle.
I have updated the version of the gradle but gradle still can't find com.github.PhilJay:MPAndroidChart:v3.1.0. implementation
Here's my project level's gradle file :
plugins {
id 'kotlin-android'
id 'com.android.application'
id 'kotlin-kapt'
id 'com.google.gms.google-services'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.wheeler"
minSdk 23
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
dataBinding = true
}
}
dependencies {
implementation 'com.google.firebase:firebase-storage-ktx:20.1.0'
implementation 'com.google.firebase:firebase-database-ktx:20.1.0'
def lifecycle_version = "2.2.0"
// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version")
// LiveData
implementation("androidx.lifecycle:lifecycle-livedata:$lifecycle_version")
def room_version = "2.4.2"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.android.gms:play-services-auth:20.4.0'
implementation 'com.google.firebase:firebase-database-ktx'
implementation platform('com.google.firebase:firebase-bom:31.1.1')
implementation 'com.google.android.material:material:1.6.0'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}
Here's my app's gradle file :
buildscript {
repositories {
google()
mavenCentral()
jcenter()
maven { url 'https://jitpack.io' }// Warning: this repository is going to shut down soon
}
dependencies{
// Add the dependency for the Google services Gradle plugin
classpath 'com.google.gms:google-services:4.3.14'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath 'com.android.tools.build:gradle:7.2.1'
}}
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
What's the solution?
You need to add the maven { url "https://jitpack.io" } line to settings.gradle now (depending on the repositoryMode - new projects in Android Studio default to PREFER_SETTINGS, which means it looks for repositories in settings.gradle instead of the other gradle files).
For example:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
maven { url "https://jitpack.io" } // add this to get MPAndroidChart
}
}

Why <id "org.jetbrains.kotlin.kapt"> fails but <id 'kotlin-kapt'> works?

I'm new to kotlin Android, today I want to build a database with room based on kotlin.
I first to the offical guide of using room with kotlin.
As the offical guide says, The dependencies for room are:
def room_version = "2.4.3"
implementation 'com.google.devtools.ksp:symbol-processing-api:1.7.20-1.0.7'
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// To use Kotlin annotation processing tool (kapt)
kapt "androidx.room:room-compiler:$room_version"
So I add them to my module gradle file and try synchronizing
Obviously, the building fails because I didn't enable kapt
So I refered to the officail guide of kapt:
as the officail guide of kapt says, I only need to add:
plugins {
id "org.jetbrains.kotlin.kapt" version "1.7.21"
}
to my module gradle file.
I did so but the building process failed, it gave the reason:
Error resolving plugin [id: 'org.jetbrains.kotlin.kapt', version: '1.7.21']
> The request for this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so compatibility cannot be checked.
So I searh kapt withgoogle ang I got Could not find method kapt() for arguments
Among the answers of the question, I try:
plugins {
...
id 'kotlin-kapt'
}
And it worked.
I want to know the reasons.
Appendix:
module gradle file:
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id "org.jetbrains.kotlin.kapt" version "1.7.21"
}
android {
namespace 'me.rexjz.database'
compileSdk 33
defaultConfig {
minSdk 28
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
def room_version = "2.4.3"
implementation 'com.google.devtools.ksp:symbol-processing-api:1.7.20-1.0.7'
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// To use Kotlin annotation processing tool (kapt)
kapt "androidx.room:room-compiler:$room_version"
implementation 'androidx.core:core-ktx:1.9.0'
testImplementation 'junit:junit:4.13.2'
// implementation project(":common")
}
project gradle file:
buildscript {
ext {
compose_version = '1.3.1'
compile_version = 33
}
repositories {
google()
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
settings.gradle:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "CellExperimentAssistant"
include ':app'
include ':authentication'
include ':database'
include ':common'
I want to know Why id "org.jetbrains.kotlin.kapt" fails but id 'kotlin-kapt' works?

Passing Data Between Fragments Using Safe Args

I'm trying to implement data transfer between fragments using Safe Args, but I'm stuck at the moment of setting Gradle.
Project Gradle looks like this
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module Gradle is like this
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.main_fragmentargs"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.navigation:navigation-fragment-ktx:2.5.0"
implementation "androidx.navigation:navigation-ui-ktx:2.5.0"
}
Wherever I enter this line, Gradle starts to swear.
apply plugin: "androidx.navigation.safeargs.kotlin"
Similar story with
def nav_version = "2.3.3"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
Please tell me how to connect it.
You have to add the plugin to your plugin closure, like described in the documentation.
Add this to your top level build.gradle:
buildscript {
repositories {
google()
}
dependencies {
def nav_version = "2.5.0"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
...
and this to your app modules build.gradle:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'androidx.navigation.safeargs.kotlin'
}
...
Also make sure that android.useAndroidX=true is set in your gradle.properties files.

"Could not resolve net.java.dev.jna:jna-platform" in Gradle when I have integrated Firebase

The error is:
Could not resolve net.java.dev.jna:jna-platform
In the integration of Firebase to mu Grandle I get an error. I don't know if it's because the structure of the document was modified, since in mine it didn't contain "buildscript","allprojects".
I'm programming with java, not with kotlin in case it can help.
My gradle.app is
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.whatdowant"
minSdk 24
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
///Implement para FIREBASE
implementation 'com.google.firebase:firebase-auth:21.0.3'
implementation 'com.google.android.gms:play-services:play-services-auth:17.0.0'
implementation 'com.google.firebase:firebase-analytics'
implementation platform('com.google.firebase:firebase-bom:29.3.1')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
apply plugin: 'com.google.gms.google-services'
> `My gradle. project is:`
buildscript {
ext.kotlin_version = "1.5.0"
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
// Add the following line:
classpath 'com.google.gms:google-services:4.3.10' // Google Services plugin
classpath "com.android.tools.build:gradle:4.2.2"
}
}
allprojects {
repositories {
mavenCentral()
google() // Google's Maven repository
}
}
```

some kotlin library attached to the project where compile with newer kotlin compiler and it can't be read... No update found

Hi here I am try a payment project using Amazon Payment Services Android SDK. Here I am successfully done the payment functionality and it's works well. After implementing into main projects I got following issue.
some kotlin library attached to the project where compile with newer kotlin compiler and it can't be read... No update found
In My Gradle
buildscript {
ext.kotlin_version = "1.6.0"
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://android-sdk.payfort.com" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
In Module
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 31
defaultConfig {
applicationId "com.android.paymentdemoapp"
minSdkVersion 23
targetSdkVersion 31
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation("com.amazon:fortpayment:+:release#aar"){
transitive = true
}
}
I'm modifying the kotlin version(1.6.10 to 1.4.32) and 'build:gradle:4.1.0' to 4.1.2 clean and rebuild the project I got the following issue...
e: /Users/raja/.gradle/caches/transforms-2/files-2.1/2a57ced0732a783c670dc97c999acc46/jetified-core-ktx-1.8.0-alpha03-api.jar!/META-INF/core-ktx_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.2.
I'm tried to configure the kotlin version and Gradle build version different way but there is no use. Anyone please help me to fix this issue. Thanks in advance.

Categories

Resources