Skip to content

Commit

Permalink
refactor(android): replace SDK build.properties with BuildConfig (#11606
Browse files Browse the repository at this point in the history
)

- Used to provide Titanium SDK version string, version code, build time, and build hash.
  • Loading branch information
jquick-axway committed Apr 8, 2020
1 parent f881591 commit 2ca1f46
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 93 deletions.
47 changes: 36 additions & 11 deletions android/titanium/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2019 by Axway. All Rights Reserved.
* Copyright (c) 2009-2020 by Axway. All Rights Reserved.
* Licensed under the terms of the Apache Public License.
* Please see the LICENSE included with this distribution for details.
*/

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

// Fetch Titanium version info to be applied to generated "BuildConfig" Java class and published AAR.
// Prefer environment variables assigned via build/package scripts under "titanium_mobile/build" folder.
def tiBuildVersionString = System.env.TI_SDK_BUILD_VERSION
if (tiBuildVersionString == null) {
def packageJson = new groovy.json.JsonSlurper().parse(file("${projectDir}/../../package.json"))
tiBuildVersionString = packageJson.version
if (tiBuildVersionString == null) {
tiBuildVersionString = '1.0.0'
}
}
def tiBuildHashString = System.env.TI_SDK_BUILD_GIT_HASH
if (tiBuildHashString == null) {
tiBuildHashString = 'HEAD'
}
def tiBuildTimeString = System.env.TI_SDK_BUILD_TIMESTAMP
if (tiBuildTimeString == null) {
tiBuildTimeString = (new Date()).format('MM/dd/yyyy HH:mm', TimeZone.getTimeZone("UTC"))
}
def tiBuildVersionCode = 0
for (nextString in tiBuildVersionString.split('\\.')) {
def intValue = Math.max(Integer.parseInt(nextString), 0)
if (tiBuildVersionCode <= 0) {
tiBuildVersionCode = intValue
} else {
tiBuildVersionCode *= 100
tiBuildVersionCode += Math.min(intValue, 99)
}
}

