Android Studio开发Kotlin项目中遇到的问题解决集

背景:Android Studio 2022.3.1

1. Unexpected tokens (use ';' to separate expressions on the same line)

无法在同一行声明一个变量并实例化。

解决:分开

(1)

var aaCo:Runoob<String>
aaCo=Runoob("aa" )

(2)点击:Join declaration and assignment(加入声明和分配)

结果:var aaCo:Runoob<String> = Runoob("aa" )

这样子不会报错,若是直接这样子打是不成功的

2.SourceSet with name 'main' not found.

在kotlin项目里面单独运行java文件的main方法时出现这个报错

解决:.idea/gradle.xml下新加

<option name ="delegatedBuild" value ="false" />

3.如何依赖模块

Baselibrary(属于library)、UserCenter(属于library)、App(属于application)

App依赖UserCenter、UserCenter依赖Baselibrary

方法一:要用api,否则BaseLibrary里面写的类UserCenter无法使用

api(project(mapOf("path" to ":BaseLibrary")))

api(project(mapOf("path" to ":UserCenter")))

方法二:操作后手动将implementation改成api

选择模块------》右键------》Open Module Setting------》Dependencies------》选择Modules------》加(+)------》3 Module Dependency

注:在Module里面新增的第三方依赖,想要被其它Module使用,使用api方式引入。比如Baselibrary里面新增retrofit依赖,但是UserCenter、App都需要使用,那么也是需要用api方式引入,如下:

api ("com.squareup.retrofit2:retrofit:2.1.0")

待更新中

相关推荐
来来走走33 分钟前
Android开发(Kotlin) 高阶函数、内联函数
android·开发语言·kotlin
来来走走1 天前
Android开发(Kotlin) 扩展函数和运算符重载
android·开发语言·kotlin
wuwu_q1 天前
用通俗易懂 + Android 开发实战的方式,详细讲解 Kotlin Flow 中的 retryWhen 操作符
android·开发语言·kotlin
li-jia-wei1 天前
我在造一个编程语言,叫 Free
kotlin
正经教主1 天前
【咨询】Android Studio 第三方手机模拟器对比【202511】
android·ide·android studio
Android-Flutter1 天前
kotlin - 显示HDR图(heic格式),使用GainMap算法,速度从5秒提升到0.6秒
android·kotlin
雨白1 天前
协程进阶:协作、互斥与共享状态管理
android·kotlin
studyForMokey2 天前
【Kotlin内联函数】
android·开发语言·kotlin
正经教主2 天前
【App开发】Mumu模拟器安装使用与Android Studio连接指南
android·ide·android studio
Larry_zhang双栖2 天前
Flutter Android Kotlin 插件编译错误完整解决方案
android·flutter·kotlin