Annotation-processing on Kotlin #Serializable residing in external jar - android-studio

I'm using kotlinx.serialization to obtain for example Data.serialize() according to this:
#Serializable
data class Data(val testField: String)
This works for a #Serializable class in the same project. I can use Data.serializer().
However, I also have a similar basic data class in an external jar from which I also want kotlinx.serialization to generate. But no corresponding .serializer() is generated for it. How should I configure Gradle for this?
project build.gradle
buildscript {
ext.kotlin_version = '1.3.60'
repositories {
mavenLocal()
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}
module build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlinx-serialization'
apply plugin: 'kotlin-kapt'
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0'
...
tried the following
kapt files('path/to/repository/com/external/jar-name.jar')
or
kapt files('path/to/repository/com/external/jar-name-sources.jar')
or
kapt 'com.external:jar-name:version'
or
kapt 'com.external:jar-name-sources:version'
}
Do I need to somehow add the source jar of the external library to AndroidStudio to be able to do this?

Related

Why is kapt not working with Kotlin for Node.Js?

I wanted to port a kotlin project with gradle building from jvm to node.js. The build.gradle goes as follow:
plugins {
id 'org.jetbrains.kotlin.js' version '1.3.72'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
jcenter();
}
apply plugin: 'kotlin-kapt'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-js"
testImplementation "org.jetbrains.kotlin:kotlin-test-js"
implementation 'org.apache.httpcomponents:httpclient:4.5.12'
implementation "com.squareup.moshi:moshi-kotlin:1.9.2"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.9.2'
testCompile 'junit:junit:4.12'
}
kotlin.target.nodejs { }
but received the gradle build error
Could not find method kapt() for arguments [com.squareup.moshi:moshi-kotlin-codegen:1.9.2]
and while this works fine with kotlin(jvm) project. I wonder why and how to solve this problem.
Thanks in advance.

Android Studio cannot sync and build NEW and OLD kotlin projects

When I am creating a new project in Android Studio, and load old projects I got this:
java.lang.NoClassDefFoundError: org/gradle/internal/impldep/com/google/common/collect/Lists
java.lang.ClassNotFoundException: org.gradle.internal.impldep.com.google.common.collect.Lists
ERROR: Unable to load class 'org.gradle.internal.impldep.com.google.common.collect.Lists'.
Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt
Now I am totally stucked with that.
1, I downloaded studio again and installed to an other drive, the probleme still exists.
2, I deleted all of my Android Studios from my computer, I downloaded and installed again. Started a new kotlin project, the same error happened.
3, invalidate/restart not fixed
4, redownload dependencies not fixed
5, update gradle not fixed
6, flutter plugin disabled, not fixed
I was used my old Android Studio for flutter/dart and kotlin too. It looks like I can not build anymore native apps/kotlin.
my gradle wrapper
distributionUrl=https://services.gradle.org/distributions/gradle-5.6.4-all.zip
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.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()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "hu.lacas.myapplication"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
app build gradle
Any way to fix this?
Thanks
LOL. I found an old project what were built!!!
I copied this strings from that:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.71"
}
and
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
and finally synced, built!!!
Something is very broken in the new Android Studio, because I got a lot of error/sync failed even if I create a NEW PROJECT

Plugin with id 'androidx.navigation.safeargs' not found

