GVKun编程网logo

android-使用kotlin设置领域时出错(kotlin配置)

10

在这篇文章中,我们将为您详细介绍android-使用kotlin设置领域时出错的内容,并且讨论关于kotlin配置的相关问题。此外,我们还会涉及一些关于AndroidJetpack是否需要使用Kotl

在这篇文章中,我们将为您详细介绍android-使用kotlin设置领域时出错的内容,并且讨论关于kotlin配置的相关问题。此外,我们还会涉及一些关于Android Jetpack是否需要使用Kotlin语言?、Android Kotlin - 解密用户名时出错、Android Kotlin RXKotlin Room - 错误 Kotlin.unit、Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败的知识,以帮助您更全面地了解这个主题。

本文目录一览:

android-使用kotlin设置领域时出错(kotlin配置)

android-使用kotlin设置领域时出错(kotlin配置)

我在现有的Android项目中使用了一些kotlin类(已经具有领域),kotlin类未使用任何领域功能,现在在运行时出现此错误

:app:compileDebugKotlin
Using kotlin incremental compilation
:app:compileDebugJavaWithJavac
Destination for generated sources was modified by kapt. PrevIoUs value = /home/debu/AndroidStudioProjects/WT_Application/app/build/generated/source/apt/debug

error: Annotation processor '__gen.AnnotationProcessorWrapper_debug_io_realm_processor_RealmProcessor' not found
1 error

:app:compileDebugJavaWithJavac Failed
:app:copyDebugKotlinClasses SKIPPED

FAILURE: Build Failed with an exception. 

我的应用程序的build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'realm-android'

android {
    def globalConfiguration = rootProject.extensions.getByName("ext")

    compileSdkVersion globalConfiguration.getAt("androidCompileSdkVersion")
    buildToolsversion globalConfiguration.getAt("androidBuildToolsversion")
    defaultConfig {
        applicationId globalConfiguration.getAt("androidApplicationId")
        minSdkVersion globalConfiguration.getAt("androidMinSdkVersion")
        targetSdkVersion globalConfiguration.getAt("androidTargetSdkVersion")
        versionCode globalConfiguration.getAt("androidVersionCode")
        versionName globalConfiguration.getAt("androidVersionName")
        testInstrumentationRunner globalConfiguration.getAt("testInstrumentationRunner")
        /*jackOptions {
            enabled true
        }*/
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    def presentationDependencies = rootProject.ext.presentationDependencies
    def presentationTestDependencies = rootProject.ext.presentationTestDependencies
    def developmentDependencies = rootProject.ext.developmentDependencies
    compile presentationDependencies.dagger
    compile presentationDependencies.butterKnife
    compile presentationDependencies.recyclerView
    compile presentationDependencies.cardview
    compile presentationDependencies.rxJava
    compile presentationDependencies.rxAndroid
    compile presentationDependencies.appcompat
    compile presentationDependencies.constraintLayout
    compile presentationDependencies.design
    compile presentationDependencies.retrofit
    compile presentationDependencies.gsonconverter
    compile presentationDependencies.rxjavaadapter
    compile presentationDependencies.glide
    compile presentationDependencies.flexBox
    compile presentationDependencies.maps
    compile presentationDependencies.mapUtils
    compile presentationDependencies.pagerIndicator
    annotationProcessor presentationDependencies.daggerCompiler
    annotationProcessor presentationDependencies.butterKnifeCompiler
    provided presentationDependencies.javaxAnnotation
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    androidTestCompile presentationTestDependencies.junit
    androidTestCompile presentationTestDependencies.mockito
    androidTestCompile presentationTestDependencies.dexmaker
    androidTestCompile presentationTestDependencies.dexmakerMockito
    androidTestCompile presentationTestDependencies.espresso
    androidTestCompile presentationTestDependencies.testingSupportLib
    //Development

    compile developmentDependencies.leakCanary
    compile files('libs/YouTubeAndroidplayerApi.jar')
}
repositories {
    mavenCentral()
}

项目build.gradle

apply from: 'buildsystem/dependencies.gradle'
buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'
        classpath "io.realm:realm-gradle-plugin:3.2.0"
        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 {
    ext {
        androidApplicationId = 'com.wandertrails'
        androidVersionCode = 1
        androidVersionName = "1.0"
        testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
        testApplicationId = 'com.wandertrails.test'
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

现在有人可以找出这个错误的原因吗?

解决方法:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

kapt presentationDependencies.daggerCompiler
kapt presentationDependencies.butterKnifeCompiler

可能会修复它.

Android Jetpack是否需要使用Kotlin语言?

Android Jetpack是否需要使用Kotlin语言?

我似乎无法在Google中找到明确的答案,而我在Jetpack上看到的所有文档和示例似乎都引用并使用Kotlin.

但是你必须使用Kotlin才能使用Jetpack吗?或Jetpack可以与传统的Java程序一起使用?

我在看Kotlin,但它为你的应用程序大小增加了几乎MB.看到我的整个应用程序目前只有大约200KB,我认为Kotlin不值得将我的应用程序的大小增加500%!

解决方法:

Jetpack是一系列图书馆以及Google建议如何构建Android应用程序的指南.这些库都有Java API,因此您无需转移到Kotlin即可使用它们.这两种语言仍然完全支持Android开发.

至于APK大小的考虑,Kotlin确实会增加它.很难说它会有多大,但Proguard应该删除很多你最终没有使用的标准库,所以它可能远远小于整个MB. Here’s一篇文章比较了一个基本上空的Hello world应用程序的Java和Kotlin APK大小,例如,发现增长只有11 KB.因此,除非你处于一个非常重要的市场,否则你可能会放弃使用Kotlin.

更重要的是,切换到Kotlin不仅仅是拉入一个库 – 它是一种新的学习语言,这意味着你必须付出一些努力,它可以改变你编写应用程序的方式.您应该更多地查看语言,看看您是否喜欢它提供的内容.

Android Kotlin - 解密用户名时出错

Android Kotlin - 解密用户名时出错

如何解决Android Kotlin - 解密用户名时出错?

在我们的 android 应用中,我们使用密码算法 [AES/CBC/PKCS7Padding] 对用户名和密码进行加密和解密。

我们在某些设备上收到 javax.crypto.BadPaddingException。我确信我们为加密和解密传递了相同的密钥。任何人都可以对此有任何想法或面临这个问题吗?

请注意,我无法将代码放在这里,我很确定为加密和解密传递了相同的密钥。

解决方法

您是否将加密的字节转换为十六进制字符串并将十六进制字符串转换回字节以进行解密?

请参考 - javax.crypto.BadPaddingException

上述答案的Kotlin版本->

fun fromHexString(s: String): ByteArray {
        val len: Int = s.length
        val data = ByteArray(len / 2)
        
        var i = 0
        while (i < len) {
            data[i / 2] = ( (Character.digit(s[i],16) shl 4) + Character.digit(s[i + 1],16) ).toByte()
            i += 2
        }
        
        return data
    }
,

在 Java 世界中,有 no PKCS7Padding。 PKCS7Padding 扩展名为“PKCS5Padding”。 也许三星的实现就是严格遵守这个约定的。因此它不会识别 PKCS7 填充。如果您想让您的代码在所有设备(包括那些三星)上运行,请改为指定“AES/CBC/PKCS5Padding”。即便如此,也会得到同样的结果。

另请参阅 Android 正式支持的 Cipher transformations 列表。

Android Kotlin RXKotlin Room - 错误 Kotlin.unit

Android Kotlin RXKotlin Room - 错误 Kotlin.unit

如何解决Android Kotlin RXKotlin Room - 错误 Kotlin.unit?

我想通过循环数组向房间数据库做一个简单的插入数据。 我使用 RXKotlin 来迭代数组 我有一个这样的数组:

fun defaultDataCategory() : ArrayList<CategoryModel>{
        var cat: CategoryModel
        var catArrayList: ArrayList<CategoryModel> = ArrayList(0)
        val date: Int = Calendar.DATE
        val formatedDate = SimpleDateFormat("yyyy-MM-dd").format(Date())
        val formatedTime = SimpleDateFormat("HH:mm").format(Date())
        val DateTime = "$formatedDate  $formatedTime"

        catArrayList.add(
            CategoryModel(
                1,"Personal",true,"Red",Converter.toDate(Calendar.getInstance().timeInMillis),"system","system"
            )
        )

        catArrayList.add(
            CategoryModel(
                2,"Work","Blue","system"
            )
        )

        catArrayList.add(
            CategoryModel(
                3,"Home","Purple","system"
            )
        )

        catArrayList.add(
            CategoryModel(
                4,"Learn","Yellow","system"
            )
        )
       return catArrayList
    }

我像这样用 RXKotlin 循环了一个数组

var catArrayList: ArrayList<CategoryModel> = DefaultData.defaultDataCategory()

        catArrayList.toObservable()
            .subscribeBy(  // named arguments for lambda Subscribers
                onNext = { homeviewmodel.insertCategory(it) },onError = { Log.e("error insert=",it.printstacktrace().toString()) },onComplete = { Log.e("complete insert=","complete insert") }
            )

我得到了一个错误“kotlin.unit”。

Error RXKotlin

如何使用 RXKotlin 迭代数组并插入房间数据库?

解决方法

试试

Observable.fromIterable(catArrayList).subscribeBy {...}

Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败

Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败

UPDATE

此处已针对此问题提交了一个错误:
https://youtrack.jetbrains.com/issue/KT-17951

更新2

该错误已在Android Studio 3.0 Canary 3中修复

原帖

我刚刚开始使用Android Studio 3.0,我从一开始就启用了kotlin支持.我在我的项目中写了一个非常简单的Kotlin类:

data class Wallet(val coins: Int) {
    fun add(value: Int): Wallet = Wallet(coins + value)
    fun substract(value: Int): Wallet = if (coins > value) Wallet(coins + value) else throw InsufficientFundsException()
}

现在我想测试这个类,首先我在Kotlin写了一个本地运行的unittest(测试目录):

class WalletTestKotlin {

    @Throws(Exception::class)
    @Test
    fun add() {
        Assert.assertEquals(22, Wallet(20).add(2).coins.toLong())
        Assert.assertNotEquals(5, Wallet(2).add(13).coins.toLong())
    }
}

它编译并运行但错误消息:

Class not found:
“com.agentknopf.hachi.repository.model.WalletTestKotlin”Empty test
suite.

因此我用Java重新编写了测试:

public class WalletTest {

    @Throws(exceptionClasses = Exception.class)
    @Test
    public void add() {
        Assert.assertEquals(22, new Wallet(20).add(2).getCoins());
        Assert.assertNotEquals(5, new Wallet(2).add(13).getCoins());
    }
}

然而,该测试也失败了 – 这次Kotlin类“Wallet”无法找到:

java.lang.NoClassDefFoundError: com/example/repository/model/Wallet

我想知道我是否遗漏了某些东西……运行Java测试,不是指Kotlin类,而是java类只能成功完成.

我的项目build.gradle文件是默认文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
        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 {
        jcenter()
        maven { url 'https://maven.google.com' }
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的特定于模块的build.gradle的依赖项:

dependencies {
    compile filetree(dir: 'libs', include: ['*.jar'])
    //Kotlin support
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    //Testing libraries
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}

解决方法:

解决方法(暂时):

把它放到你的(app-level)build.gradle中:

task copyTestClasses(type: copy) {
    from "build/tmp/kotlin-classes/debugUnitTest"
    into "build/intermediates/classes/debug"
}

然后在“启动前”的底部向下修改测试JUnit Run / Debug配置,其中包含“Gradle-aware make”,另一部分,选择gradle任务,选择它所在的项目build.gradle文件,然后键入copyTestClasses. Click here for screenshots的不同测试框架,但管道工作方式相同.

您可能需要根据构建类型更改/添加更多目录管道.您找到这些奇怪位置的方法是通过在项目树中搜索相关的.class文件.

关于android-使用kotlin设置领域时出错kotlin配置的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Android Jetpack是否需要使用Kotlin语言?、Android Kotlin - 解密用户名时出错、Android Kotlin RXKotlin Room - 错误 Kotlin.unit、Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败的相关知识,请在本站寻找。

本文标签: