簡體   English   中英

減小簽名的 apk 大小

[英]reduce the signed apk size

在我的應用程序項目中,資源大小為 1 mb,但我的 apk 大小為 60mb,我搜索了許多解決方案,但找不到任何減小應用程序大小的解決方案,幫助我減小應用程序大小。我添加了 build.gradle 的代碼。 如果你能幫我解決這個問題,我會很感激你的。

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

import com.android.build.OutputFile

def getVersionCode = { ->

    def code = project.hasProperty('versionCode') ? versionCode.toInteger() : 14

    println "VersionCode is set to $code"

    return code
}
def getVersionName = { ->

    def name = project.hasProperty('versionName') ? versionName.toString() : "1.6.1"

    println "VersionName is set to $name"

    return name
}
def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {

    lintOptions {
        checkReleaseBuilds false

// 或者,如果您願意,您可以繼續檢查發布版本中的錯誤, // 但即使發現錯誤也繼續構建:


        abortOnError false
    }
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    testOptions {
        unitTests.includeAndroidResources = true
    }

    defaultConfig {
        applicationId "example.com"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        //versionCode getVersionCode()
        //versionName getVersionName()
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
        manifestPlaceholders = [
                onesignal_app_id               : 'baad408e-ec86-40b1-9c39-22e2873d0657',
                // Project number pulled from dashboard, local value is ignored.
                onesignal_google_project_number: 'REMOTE'
        ]
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
        debug {
            signingConfig signingConfigs.debug
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
    useLibrary 'android.test.runner'
    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'}
dependencies
{
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    //implementation 'com.android.support:support-compat:28.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.makeramen:roundedimageview:2.3.0'
    implementation 'com.beust:klaxon:5.0.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.50'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'androidx.room:room-runtime:2.2.5'
    implementation 'androidx.room:room-ktx:2.2.5'
    kapt 'androidx.room:room-compiler:2.2.5'
    implementation 'com.onesignal:OneSignal:3.11.4'
    implementation 'com.google.ads.mediation:adcolony:4.1.4.0'
    implementation 'com.google.ads.mediation:facebook:5.7.0.0'
    implementation 'com.google.ads.mediation:vungle:6.4.11.1'
    implementation 'com.google.firebase:firebase-core:17.3.0'
    // Recommended: Add the Firebase SDK for Google Analytics.
    implementation 'com.google.firebase:firebase-analytics:17.3.0'
    implementation 'com.google.firebase:firebase-messaging:20.1.5'
    implementation 'com.google.firebase:firebase-auth:19.3.0'
    implementation 'com.google.firebase:firebase-database:19.2.1'
    implementation 'com.google.firebase:firebase-ads:19.0.1'  
    implementation 'com.google.firebase:firebase-crashlytics:17.0.0-beta04'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:core:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
    androidTestImplementation 'androidx.test:rules:1.3.0-alpha05'
    implementation 'network.mysterium:mobile-node:0.22.1'
    implementation 'com.github.rasoulmiri:buttonloading:v1.0.8'
}
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}
gradle.projectsEvaluated {
    preBuild.dependsOn(applyGoogleServicesIfNeeded)
}
task applyGoogleServicesIfNeeded {
    if (project.hasProperty('applyGoogleServices')) {
        apply plugin: 'com.google.gms.google-services'
    }
}
repositories {
    mavenCentral()
}

您添加了許多依賴項,這可能是消耗空間的原因。 刪除未使用的依賴項。 如果您有圖標或圖像,您還可以檢查您的可繪制文件夾,將其轉換為 webp。 並且不要太擔心 apk 的大小,如果它在.aab 中是 60 MB,它在 Playstore 中大約是 30 MB。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM