#android #kotlin #gradle
Вопрос:
Имея библиотечный модуль, который нужно преобразовать в kotlin, после изменения класса он начинает получать ошибку «ошибка: не удается найти символ».
проект представляет собой
java_kotlin_test
app
build.gradle <-- app's gradle
mylibrary2
build.gradle <-- module's gradle
build.gradle <-- top level gradle
Это сборка проекта верхнего уровня.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.5.10'
ext.versions = [
version : '0.0.2'
]
repositories {
mavenLocal()
google()
jcenter()
}
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 {
mavenLocal()
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
allprojects { proj ->
ext {
group_id = "com.hardcode.test"
}
}
Это сборка библиотечного модуля mylibrary2
.gradle
group = project.ext.group_id
project.ext.artifactId = 'mylibrary2'
version = versions.version
apply plugin: "maven"
//apply plugin: 'kotlin-android'. // note: uncomment it will get error "Failed to apply plugin [id 'kotlin-android']"
apply plugin: 'com.android.library'
uploadArchives {
repositories {
mavenDeployer {
repository(url: mavenLocal().getUrl())
}
}
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
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.3.0'
implementation 'com.google.android.material:material:1.3.0'
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.core:core-ktx: "
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
repositories {
mavenCentral()
}
in the app it has a java class using the classes from the lib module:
package com.demo.java_kotlin_lab;
import com.demo.mylibrary2.ErrorCode;
import com.demo.mylibrary2.MyUser;
import com.demo.mylibrary2.MyUserkt;
class testJava {
testJava() {
MyUser a = new MyUser("eee", "888");
MyUserkt akt = new MyUserkt("eee", "888");
if (ErrorCode.INVALID_USER_AUTH == 1) {
}
}
}
when the MyUser
and ErrorCode
was in java it compiles fine.
package com.demo.mylibrary2;
import androidx.annotation.Nullable;
public class MyUserkt {
@Nullable
private String firstName;
@Nullable
private String lastName;
public MyUserkt(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
===
package com.demo.mylibrary2;
public class ErrorCode {
public static final int ID_NOT_FOUND = 400001;
public static final int INVALID_USER_AUTH = 403001;
public static final int INVALID_USER_SCOPE = 403002;
ErrorCode() {
}
}
but after convert to kotlin it failed to compile:
package com.demo.mylibrary2
class MyUserkt(var firstName: String?, var lastName: String?)
===
package com.demo.mylibrary2
object ErrorCode {
@JvmStatic val ID_NOT_FOUND = 400001
@JvmStatic val INVALID_USER_AUTH = 403001
@JvmStatic val INVALID_USER_SCOPE = 403002
}
the app’s build.gradle:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.demo.java_kotlin_lab"
minSdkVersion 21
targetSdkVersion 30
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 "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation project(":mylibrary2")
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}