java - 减小签名后的 apk 大小

标签 java android

在我的应用程序项目中,资源大小为 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

// Or, if you prefer, you can continue to check for errors in release builds, // but continue the build even when errors are found:

<小时/>
        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 中的 apk 大小为 60 MB,那么 Playstore 中的 apk 大小约为 30 MB。

关于java - 减小签名后的 apk 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62441603/

相关文章:

java - Datanucleus增强器错误: transient method

JAVA: IO new File(filename) throwing "Can' t read input file!"

android - 在自动模式下如何获取相机准确的白平衡参数值?

android - onAttachedToWindow 和 onStart 之间的确切区别是什么

android - 更新到 API 23 后获取 java.lang.NoClassDefFoundError

android - 在 Android 中显示用于录音的声波

android - 有没有办法访问存储在项目文件夹中的文件?

java - 如何使用扫描器、循环、数组、if 和 else 语句来计算单词数(不计算特殊字符系列)

java 从数组列表、词典单词中进行二分搜索

java - Ant 大战僵尸游戏