android {
compileSdkVersion 29
defaultConfig {
minSdkVersion 19
targetSdkVersion 29
versionName tiBuildVersionString
versionCode tiBuildVersionCode
buildConfigField('String', 'TI_BUILD_HASH_STRING', '"' + tiBuildHashString + '"')
buildConfigField('String', 'TI_BUILD_TIME_STRING', '"' + tiBuildTimeString + '"')
manifestPlaceholders = project.ext.tiManifestPlaceholders
javaCompileOptions {
annotationProcessorOptions {
Expand Down Expand Up @@ -163,7 +196,7 @@ project.afterEvaluate {
}

// Runs our "prebuild.js" script before the C/C++ compile, but after Java compile. (Mid-build script?)
// This downloads the V8 library, generates C/C++ files, and generates some asset files.
// Generates C/C++ files providing our Android-only JS files via byte arrays.
tasks.withType(JavaCompile) {
dependsOn snapshotTiCommonFiles
doLast {
Expand Down Expand Up @@ -228,18 +261,10 @@ dependencies {
publishing {
publications {
titaniumPublication(MavenPublication) {
// Use the base version string (such as "9.0.0") without the ".RC" or ".GA" appended to it.
// Note: Ideally, we should use the full "versionTag" string, but our Jenkins build system
// currently appends the ".RC" or ".GA" part after the "node scons package" process.
def versionString = '1.0.0'
if (System.env.TI_SDK_BUILD_VERSION != null) {
versionString = System.env.TI_SDK_BUILD_VERSION
}

// Set up maven repo info.
groupId 'org.appcelerator'
artifactId 'titanium'
version versionString
version tiBuildVersionString
artifact file("${buildDir}/outputs/aar/titanium-release.aar")

// Generates the "*.pom" XML file containing all of Titanium's above dependencies,
Expand Down
56 changes: 2 additions & 54 deletions android/titanium/prebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const exec = util.promisify(require('child_process').exec); // eslint-disable-li
const fs = require('fs-extra');
const glob = util.promisify(require('glob'));
const path = require('path');
const timestamp = require('../../build/lib/utils').timestamp;

// Determine if we're running on a Windows machine.
const isWindows = (process.platform === 'win32');
Expand Down Expand Up @@ -56,51 +55,6 @@ async function gperf(workingDirPath, inputFilePath, outputFilePath) {
await fs.writeFile(outputFilePath, stdout);
}

/** Generates a "build.properties" file under "assets" providing Titanium SDK's build version and time. */
async function generateBuildProperties() {
// This function escapes the "value" string in a property key=value pair.
function escapeValue(stringValue) {
if (stringValue) {
stringValue = stringValue.replace(/\\/g, '\\\\');
stringValue = stringValue.replace(/:/g, '\\:');
stringValue = stringValue.replace(/[\r\n]/g, '');
stringValue = stringValue.replace(/[\u0080-\uFFFF]/g, (match) => {
return '\\u' + ('0000' + match.charCodeAt(0).toString(16)).slice(-4);
});
}
return stringValue;
}

// Fetch Titanium SDK's build version and git hash from environment variables.
let buildVersion = process.env.TI_SDK_BUILD_VERSION;
if (!buildVersion) {
buildVersion = '1.0.0';
}
let buildGitHash = process.env.TI_SDK_BUILD_GIT_HASH;
if (!buildGitHash) {
buildGitHash = 'HEAD';
}

let buildTimestamp = process.env.TI_SDK_BUILD_TIMESTAMP;
if (!buildTimestamp) {
buildTimestamp = timestamp();
}

// Create the "build.properties" content.
const fileContentString
= '#Generated by Titanium\n'
+ '\n'
+ 'build.version=' + escapeValue(buildVersion) + '\n'
+ 'build.githash=' + escapeValue(buildGitHash) + '\n'
+ 'build.timestamp=' + escapeValue(buildTimestamp) + '\n';

// Create the "build.properties" file under the "assets" directory.
const directoryPath = path.join(__dirname, 'assets', 'Resources', 'ti.internal');
const filePath = path.join(directoryPath, 'build.properties');
await fs.mkdirs(directoryPath);
await fs.writeFile(filePath, fileContentString);
}

/** Generates C/C++ source files containing internal JS files and from gperf templates. */
async function generateSourceCode() {
// Generate a "KrollNativeBindings.h" file with perfect hashes via gperf tool.
Expand Down Expand Up @@ -168,14 +122,8 @@ async function generateSourceCode() {

/** Executes the pre-build step. */
async function main() {
// Run the following operations in parallel.
await Promise.all([
// Generate a "build.properties" file providing the Titanium SDK's version and other info.
generateBuildProperties(),

// Generate C/C++ source files with JS files embedded in them and from gperf templates.
generateSourceCode()
]);
// Generate C/C++ source files with JS files embedded in them and from gperf templates.
await generateSourceCode();
}

main()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@
import ti.modules.titanium.TitaniumModule;

import java.io.File;
import java.io.InputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -93,7 +91,6 @@ public abstract class TiApplication extends Application implements KrollApplicat
private TiProperties appProperties;
private WeakReference<Activity> currentActivity;
private String density;
private String buildVersion = "", buildTimestamp = "", buildHash = "";
private String defaultUnit;
private TiResponseCache responseCache;
private BroadcastReceiver localeReceiver;
Expand Down Expand Up @@ -309,26 +306,6 @@ public Activity getRootOrCurrentActivity()
return null;
}

protected void loadBuildProperties()
{
// Initialize build property member variables.
this.buildVersion = "1.0.0";
this.buildTimestamp = "N/A";
this.buildHash = "N/A";

// Attempt to read the "build.properties" file.
try (InputStream stream = getAssets().open("Resources/ti.internal/build.properties")) {
if (stream != null) {
Properties properties = new Properties();
properties.load(stream);
this.buildVersion = properties.getProperty("build.version", this.buildVersion);
this.buildTimestamp = properties.getProperty("build.timestamp", this.buildTimestamp);
this.buildHash = properties.getProperty("build.githash", this.buildHash);
}
} catch (Exception e) {
}
}

@Override
public void loadAppProperties()
{
Expand Down Expand Up @@ -356,8 +333,6 @@ public void onCreate()
super.onCreate();
Log.d(TAG, "Application onCreate", Log.DEBUG_MODE);

loadBuildProperties();

// handle uncaught java exceptions
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
Expand Down Expand Up @@ -719,7 +694,7 @@ public String getDeployType()
*/
public String getTiBuildVersion()
{
return buildVersion;
return BuildConfig.VERSION_NAME;
}

@Override
Expand All @@ -730,12 +705,12 @@ public String getSDKVersion()

public String getTiBuildTimestamp()
{
return buildTimestamp;
return BuildConfig.TI_BUILD_TIME_STRING;
}

public String getTiBuildHash()
{
return buildHash;
return BuildConfig.TI_BUILD_HASH_STRING;
}

@Override
Expand Down

0 comments on commit 2ca1f46

Please sign in to comment.