From a7e5acefebeac9826b0ab341a900f14ad487f93d Mon Sep 17 00:00:00 2001 From: blagoev Date: Thu, 6 Oct 2016 16:08:31 +0300 Subject: [PATCH] Strip symbols in release builds (Guard against BAD GRADLE PLUGIN) --- runtime/build.gradle | 185 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 3 deletions(-) diff --git a/runtime/build.gradle b/runtime/build.gradle index f13d7e546..de675261b 100644 --- a/runtime/build.gradle +++ b/runtime/build.gradle @@ -1,5 +1,7 @@ println "Build.gradle running" +import org.gradle.internal.os.OperatingSystem; + apply plugin: 'com.android.model.library' buildscript { @@ -17,8 +19,6 @@ allprojects { } } - - def ndkDebuggable = false; @@ -31,6 +31,162 @@ for (String runTask : runTasks) { } +def ndkDir = System.getenv("ANDROID_NDK_HOME") +def propertiesFile = project.rootProject.file('local.properties') +if (propertiesFile.exists()) { + Properties properties = new Properties() + properties.load(propertiesFile.newDataInputStream()) + ndkDir = properties.getProperty('ndk.dir') +} +project.ext.ndkDir = ndkDir; + + +class StripSymbolsTaskExecutionListener implements TaskExecutionListener +{ + @Override + void beforeExecute(Task task) + { + } + + String getMachineName() + { + if (OperatingSystem.current().isWindows()) + { + return "windows"; + } + else if (OperatingSystem.current().isMacOsX()) + { + return "darwin"; + } + else + { + return "linux"; + } + } + + String getStripToolName(String stripTaskName) + { + stripTaskName = stripTaskName.toLowerCase(); + def stripToolName = "" + if (stripTaskName.contains("arm64")) + { + stripToolName = "aarch64-linux-android-strip"; + } + else if (stripTaskName.contains("arm")) + { + stripToolName = "arm-linux-androideabi-strip"; + } + else if (stripTaskName.contains("mips64")) + { + stripToolName = "mips64el-linux-android-strip"; + } + else if (stripTaskName.contains("mips")) + { + stripToolName = "mipsel-linux-android-strip"; + } + else if (stripTaskName.contains("x86_64")) + { + stripToolName = "x86_64-linux-android-strip"; + } + else if (stripTaskName.contains("x86")) + { + stripToolName = "i686-linux-android-strip"; + } + else + { + throw new IllegalArgumentException("NOT SUPPORTED ARCHITECTURE NAME: ${stripTaskName}"); + } + + if (OperatingSystem.current().isWindows()) + { + stripToolName += ".exe"; + } + + return stripToolName; + } + + String getStripToolDirectory(String stripTaskName) + { + stripTaskName = stripTaskName.toLowerCase(); + def stripToolDirectory = "" + if (stripTaskName.contains("arm64")) + { + stripToolDirectory = "aarch64-linux-android-4.9"; + } + else if (stripTaskName.contains("arm")) + { + stripToolDirectory = "arm-linux-androideabi-4.9"; + } + else if (stripTaskName.contains("mips64")) + { + stripToolDirectory = "mips64el-linux-android-4.9"; + } + else if (stripTaskName.contains("mips")) + { + stripToolDirectory = "mipsel-linux-android-4.9"; + } + else if (stripTaskName.contains("x86_64")) + { + stripToolDirectory = "x86_64-4.9"; + } + else if (stripTaskName.contains("x86")) + { + stripToolDirectory = "x86-4.9"; + } + else + { + throw new IllegalArgumentException("NOT SUPPORTED ARCHITECTURE: ${stripTaskName}"); + } + + return stripToolDirectory; + } + + void afterExecute(Task task, TaskState taskState) + { + if (task.getName().contains("stripSymbols") && !taskState.failure) + { + println "Found STRIP COMMAND: ${task.getName()}" + println "input file " + task.getInputs().getFiles().getAsPath(); + println "output dir " + task.getOutputs().getFiles().getAsPath(); + def _ndkDir = task.project.ext.ndkDir; + + def libName = new File(task.getInputs().getFiles().getAsPath()).getName(); + def libToStripPath = new File(task.getOutputs().getFiles().getAsPath(), libName).getAbsolutePath(); + + println "output file ${libToStripPath}"; + + def stripToolName = getStripToolName(task.getName()); + def machineName = getMachineName(); + def stripToolDirectory = getStripToolDirectory(task.getName()); + def s = File.separatorChar; + def stripToolPath = "${_ndkDir}${s}toolchains${s}${stripToolDirectory}${s}prebuilt${s}${machineName}-x86_64${s}bin${s}${stripToolName}" + + println "Library to strip: ${libToStripPath}" + println "Strip tool: ${stripToolPath}" + + def command = ""; + if (OperatingSystem.current().isWindows()) + { + command = "cmd /c ${stripToolPath} ${libToStripPath}"; + } + else + { + command = "${stripToolPath} ${libToStripPath}"; + } + + println "Executing strip command: ${command}" + def result = command.execute(); + println result.text; + } + } +} + +if (!ndkDebuggable) +{ + project.gradle.addListener(new StripSymbolsTaskExecutionListener()) +} + + model { android { compileSdkVersion = 23 @@ -101,11 +257,16 @@ model { setRuntimeCommit.dependsOn(setPackageVersion) tasks.whenTaskAdded { task -> - if (task.getName() == "androidRelease") { + if (task.getName() == "androidRelease") + { task.dependsOn(setRuntimeCommit) task.mustRunAfter setRuntimeCommit task.finalizedBy revertVersionFile } +// else if (task.getName().contains("stripSymbols") +// { +// +// } } } } @@ -218,6 +379,24 @@ task setRuntimeCommit { } } +//task stripArmBinary(type: Exec) { +// onlyIf { +// ndkDebuggable == false +// } +// +// doFirst { +// def binaryPath = "$rootDir/build/intermediates/binaries/release/lib/armeabi-v7a/libNativeScript.so" +// println "Striping ARM binary ${binaryPath}" +// +// if (isWinOs) { +// commandLine "cmd", "/c", "git", "checkout", "--", versionFilePath +// } else { +// commandLine "git", "checkout", "--", versionFilePath +// } +// +// } +//} + task revertVersionFile(type: Exec) { onlyIf {