首页 > 解决方案 > android kotlin,将 lib 从 java 转换为 kotlin,并在“错误:找不到符号”中的 java 代码中出错

问题描述

有一个库模块要转换为 kotlin,在更改一个类后它开始出现错误“错误:找不到符号”。

该项目是

java_kotlin_test
+app
   +build.gradle <-- app's gradle
+mylibrary2
   +build.gradle <-- module's gradle
+build.gradle <-- top level gradle

这是顶级项目的 build.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"
    }
}

这是库模块mylibrary2build.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()
}

在应用程序中,它有一个使用 lib 模块中的类的 java 类:

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) {

        }
    }
}

MyUserandErrorCode在 java 中时,它编译得很好。

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() {
    }
}

但转换为 kotlin 后编译失败: 在此处输入图像描述

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
}

应用程序的 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'
}

标签: androidkotlingradle

解决方案


推荐阅读