When I try to add Safe Args (Android Navigation) to my app's as following
( using this guide : https://developer.android.com/topic/libraries/architecture/navigation/navigation-pass-data ) :
apply plugin: 'com.android.application'
apply plugin: 'androidx.navigation.safeargs'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
android {...
I receive this error :
Plugin with id 'androidx.navigation.safeargs' not found.
To add androidx.navigation.safeargsplugin follow below step's
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha09"
(latest)
Add above to your Project Gradel file inside dependency block
Then add apply plugin: 'androidx.navigation.safeargs' to your app/Module gradle file
You can use dependencies as below in Project level bulild.gradle in Android Studio bumblebee
id 'androidx.navigation.safeargs' version '2.4.2' apply false
Just add this line in your build.gradle project level :
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.2.0-rc02"
In newer version of Android Studio 3.2+, below dependency need to add in both build.gradle file
Project-Level build.gradle
dependencies {
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5'
}
App-Level build.gradle
plugins {
id 'androidx.navigation.safeargs'
}
Add
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0-alpha05"
In your project-level dependencies
For eg :
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0-alpha05"
}
Don't forget to add the latest version
It appears because you are declaring it in the wrong build.gradle file. You have to place it in the build.gradle that looks like this
buildscript {
repositories {
google()
}
dependencies {
def nav_version = "2.1.0"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
This link explain how it is added https://developer.android.com/jetpack/androidx/releases/navigation#safe_args
as per this https://developer.android.com/jetpack/androidx/releases/navigation
Add above to your Project Gradel file inside dependency block
`classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:2.2.1`
then add
apply plugin: androidx.navigation.safeargs
to your app/Module gradle file
Add this line in Project-level build gradle
id 'androidx.navigation.safeargs' version '2.4.2' apply false
Add this in App-level build gradle
plugins {
id 'androidx.navigation.safeargs'
}
I had to add it to the top level build.gradle, not sure if you've just added it to your app level build.gradle instead
FYI: I had to add it in this manner in order for it to work in Android Studio Dolphin | 2021.3.1:
Gradle Scripts > build.gradle (Project: YourProject)
plugins {
id blah
.
.
.
id 'androidx.navigation.safeargs' version '2.4.2' apply false
}
And also here:
Gradle Scripts > build.gradle (Module: YourProject.app)
dependencies {
implementation blah
.
.
implementation 'androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0-alpha02'
testImplementation blah
androidTestImplementation blah
}
I hope this may help others. I followed the directions at developer.android.com and all the instructions here. None of them solved the issue for me.
Mine was a simple order of operation in my modules build.gradle file.
I had:
id 'androidx.navigation.safeargs.kotlin'
listed as the first line in my plugins block.
moving to the end of the block solved my issue

Gradle DSL method not found: complie()

Error:(17, 0) Gradle DSL method not found: 'compile()'
Possible causes:The project 'WizardPagerrr_production_server' may be using a version of Gradle that does not contain the method.
Gradle settingsThe build file may be missing a Gradle plugin.
Apply Gradle plugin
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.android.support:support-v4:21.0.3'
}
android {
compileSdkVersion 21
buildToolsVersion '17.0.0'
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
This error message is telling you what's wrong:
The build file may be missing a Gradle plugin. Apply Gradle plugin
You need an apply plugin statement in your build file to tell Gradle how to build the module. If this module is an Android Application module that will output an APK, use:
apply plugin: 'com.android.application'
If it's a library module that will output an AAR, use:
apply plugin: 'com.android.library'
The deeper meaning of the error message is that in this context Gradle doesn't have a definition for the compile dependency directive (i.e it's not built in to Gradle). It needs the Android Gradle plugin to set that up.

Errors with gradle on Android Studio 0.4.6

Hello StackOverflow,
I have recently updated my Android Studio to the latest version (0.4.6), and I am encountering weird problems with it. When first creating my project, I got this error:
So I obviously went to my SDK Manager and updated my Build Tools to version 19.0.2. However, I still got this error message. I took a look into my build.gradle file and saw that I am missing the android paragraph, So I changed it from this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
To this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.0.2'
//Other Configuration
}
allprojects {
repositories {
mavenCentral()
}
}
But now I am getting this error message when trying to build (and the older Build Tools error):
I'm not sure what to do now.. How can I solve this?
As suggested by Scott keep your Root level build.gradle file like this
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
Content of this file will be included in each module level build.gradle file at the type of Gradle sync or compilation.
Check all build.gradle files inside your modules. They all should look similar to this
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.0.2'
//Other Configuration
}
dependencies{
// Your all module dependencies here
}
Undo the changes you made to the top-level build file, and make those changes to the build files in your individual modules. In particular, putting an apply plugin: 'android' statement or android block in your top-level build file in that manner won't work. The error message you're seeing is happening because the build system is trying to build an Android app out of the root directory of your project, but none of the source files for such a project are there.

Resources