2

Recently I migrated my project to Android Studio. The project references a library project without copying it (See this answer).

I noticed that the apk generated from Android Studio is larger than the one generated from Eclipse (almost twice larger). When I unrar the two apk files I see that in the apk from Android Studio there is one more folder "main" and it contains "res" folder and AndroidManifest.xml.

The problem is that there is "res" folder (/res) in apk's main directory which contains the same resources (/main/res).

I think that the next gradle file causes the problem but without it the project can not be built.

build.gradle for my library module - my_android_project_lib module

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 for my_android_app module

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 for my_android_app project

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

Do you know how to fix the problem? I'm missing something important.

Community
  • 1
  • 1
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
  • Could you post the size of each element in the APKs? This will help identifying where the problem lies. – Sebastiano Jan 29 '15 at 15:25
  • The fact that you're pulling in both `'my_android_project_lib/src/main/res'` and `'slidingMenuLib/src/main/res'` in your main project's build is problematic. In fact, even though ti depends on slidingMenuLib as a submodule it ends up bringing in a lot of its stuff explicitly anyway. – Scott Barta Jan 29 '15 at 17:29

2 Answers2

4

I found the problem. When I removed the following line from my build.gradle of my_android_project_lib module, application's size changed back to normal.

resources.srcDirs = ['anyoption_android_v2_lib/src']

The final build.gradle for my_android_project_lib module is:

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'
}
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
1

on commenting

resources.srcDirs = ['src']

in build.gradle file, worked for me.

The SourceSets in build.gradle file looks like this,

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')
}
pallavi
  • 432
  • 3
  • 14