Daniel Wilson Daniel Wilson - 3 months ago 23
Android Question

Android Studio stale APK for build variant after xml changes

This is a very strange one. Today AS (1.4) stopped correctly re-creating the debug apk in my project:


  • Any XML changes are not reflected in the code when running the project

  • Changes to Java code are propagating, even when the XML changes are not

  • Cleaning and then hitting run seems to fix it, an updated APK is successfully created every time.

  • I have two build variants in my project, this problem only affects one of them.

  • I am using Genymotion, but the problem exists on the regular emulator and hardware as well.

  • I suspect there is some kind of lock being placed on the xml files. For example if I rename a file and hit run, the changes propagate. But inspecting the R.java shows entries for both the old file name and the new one - the old one is simply not being cleared / refreshed.

  • The xml layout I'm testing this with has now been stripped back to a blank relative layout with a background color. The color is not being updated when it should be.



I don't really know much about the build process, so all I have tried is deleting the build folder, re-importing the project and the old restart / invalidate cache on Android Studio. Any suggestions as to what might cause this strange behavior or how to fix it?

Edit:
It's very difficult to pin down the behavior behind this - sometimes I've noticed it consistently affecting only certain xml files. Looks like this guy has the same issue: Android Studio project build issue

I've spent a couple hours reinstalling Android Studio from scratch and the issue remains..

Answer

I actually figured out the issue here a while ago and it was pretty subtle. My project has build variants with a main source set as well as a source set for each variant. It was defined somewhat like this:

sourceSets {

    def defaultDirectory = "src/main/"
    def flavorADirectory = "src/flavor_a/"

    flavor_a {
        java.srcDirs = [defaultDirectory + 'java', flavorADirectory + 'java']
        res.srcDirs = [defaultDirectory + 'res', flavorADirectory + 'res']
        manifest.srcFile flavorADirectory + 'AndroidManifest.xml'
    }

    def flavorBDirectory = "src/flavor_b/"

    flavor_b{
        java.srcDirs = [defaultDirectory + 'java', flavorBDirectory + 'java']
        res.srcDirs = [defaultDirectory + 'gift', flavorBDirectory + 'res']
        manifest.srcFile flavorBDirectory + 'AndroidManifest.xml'
    }
}

So the variants compiled fine, but every xml change was triggering a complete rebuild. I'm not sure the exact reasoning behind it, but I believe the main sourceset must be specified as a separate entity. Android Studio must see the main keyword and know better what to do with it. That means there is no need to specify the default directory as part of the build variant source sets Removing that like so, makes the problem go away and no more constant rebuilds occur:

 sourceSets {

    def defaultDirectory = "src/main/"

    main {
        java.srcDirs = [defaultDirectory + 'java']
        res.srcDirs = [defaultDirectory + 'res']
    }

    def flavorADirectory = "src/flavor_a/"

    flavor_a {
        java.srcDirs = [flavorADirectory + 'java']
        res.srcDirs = [flavorADirectory + 'res']
        manifest.srcFile flavorADirectory + 'AndroidManifest.xml'
    }

    def flavorBDirectory = "src/flavor_b/"

    flavor_b{
        java.srcDirs = [flavorBDirectory + 'java']
        res.srcDirs = [flavorBDirectory + 'res']
        manifest.srcFile flavorBDirectory + 'AndroidManifest.xml'
    }
}
Comments