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

待更新中

相关推荐
萝卜大战僵尸43 分钟前
Android Studio_lx
android·ide·android studio
花花鱼1 小时前
Android studio CMake4.1 找不到了的解决
android·ide·android studio
雨白14 小时前
深入理解 Kotlin 协程 (五):偷天换日,探秘状态机与调度的运转引擎
kotlin
Carson带你学Android1 天前
告别 IDE?Android CLI 来了,开发进入 AI Agent 时代
android studio·ai编程
学习使我健康1 天前
Android App 启动原理
android·android studio
CeshirenTester2 天前
Claude Code 不只是会写代码:这 10 个 Skills,才是效率分水岭
android·开发语言·kotlin
朝星2 天前
Android开发[2]:Flow
android·kotlin
alexhilton2 天前
Compose中初始加载逻辑究竟应该放在哪里?
android·kotlin·android jetpack
SoulRed3 天前
Android Studio 调试flutter gradle的问题
android·flutter·android studio
zhangphil3 天前
Android Coil3图片解码Bitmap后存入磁盘,再次加载读磁盘Bitmap缓存
android·kotlin