Java Android Kotlin 库集成后项目真机部署耗时过长

Java Android project real device deployment takes too much time after Kotlin library integration

我有 android Java 和 Kotlin 不同的项目。我通过在 gradle、

中进行以下更改,将现有的 Kotlin 项目转换为库
//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        //applicationId "com.example.xxxxxxxx"
....
}
}

并且我已经删除了库项目的意图过滤器,

<activity android:name=".view.MainActivity"
            android:theme="@style/MySuperTheme" >

</activity>

当我执行 file->invalidate cache 并重新启动然后清理重建并部署到真实设备时,它需要超过 10 分钟 它给出以下错误消息,

Installation did not succeed.
The application could not be installed.

List of apks:
[0] '/Users/panchanathantharsan/StudioProjects/xxxxxxx/app/build/outputs/apk/debug/app-debug.apk'
Installation failed due to: 'device '0123456789ABCDEF' not found'

在第一个 运行 之后,它的部署速度很快。每次清理、重建和部署时都非常慢。

这是我的主要项目gradle,

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.apollographql.apollo'

android {
    compileSdkVersion 30
    buildToolsVersion '30.0.1'


    defaultConfig {
        applicationId "com.kaleSystems.kalePos"
        minSdkVersion 25
        //noinspection OldTargetApi
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }
    }
    buildFeatures {
        dataBinding = true
        // for view binding:
        // viewBinding = true
    }


    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }

    useLibrary 'android.test.mock'
}

repositories {
    maven { url "https://jitpack.io" }
    google()
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    //noinspection GradleCompatible
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation files('libs/poi-excelant-3.14-20160307.jar')
    implementation files('libs/poi-ooxml-3.14-20160307.jar')
    implementation files('libs/poi-ooxml-schemas-3.14-20160307.jar')
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.android.volley:volley:1.1.1'
    implementation 'com.github.PhilJay:MPAndroidChart:v2.1.6'
    implementation files('libs/httpcore-4.2.3.jar')
    implementation files('libs/httpclient-4.2.3.jar')
    implementation 'androidx.percentlayout:percentlayout:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.github.felHR85:UsbSerial:3.3'
    implementation 'com.github.douglasjunior:android-simple-tooltip:0.2.2'
    implementation 'com.google.firebase:firebase-crash:16.2.1'
    implementation 'com.google.firebase:firebase-core:18.0.0'
    implementation project(path: ':reportModule')
    //noinspection GradleCompatible
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation files('libs/kaleprintsdk-release.aar')

    implementation 'com.hbb20:ccp:1.7.1'

    //graphql
    implementation 'com.apollographql.apollo:apollo-runtime:2.2.0'
    implementation "com.apollographql.apollo:apollo-android-support:2.2.0"
    implementation 'com.squareup.okhttp3:okhttp:4.6.0'


}

这是图书馆 gradle,

//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.apollographql.apollo'


android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        //applicationId "com.example.kale_cloud_reports"
        minSdkVersion 25
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        packagingOptions {
        }
    }
    buildFeatures {
        dataBinding = true
        // for view binding:
        // viewBinding = true
    }

    testOptions {
        unitTests.includeAndroidResources = true
    }

    apollo {
        generateModelBuilder = true
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72'
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.google.android.material:material:1.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//    implementation'com.android.volley:volley:1.1.1'
    implementation 'com.google.code.gson:gson:2.8.5'

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

    //graphql
    implementation "com.squareup.okhttp3:okhttp:4.6.0"
    implementation "com.apollographql.apollo:apollo-runtime:2.2.0"
    implementation "com.apollographql.apollo:apollo-coroutines-support:2.2.0"

    implementation 'com.android.support:multidex:1.0.3'
}

非常感谢任何帮助。这正在减慢开发过程。 谢谢。

注意:我部署到模拟器的任何操作都是正常且快速的。没有延误。

折腾了几天成功解决问题,

创建了一个新的示例项目并添加了 Kotlin 库项目,使其运行顺畅、快速。

比较了两个主项目 gradle 配置,发现 android.enableD8=false 是导致问题的原因。

请在此处 https://android-developers.googleblog.com/2017/08/next-generation-dex-compiler-now-in.html

从官方 link 阅读更多关于 enableD8 的信息

使它成为现实,所有构建都已排序,应用程序 运行 顺利进行。 android.enableD8=true

如果有人像这样卡住了,请利用它。检查 gradle.Properties 文件并进行更改。 在 3.0 版本之后的 android studio 中默认设置为 true。 不知何故,它在我的案例中被修改了。 干杯。