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

待更新中

相关推荐
天勤量化大唯粉19 小时前
基于距离的配对交易策略:捕捉价差异常偏离的均值回归机会(天勤量化代码实现)
android·开发语言·python·算法·kotlin·开源软件·策略模式
hudawei99619 小时前
kotlin冷流热流的区别
android·开发语言·kotlin·flow··冷流·热流
万能的小裴同学19 小时前
Android Studio 2025版JNI配置
android·ide·android studio
hudawei99619 小时前
对比kotlin和flutter中的异步编程
开发语言·flutter·kotlin·异步·
モンキー・D・小菜鸡儿19 小时前
Android11 新特性与适配指南
android·kotlin·安卓新特性
starrycode8882 天前
【每日一个知识点】Kotlin基础语法核心学习笔记
笔记·学习·kotlin
alexhilton2 天前
学会在Jetpack Compose中加载Lottie动画资源
android·kotlin·android jetpack
用户69371750013842 天前
29.Kotlin 类型系统:智能转换:类型检查 (is) 与类型转换 (as)
android·后端·kotlin
用户69371750013842 天前
30. Kotlin 扩展:为“老类”添“新衣”:扩展函数与扩展属性
android·后端·kotlin
ForteScarlet3 天前
如何解决 Kotlin/Native 在 Windows 下 main 函数的 args 乱码?
开发语言·windows·kotlin