Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Add support for split build per architecture #5160

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion local-cli/generator-android/templates/src/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
apply plugin: "com.android.application"

import com.android.build.OutputFile

/**
* The react.gradle file registers two tasks: bundleDebugJsAndAssets and bundleReleaseJsAndAssets.
* These basically call `react-native bundle` with the correct arguments during the Android build
Expand Down Expand Up @@ -49,6 +51,22 @@ apply plugin: "com.android.application"

apply from: "react.gradle"

/**
* Set this to true to create three separate APKs instead of one:
* - A universal APK that works on all devices
* - An APK that only works on ARM devices
* - An APK that only works on x86 devices
* The advantage is the size of the APK is reduced by about 4MB.
* Upload all the APKs to the Play Store and people will download
* the correct one based on the CPU architecture of their device.
*/
def enableSeparateBuildPerCPUArchitecture = false

/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = true

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
Expand All @@ -63,12 +81,33 @@ android {
abiFilters "armeabi-v7a", "x86"
}
}
splits {
abi {
enable enableSeparateBuildPerCPUArchitecture
universalApk true
reset()
include "armeabi-v7a", "x86"
}
}
buildTypes {
release {
minifyEnabled false // Set this to true to enable Proguard
minifyEnabled enableProguardInReleaseBuilds
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]
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
}
}
}
}

dependencies {
Expand Down
23 changes: 21 additions & 2 deletions local-cli/generator-android/templates/src/app/react.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,33 @@ task bundleReleaseJsAndAssets(type: Exec) {
enabled config.bundleInRelease ?: true
}

void runBefore(String dependentTaskName, Task task) {
Task dependentTask = tasks.findByPath(dependentTaskName);
if (dependentTask != null) {
dependentTask.dependsOn task
}
}

gradle.projectsEvaluated {

// hook bundleDebugJsAndAssets into the android build process

bundleDebugJsAndAssets.dependsOn mergeDebugResources
bundleDebugJsAndAssets.dependsOn mergeDebugAssets
processDebugResources.dependsOn bundleDebugJsAndAssets

runBefore('processArmeabi-v7aDebugResources', bundleDebugJsAndAssets)
runBefore('processX86DebugResources', bundleDebugJsAndAssets)
runBefore('processUniversalDebugResources', bundleDebugJsAndAssets)
runBefore('processDebugResources', bundleDebugJsAndAssets)

// hook bundleReleaseJsAndAssets into the android build process

bundleReleaseJsAndAssets.dependsOn mergeReleaseResources
bundleReleaseJsAndAssets.dependsOn mergeReleaseAssets
processReleaseResources.dependsOn bundleReleaseJsAndAssets

runBefore('processArmeabi-v7aReleaseResources', bundleReleaseJsAndAssets)
runBefore('processX86ReleaseResources', bundleReleaseJsAndAssets)
runBefore('processUniversalReleaseResources', bundleReleaseJsAndAssets)
runBefore('processReleaseResources', bundleReleaseJsAndAssets)

}