Tag: android

Kotlin崩溃无法将提供的符号转换为Dependency类型的对象:org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension_Decorated

在将kotlin版本从1.0.5-2撞到1.1.0之后,我得到一个崩溃: Error:(114, 0) Cannot convert the provided notation to an object of type Dependency: org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension_Decorated@5a39a165. The following types/formats are supported: – Instances of Dependency. – String or CharSequence values, for example 'org.gradle:gradle-core:1.0'. – Maps, for example [group: 'org.gradle', name: 'gradle-core', version: '1.0']. – FileCollections, for example files('some.jar', 'someOther.jar'). – Projects, for example project(':some:project:path'). – ClassPathNotation, […]

Kotlin:如何访问自定义视图的Attrs

我在Kotlin中创建一个自定义视图,并想访问它的属性资源。 以下是我的代码 class CustomCardView : FrameLayout { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) init { LayoutInflater.from(context).inflate(R.layout.view_custom_card, this, true) if (attrs != null) { val a = context.obtainStyledAttributes(attrs, R.styleable.custom_card_view) if (a.hasValue(R.styleable.custom_card_view_command)) { var myString = a.getString(R.styleable.custom_card_view_command) } } } } 请注意,这将在init函数中的attrs错误。 […]

将任何转换为​​Kotlin中的ArrayList,改造

我在我的android应用程序中使用Kotlin和Retrofit。 答案是一个类。 具有通用的价值即。 数据:任何? data class GlobalResponse(@Expose val status: String, @Expose val message: String, @Expose val data: Any?) data class District(@Expose val name:String) @GET("{endpoint}") fun getData( @Path("endpoint") endpoint: String ): Observable<GlobalResponse> RestService.create().getData("res") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ response -> // Ok Log.e("Hello", "Hello") // problem here // Jumps to error-> below Log.e("Name", ((response.data as ArrayList<District>)[0]).name) }, { […]

不能用Mockito 2来嘲笑最终的Kotlin课堂

我无法使用Mockito 2来嘲笑Kotlin最后一堂课。另外,我正在使用Robolectric。 这是我的测试代码: @RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) public class Test { // more mocks @Mock MyKotlinLoader kotlinLoader; @Before public void setUp() { MockitoAnnotations.initMocks(this); } } 当我们尝试初始化setUp()方法中的模拟时,测试失败。 另外,我在我的代码中使用下面的gradle依赖关系: testCompile 'org.robolectric:robolectric:3.3.2' testCompile 'org.robolectric:shadows-multidex:3.3.2' testCompile 'org.robolectric:shadows-support-v4:3.3.2' testCompile("org.powermock:powermock-api-mockito2:1.7.0") { exclude module: 'hamcrest-core' exclude module: 'objenesis' } testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-inline:2.8.9' 所有其他单元测试通过使用这种配置,但只要我试图嘲笑Kotlin类,它会引发以下错误: Mockito cannot mock/spy because : […]

用Kotlin反射调用类的构造函数

我有以下的数据类 data class Person (val id: Int? = null, val name: String, val active: Boolean) 我需要通过反射来调用它的构造函数。 我试了下面的代码 private fun <T> createEntity(constructor: Constructor<*>, vararg args: T) : Any { return constructor.newInstance(args) } 并用args参数的一个数组来调用它。 val fields = entity.declaredFields var elements = Array<Any>(getFieldsCount(fields), { i -> val index = cursor.getColumnIndex(fields[i].name.toUpperCase()) when (fields[i].type) { kotlin.Int::class.java -> cursor.getInt(index) kotlin.Boolean::class.java -> […]

Kotlin + Mockito2:演示者测试的ClassNotFoundException

我有一个基于CleanArchitecture-Android风格的Kotlin编写的示例应用程序。 以下是应用程序的配置: 演示/的build.gradle dependencies { implementation project(':domain') def presentationDependencies = rootProject.ext.presentationDependencies def commonDependencies = rootProject.ext.commonDependencies testCompile testingDependencies.junit testCompile testingDependencies.mockito // org.mockito:mockito-inline:2.10.0 } 呈现/ SRC /测试/ JAVA /包/ RepoPreferencesPresenterTest.kt internal class RepoPreferencesPresenterTest : UnitTest() { // FIXME: Mocking fails @Mock private lateinit var mockRepositoriesUseCase: RepositoriesUseCase @Mock private lateinit var mockView: RepoPreferencesContract.View private lateinit var presenter: RepoPreferencesPresenter<RepoPreferencesContract.View> […]

Kotlin kapt无法看到支持注释

我把一个枯燥的Java项目加入了kotlin。 一切编译和工作,直到添加注释处理kapt。 现在,我得到java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing 和一堆显示的警告是e: @Retention(RetentionPolicy.SOURCE) e: ^ e: symbol: class Retention e: location: class RegistryDef 和 e: @StringDef({ASCENDING_ORDER, DESCENDING_ORDER, SortOrder.UNKNOWN}) e: ^ e: symbol: class StringDef e: location: class RegistryDef 我明确地将支持注释添加到项目中,以确保它们包含在内,并使用Gradle将所有版本强制为最新版本。 有没有人经历过这个,你是如何解决它的?

Kotlin单元测试没有发现模块依赖性接口

我有一个app模块和一个domain模块。 在我的domain模块中,我有一个名为Repository的接口。 在我的app模块中,我使用dagger来为我的类注入一个实现,这工作正常。 当我然后使用kotlin单元测试去测试时,在运行时我得到一个NoClassDefFoundError 。 我也尝试在我的app模块依赖关系中包含domain模块,但是这也不起作用: testImplementation project(':domain') 这里是我目前的测试依赖关系,以及我如何包括模块 implementation project(':domain') testImplementation 'junit:junit:4.12' testImplementation 'com.nhaarman:mockito-kotlin:1.5.0' 在我的单元测试中,我使用它可能是这个问题: @Mock lateinit var mockRepo : Repository

Kotlin的建筑师

我正在从官方文档学习Kotlin ,我创建了一个class ,如下创建一个具有两个parameters constructor parameters 。 constructor主体在init块中。 class Person(name: String, surname: String) { init { Log.d("App", "Hello"); } } 那么,我想创建一个更多的constructor ,将采取一个constructor中的一个parameter 。 Kotlin什么办法呢?

Kotlin在Android Studio 3.0 Canary 2中使用Android DataBinding

我的项目有2个模块,它们是app和apphelper作为一个库。 我一直在尝试使用android数据绑定与它kotlin,但我无法弄清楚为什么当我建立项目时,它不断显示我这些错误: 失败:生成失败,出现异常。 * What went wrong: A problem occurred configuring project ':app'. > Could not resolve all dependencies for configuration ':app:stagingRuntimeClasspath'. > Unable to find a matching configuration in project :apphelper: – Configuration 'debugApiElements': – Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'. – Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=staging}' and found incompatible value 'BuildTypeAttr{name=debug}'. – Found […]