使用 Android Studio 构建的应用程序 Apk 大于使用 Eclipse 构建的应用程序 Apk
Application Apk built with Android Studio is larger than one built with Eclipse
最近我将我的项目迁移到 Android Studio。该项目引用了一个 库项目 而没有复制它(参见 this answer)。
我注意到从 Android Studio 生成的 apk 比从 Eclipse 生成的要大(几乎大两倍)。当我解压这两个 apk 文件时,我看到来自 Android Studio 的 apk 中还有一个文件夹 "main",它包含 "res" 文件夹和 AndroidManifest.xml。
问题是 apk 的主目录中有 "res" 文件夹 (/res),其中包含相同的资源 (/main/res)。
我认为下一个 gradle 文件导致了问题,但没有它项目无法构建。
build.gradle 用于我的库模块 - my_android_project_lib 模块
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
}
// I think this duplicates the resources
sourceSets {
main {
manifest.srcFile 'my_android_project_lib/src/main/AndroidManifest.xml'
java.srcDirs = ['my_android_project_lib/src']
resources.srcDirs = ['my_android_project_lib/src']
aidl.srcDirs = ['my_android_project_lib/src']
renderscript.srcDirs = ['my_android_project_lib/src']
res.srcDirs = ['my_android_project_lib/src/main/res', 'slidingMenuLib/src/main/res']
assets.srcDirs = ['my_android_project_lib/src/main/assets']
}
}
}
dependencies {
compile project(':slidingMenuLib')
compile fileTree(dir: 'my_android_project_lib/libs', include: ['*.jar'])
compile fileTree(dir: 'slidingMenuLib/libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:1.7.1'
}
settings.gradle
include ':my_android_app'
include ':my_android_project_lib'
include ':slidingMenuLib'
project(':my_android_project_lib').projectDir = new File(settingsDir, '../my_android_project_lib')
build.gradle 用于 my_android_app 模块
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "my.android.app"
minSdkVersion 15
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(path: ':my_android_project_lib')
}
build.gradle 用于 my_android_app 项目
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
你知道如何解决这个问题吗?我遗漏了一些重要的东西。
我发现了问题。当我从 my_android_project_lib 模块的 build.gradle 中删除以下行时,应用程序的大小变回正常。
resources.srcDirs = ['anyoption_android_v2_lib/src']
my_android_project_lib 模块的最终 build.gradle 是:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
}
// The problem is here.
sourceSets {
main {
manifest.srcFile 'my_android_project_lib/src/main/AndroidManifest.xml'
java.srcDirs = ['my_android_project_lib/src']
res.srcDirs = ['my_android_project_lib/src/main/res']
// Removed
// resources.srcDirs = ['my_android_project_lib/src'] - MAIN PROBLEM
// aidl.srcDirs = ['my_android_project_lib/src']
// renderscript.srcDirs = ['my_android_project_lib/src']
// res.srcDirs = ['my_android_project_lib/src/main/res', 'slidingMenuLib/src/main/res']
// assets.srcDirs = ['my_android_project_lib/src/main/assets']
}
}
}
dependencies {
compile project(':slidingMenuLib')
compile fileTree(dir: 'my_android_project_lib/libs', include: ['*.jar'])
compile fileTree(dir: 'slidingMenuLib/libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:1.7.1'
}
正在评论
resources.srcDirs = ['src']
在 build.gradle
文件中,为我工作。
build.gradle
文件中的 SourceSet 如下所示,
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
// resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
最近我将我的项目迁移到 Android Studio。该项目引用了一个 库项目 而没有复制它(参见 this answer)。
我注意到从 Android Studio 生成的 apk 比从 Eclipse 生成的要大(几乎大两倍)。当我解压这两个 apk 文件时,我看到来自 Android Studio 的 apk 中还有一个文件夹 "main",它包含 "res" 文件夹和 AndroidManifest.xml。
问题是 apk 的主目录中有 "res" 文件夹 (/res),其中包含相同的资源 (/main/res)。
我认为下一个 gradle 文件导致了问题,但没有它项目无法构建。
build.gradle 用于我的库模块 - my_android_project_lib 模块
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
}
// I think this duplicates the resources
sourceSets {
main {
manifest.srcFile 'my_android_project_lib/src/main/AndroidManifest.xml'
java.srcDirs = ['my_android_project_lib/src']
resources.srcDirs = ['my_android_project_lib/src']
aidl.srcDirs = ['my_android_project_lib/src']
renderscript.srcDirs = ['my_android_project_lib/src']
res.srcDirs = ['my_android_project_lib/src/main/res', 'slidingMenuLib/src/main/res']
assets.srcDirs = ['my_android_project_lib/src/main/assets']
}
}
}
dependencies {
compile project(':slidingMenuLib')
compile fileTree(dir: 'my_android_project_lib/libs', include: ['*.jar'])
compile fileTree(dir: 'slidingMenuLib/libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:1.7.1'
}
settings.gradle
include ':my_android_app'
include ':my_android_project_lib'
include ':slidingMenuLib'
project(':my_android_project_lib').projectDir = new File(settingsDir, '../my_android_project_lib')
build.gradle 用于 my_android_app 模块
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "my.android.app"
minSdkVersion 15
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(path: ':my_android_project_lib')
}
build.gradle 用于 my_android_app 项目
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
你知道如何解决这个问题吗?我遗漏了一些重要的东西。
我发现了问题。当我从 my_android_project_lib 模块的 build.gradle 中删除以下行时,应用程序的大小变回正常。
resources.srcDirs = ['anyoption_android_v2_lib/src']
my_android_project_lib 模块的最终 build.gradle 是:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
}
// The problem is here.
sourceSets {
main {
manifest.srcFile 'my_android_project_lib/src/main/AndroidManifest.xml'
java.srcDirs = ['my_android_project_lib/src']
res.srcDirs = ['my_android_project_lib/src/main/res']
// Removed
// resources.srcDirs = ['my_android_project_lib/src'] - MAIN PROBLEM
// aidl.srcDirs = ['my_android_project_lib/src']
// renderscript.srcDirs = ['my_android_project_lib/src']
// res.srcDirs = ['my_android_project_lib/src/main/res', 'slidingMenuLib/src/main/res']
// assets.srcDirs = ['my_android_project_lib/src/main/assets']
}
}
}
dependencies {
compile project(':slidingMenuLib')
compile fileTree(dir: 'my_android_project_lib/libs', include: ['*.jar'])
compile fileTree(dir: 'slidingMenuLib/libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:1.7.1'
}
正在评论
resources.srcDirs = ['src']
在 build.gradle
文件中,为我工作。
build.gradle
文件中的 SourceSet 如下所示,
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
// resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}