3

When developing for Instant Apps the use of Configuration APKs (https://developer.android.com/topic/instant-apps/guides/config-splits.html) offers useful option for cutting down on APK size. However it looks like they are only supported when using mindSdk of 21 or later. For example, you get following error if you try to use this capability for lower sdk versions.

MinSdkVersion 17 is too low (<21) to support pure splits, reverting to full APKs

Is there way to have base module for example use pure split functionality while still having installed app target pre lollipop devices?

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63

3 Answers3

2

Since the minSdkVersion flag will toggle quite a few checks, this change is not done automatically for you.

I'd recommend introducing a product flavor for both the installed and instant module with different minSdkVersions, like this:

// :feature/base/build.gradle

flavorDimensions 'delivery'

productFlavors {
    instant {
        dimension 'delivery'
        minSdkVersion rootProject.minSdkInstant
    }
    installed {
        dimension 'delivery'
    }
}

This will have to be done similarly in other modules that depend on the feature module with API level < 21.

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
  • 1
    @kayboardsurfer I already have 3 product flavors in use right now.....guess I should be able to use 2 `flavorDimensions` values to combine these. – John O'Reilly Oct 30 '17 at 17:41
1

Instant apps are only supported in API 21+ devices (link), older devices will only support full apk.

The better approach is to define different minSdk based on your module, like:

You can take a look at Google's analytics sample on GitHub

Project's gradle

ext {
    buildTools = '26.0.2'
    compileSdk = 26
    minSdk = 15
    minSdkInstant = 21
    versionCode = 1
    versionName = '1.0'
    supportLib = '26.1.0'
    firebaseVer = '10.2.4'
    instantAppsVer = '1.0.0'
}

Base gradle

android {
    compileSdkVersion rootProject.compileSdk
    buildToolsVersion rootProject.buildTools
    baseFeature true
    defaultConfig {
        minSdkVersion rootProject.minSdk
        targetSdkVersion rootProject.compileSdk
        versionCode rootProject.versionCode
        versionName rootProject.versionName
    }
    buildTypes {
        release {}
    }
}

Installed gradle

 defaultConfig {
        applicationId "com.example.android.instant.analytics"
        minSdkVersion rootProject.minSdk
        targetSdkVersion rootProject.compileSdk
        versionCode rootProject.versionCode
        versionName rootProject.versionName

    }

Instant gradle

android {
    defaultConfig {
        minSdkVersion rootProject.minSdkInstant
    }
}
Ariel Carbonaro
  • 1,529
  • 1
  • 13
  • 25
  • that would allow using different `minSdk` for instant app module....the issue I'm having is with something like base module (that's shared between instant and installed app)....and for which I want to start using that split functionality – John O'Reilly Oct 27 '17 at 16:41
  • @JohnO'Reilly your base module should have minSdkVersion 17 and your instant app module should have minSdkVersion 21. I've edited my answer with a detailed example – Ariel Carbonaro Oct 27 '17 at 16:52
  • 1
    the problem with that then is that I can't setup configuration splits (https://developer.android.com/topic/instant-apps/guides/config-splits.html) for the base module – John O'Reilly Oct 27 '17 at 22:02
1

You can specify a different lower minSdkVersion for your application module if you use tools:overrideLibrary Please see How do I use tools:overrideLibrary in a build.gradle file?

This is how I did it:

app/build.gradle

android {
    defaultConfig {
        minSdkVersion 17
    }
}

app’s manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.package.app" xmlns:tools="http://schemas.android.com/tools" >

    <uses-sdk tools:overrideLibrary="com.package,com.package.feature"/>

</manifest>

You will need to add all the package names where you wish to override their minSdkVersion. You will know which ones to add by looking at the error:

Manifest merger failed ... AndroidManifest.xml as the library might be using APIs not available in 17 Suggestion: use a compatible library with a minSdk of at most 17, or increase this project's minSdk version to at least 21, or use tools:overrideLibrary="com.package.feature" to force usage (may lead to runtime failures)

base and feature/build.gradle

android {
    defaultConfig {
        minSdkVersion 21
    }
}

Test it by building the installed/instant APKs, then analyzing then for the minSdkVersion value. The installed should be at 17, and your base/feature APKs will still be on 21.

Prags
  • 2,457
  • 2
  • 21
  • 38