Skip to content

Instantly share code, notes, and snippets.

@hkkcngz
Last active September 12, 2022 12:55
Show Gist options
  • Save hkkcngz/d5f8242dafcb28576d502f96e4a9505d to your computer and use it in GitHub Desktop.
Save hkkcngz/d5f8242dafcb28576d502f96e4a9505d to your computer and use it in GitHub Desktop.
build.gradle
plugins {
id 'com.android.application'
}
import java.util.regex.Matcher
import java.util.regex.Pattern
/*
sonarqube {
properties {
property "sonar.projectName", "LargeProjectExample"
property "sonar.projectKey", "LargeProjectExample"
property "sonar.host.url", "http://localhost:9000"
property "sonar.language", "java"
property "sonar.login", "admin"
property "sonar.password", "******"
}
}
*/
android {
useLibrary 'org.apache.http.legacy'
compileSdk 32
defaultConfig {
applicationId "com.largeproject.example"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
resConfigs "en", "tr"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
signingConfigs {
release {
keyAlias 'key0'
keyPassword '******'
storeFile file('27tkey.jks')
storePassword '******'
}
}
buildTypes {
// ORTAMLAR SÜREÇLER
release {
signingConfig signingConfigs.release
// zipAlignEnabled true //
//shrinkResources true //res
minifyEnabled true //java code
// multiDexEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debuggable false
}
debug {
//applicationIdSuffix ".debug"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debuggable true
}
dev {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debuggable true
/* DEV ORTAMI */
buildConfigField "String", "INFO", '"someinfodev"'
}
plat {
/* PLATFORM ORTAMI */
buildConfigField "String", "INFO", '"someinfoplat"'
}
prod {
/* PROD ORTAMI */
buildConfigField "String", "INFO", '"someinfoprod"'
}
}
flavorDimensions "APP"
productFlavors {
largeProject {
dimension "APP"
// DEĞİŞTİR
String appName = "Large Project"
String appVersionName = "4.1.55"
Integer appVersionCode = 255
String appIdSuffix = ".largeproject"
// DEĞİŞTİR
manifestPlaceholders = [appName: appName, appIcon: "@drawable/ic_second"]
resValue("string", "app_name", appName)
versionName appVersionName
versionCode appVersionCode
applicationIdSuffix appIdSuffix
}
largeProject2 {
dimension "APP"
String appName = "Another Large Project"
String appVersionName = "4.1.55"
Integer appVersionCode = 255
String appIdSuffix = ".largeProject2"
manifestPlaceholders = [appName: appName, appIcon: "@drawable/ic_second"]
resValue("string", "app_name", appName)
versionName appVersionName
versionCode appVersionCode
applicationIdSuffix appIdSuffix
buildConfigField 'String', 'merhabaa', '"selam"'
}
}
// Ignore Operations. Yürürlükte olmayan süreçleri gizlemek için. contains kullanılmamalıdır.
variantFilter { variant ->
String uygulama = variant.flavors*.name.get(0).toLowerCase()
String buildType = variant.buildType.name.toLowerCase()
// println("uygulama : " + uygulama + " buildType : " + buildType)
if (uygulama == "largeproject" && buildType == "ent") {
variant.setIgnore(true)
}
}
// Varsayılan Medya Kayıt Yerini Değiştirme Groovy
List<String> resBuildTypes = new ArrayList<String>()
List<String> resBuildType = new ArrayList<String>()
resBuildTypes.add("src/main/res/ortak")
productFlavors.all { flavor ->
String folder = "src/main/res/" + flavor.name
resBuildTypes.add(folder)
resBuildType.add(flavor.name)
}
//println resBuildTypes //[src/main/res/ortak, src/main/res/largeProject, src/main/res/largeProject2]
//println resBuildType //[largeProject, largeProject2]
sourceSets {
main.res.srcDirs = resBuildTypes
//main.manifest.srcFile = "src/main/res/"
for (String var : resBuildType) {
"$var" {
setRoot "src/main/res/" + var
}
}
}
buildFeatures {
buildFeatures.dataBinding = true
buildFeatures.viewBinding = true
}
}
// Build alınan proje isteği bilgisini almamızı sağlayan task.
def projectName
def projectBuildType
task getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern
// take buildTypes to String
String buildTypes = ""
android.buildTypes.all {
type ->
//println(type.name) Capitalize
String output = type.name.substring(0, 1).toUpperCase() + type.name.substring(1)
buildTypes += output + "|"
}
buildTypes = buildTypes.substring(0, buildTypes.length() - 1) //delete last | character
//println buildTypes
if (tskReqStr.contains("assemble"))
pattern = Pattern.compile("assemble(\\w+)($buildTypes)") // Oluşturulan Her BuildType İçin Ekleme Yapılması Gereklidir. Dev|Ent|Tst,
else
pattern = Pattern.compile("generate(\\w+)($buildTypes)") // $buildTypes bu isteği otomatize ederek karşılar.
Matcher matcher = pattern.matcher(tskReqStr)
if (matcher.find()) {
//projectVariant = matcher.group().toLowerCase()
projectName = matcher.group(1).toLowerCase() // Changing to 2 will return build type, 1 provides product flavor
projectBuildType = matcher.group(2).toLowerCase() // Changing to 2 will return build type, 1 provides product flavor
} else {
println "NO MATCH FOUND"
}
}
task buildVariantTasks(type: Copy) {
dependsOn getCurrentFlavor
println "flavor name is " + projectName
println "build type is " + projectBuildType
if(projectName!=null && projectBuildType!=null) {
// Class ve Resourse'ları Ayırma
// Ortak Alınan Class Dosyalarını Belirlemek İçin "ortak" yazan kısmı değiştirmeniz yeterli.
String appID = android.defaultConfig.applicationId
String appIdSlashes = "src/main/java/" + appID.replaceAll("\\.", "/") + "/"
android.sourceSets.main {
java.srcDirs = [appIdSlashes + "ortak", appIdSlashes + projectName]
res.srcDirs = ['src/main/res/ortak', 'src/main/res/'+ projectName]
manifest.srcFile "src/main/res/" + projectName + "/AndroidManifest.xml"
}
android.applicationVariants.all { variant ->
// Output APK Name & appVer - Her Ürüne Eklenecek Ortak BuildConfig Belirleme
String buildType = variant.buildType.name
String flavorName = variant.getFlavorName()
String buildTime = new Date().format("yyMMddHHmm", TimeZone.getTimeZone("Asia/Istanbul"))
variant.outputs.all {
buildConfigField 'String', 'INFO', "\"${flavorName}.${buildType}\""
buildConfigField 'String', 'appVer', "\"${versionName}_${buildTime}\""
outputFileName = getFileName(flavorName, versionName, buildTime, buildType)
}
// Ürüne Özel Tanımlanan BuildConfigFielde Göre İşlem, Üründe Şu Ayar Tanımlanmışsa.
/*variant.productFlavors.each { flavor ->
if (variant.getFlavorName() == projectName) {
flavor.buildConfigFields.each { key, value ->
if(key == "INFO") {
println value.type
println value.name
println value.value
}
}
}
}*/
// Release Sürüm İçin İşlemler -> Allatori is here.
if(projectBuildType.contains("release") && flavorName == projectName) {
//println(variant.name)
copy {
from "$projectDir/allatori.xml"
into "$buildDir/intermediates/classes/"
expand(classesRoot: variant.javaCompileProvider.get().destinationDir,
kotlinRoot: "${buildDir}/tmp/kotlin-classes/${variant.name}",
androidJar: "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar",
classpathJars: variant.javaCompileProvider.get().classpath.getAsPath(),
logFile: "allatori-log-${variant.name}.xml")
rename('allatori.xml', "allatori-${variant.name}.xml")
}
new File("${variant.javaCompileProvider.get().destinationDir}-obfuscated").deleteDir()
javaexec {
main = 'com.allatori.Obfuscate'
classpath = files("$rootDir/allatori/allatori.jar")
args "$buildDir/intermediates/classes/allatori-${variant.name}.xml"
}
new File("${variant.javaCompileProvider.get().destinationDir}").deleteDir()
new File("${variant.javaCompileProvider.get().destinationDir}-obfuscated").renameTo(new File("${variant.javaCompileProvider.get().destinationDir}"))
}
}
}
}
preBuild.dependsOn buildVariantTasks
static String getFileName(flavorName, versionName, buildTime, buildType) {
return flavorName + "_" + versionName + "_" + buildTime + "_" + buildType + ".apk"
}
// Bağımlılıklar için versiyon tanımlama örneği
ext {
appCombat = '1.4.1'
material = '1.5.0'
constrain = '2.1.3'
junit = '4.13.2'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:' + appCombat
implementation 'com.google.android.material:material:'+ material
implementation 'androidx.constraintlayout:constraintlayout:'+constrain
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:'+junit
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment