Skip to content

Instantly share code, notes, and snippets.

@vovkasm
Last active November 6, 2019 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vovkasm/811da54d3fb1e0737e202fcb315f49ad to your computer and use it in GitHub Desktop.
Save vovkasm/811da54d3fb1e0737e202fcb315f49ad to your computer and use it in GitHub Desktop.
Android build with additional dimensions (snippets)

Directory structure

dir structure

android {
compileSdkVersion rootProject.ext.compileSdkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.sampleorg.SampleDev"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionName versName
versionCode versCode as Integer
multiDexEnabled true
resConfigs "ru"
}
splits {
abi {
enable enableSeparateBuildPerCPUArchitecture
reset()
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
universalApk false // If true, also generate a universal APK
}
}
buildTypes {
release {
// Uncomment to quick test release build on device
// signingConfig debug.signingConfig
minifyEnabled true
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
debug {
debuggable true
}
}
flavorDimensions "channel"
productFlavors {
staging {
dimension "channel"
}
production {
dimension "channel"
applicationId "com.sampleorg.Sample"
}
}
applicationVariants.all { com.android.build.gradle.api.ApplicationVariant variant ->
String SAMPLE_BUILD_CONFIGURATION = 'Debug'
String SAMPLE_AMPLITUDE_KEY = "xxx"
String SAMPLE_FACEBOOK_APP_ID = "xxx"
String SAMPLE_VK_APP_ID = "xxx"
String SAMPLE_APPSFLYER_KEY = "xxx"
String SAMPLE_PLACES_API_KEY = "xxx" // from google-services.json :-/
String SAMPLE_URI_SCHEME = "sampledev"
if (variant.flavorName == 'production') {
SAMPLE_BUILD_CONFIGURATION = 'Production'
SAMPLE_AMPLITUDE_KEY = "xxx"
SAMPLE_FACEBOOK_APP_ID = "xxx"
SAMPLE_VK_APP_ID = "xxx"
SAMPLE_PLACES_API_KEY = "xxx"
SAMPLE_URI_SCHEME = "sample"
} else if (variant.name == 'stagingRelease') {
SAMPLE_BUILD_CONFIGURATION = 'Staging'
}
variant.buildConfigField("String", "SAMPLE_BUILD_CONFIGURATION", '"' + SAMPLE_BUILD_CONFIGURATION + '"')
variant.buildConfigField("String", "SAMPLE_AMPLITUDE_KEY", '"' + SAMPLE_AMPLITUDE_KEY + '"')
variant.buildConfigField("String", "SAMPLE_APPSFLYER_KEY", '"' + SAMPLE_APPSFLYER_KEY + '"')
resValue "string", "facebook_app_id", SAMPLE_FACEBOOK_APP_ID
resValue "string", "fb_login_protocol_scheme", "fb" + SAMPLE_FACEBOOK_APP_ID
resValue "integer", "com_vk_sdk_AppId", SAMPLE_VK_APP_ID
resValue "string", "places_api_key", SAMPLE_PLACES_API_KEY
resValue "string", "sample_uri_scheme", SAMPLE_URI_SCHEME
}
// 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, "arm64-v8a": 3, "x86_64": 4]
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
}
}
}
applicationVariants.all { com.android.build.gradle.api.ApplicationVariant variant ->
def mergeAssetsTask = variant.mergeAssetsProvider.get()
mergeAssetsTask.doLast {
copy {
def assetsOutputDir = mergeAssetsTask.outputDir.get()
from([
"../../node_modules/react-native-vector-icons/Fonts/Ionicons.ttf",
"../../fonts/custom.ttf"
])
into("${assetsOutputDir}/fonts")
}
}
}
}
@ReactModule(name= SampleNativeModule.NAME)
public class SampleNativeModule extends ReactContextBaseJavaModule {
final static String NAME = "SampleNative";
public SampleNativeModule(@NonNull ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return NAME;
}
@Nullable
@Override
public Map<String, Object> getConstants() {
Map<String, Object> constants = new HashMap<>();
constants.put("appBuild", String.valueOf(BuildConfig.VERSION_CODE));
constants.put("appIdentifier", BuildConfig.APPLICATION_ID);
constants.put("appVersion", BuildConfig.VERSION_NAME);
constants.put("configuration", BuildConfig.SAMPLE_BUILD_CONFIGURATION);
constants.put("isDevice", isDevice());
constants.put("marketUrl", "market://details?id=" + BuildConfig.APPLICATION_ID);
return constants;
}
@Override
public boolean hasConstants() {
return true;
}
@ReactMethod
public void openNotificationsSettings(Promise promise) {
ReactContext reactContext = getReactApplicationContext();
Intent intent = getOpenNotificationsSettingsIntent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
reactContext.startActivity(intent);
promise.resolve(null);
}
private boolean isDevice() {
return true;
}
private Intent getOpenNotificationsSettingsIntent() {
ReactContext reactContext = getReactApplicationContext();
String packageName = reactContext.getPackageName();
Intent intent = new Intent();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", packageName);
intent.putExtra("app_package", packageName);
intent.putExtra("app_uid", reactContext.getApplicationInfo().uid);
} else {
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + packageName));
}
return intent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment