首页 > 解决方案 > 无法在使用 CI/CD 工具获取的真实设备中启动 apk。(无法实例化应用程序)

问题描述

无法在使用 CI/CD 工具生成的真实设备中启动 apk。

注意:在使用 android studio 的本地构建中,我可以成功启动 apk。

来自 CI/CD 工具的 apk 出现以下错误

Process: com.example.android, PID: 29727
    java.lang.RuntimeException: Unable to instantiate application com.example.android: java.lang.ClassNotFoundException: Didn't find class "com.example.android" on path: DexPathList[[zip file "/data/app/com.example.android-1EY-LhMoZCN1KZl-xLdyog==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android-1EY-LhMoZCN1KZl-xLdyog==/lib/arm64, /system/lib64, /system/vendor/lib64]]
        at android.app.LoadedApk.makeApplication(LoadedApk.java:993)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5801)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1685)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "ccom.example.android" on path: DexPathList[[zip file "/data/app/com.example.android-1EY-LhMoZCN1KZl-xLdyog==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android-1EY-LhMoZCN1KZl-xLdyog==/lib/arm64, /system/lib64, /system/vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1088)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:987)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5801) 
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1685) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:6651) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 

我的 build.gradle

buildscript {
    repositories {
        google()
        maven { url 'https://maven.fabric.io/public' }
        mavenCentral()
    }

    dependencies {
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0'
    }
}
apply plugin: 'com.android.application'
def getTimeStamp() {
    def date = new Date();
    return date.format("yyyy_MM_dd");
    }
repositories {
    maven { url 'https://maven.fabric.io/public' }
    google()
    flatDir {
        dirs "../Helloworld/libs"
    }
}

apply plugin: 'com.google.firebase.crashlytics'


dependencies {
    implementation fileTree(include: '*.jar', dir: 'libs')
    implementation project(':Helloworld')
    androidTestImplementation 'androidx.annotation:annotation:1.0.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'org.mockito:mockito-core:2.8.47'
    androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
    androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.9.0'
}

android {


    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    defaultConfig {
        multiDexEnabled true
        versionCode = 1.0
        versionName = 2
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
    }

    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'
    }

    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    signingConfigs {
        release {
            storeFile file("helloworld.keystore")
            storePassword "helloworld"
            keyAlias "hello"
            keyPassword "helloworld"
            v2SigningEnabled true
            v1SigningEnabled true
        }
    }

    buildTypes {

        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-project.txt'
        }
        debug {
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-project.txt'
        }
    }

    flavorDimensions "def"

    def flavors = productFlavors {

        hellowolrd {
            manifestPlaceholders = [appName: "hellowolrd"]
            applicationId = "com.android.hellowolrd"

        }
        sampleApp {
            manifestPlaceholders = [appName: "sampleApp"]
            applicationId = "com.android.sampleApp"

        }

         ott {
            manifestPlaceholders = [appName: "ott"]
            applicationId = "com.android.OTT"

        }

    }

    sourceSets {
     hellowolrd {
            res.srcDirs = ['src/hellowolrd/res',
                           'src/Resources/userprofile/resources/hellowolrd']
            assets.srcDirs = ['src/Resources/userprofile/assets/hellowolrd']
        }

         sampleApp {
            res.srcDirs = ['src/sampleApp/res',
                           'src/Resources/userprofile/resources/sampleApp']
            assets.srcDirs = ['src/Resources/userprofile/assets/sampleApp']
        }

        ott {
            res.srcDirs = ['src/ott/res',
                           'src/Resources/userprofile/resources/ott']
            assets.srcDirs = ['src/Resources/userprofile/assets/ott']
        }
    }


    android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = outputFileName.replace('.apk', "-${versionCode + "_" + getTimeStamp()}.apk")
            outputFileName = outputFileName.replace('app-', "")
        }
    }


}


apply plugin: 'com.google.gms.google-services'

标签: androidcontinuous-integrationcontinuous-deployment

解决方案


推荐阅读