项目结构图如下
gradle.properties
#1使用abc,2使用efg
BUILD_TYPE=2
build.gradle关键代码:
sourceSets {
def type = BUILD_TYPE as int
println "sourceSets BUILD_TYPE ===> " + type + " " + (type == 1)
main{
if(1 === type){
java.srcDirs = ["src/main/java",'src/abc/java']
println "sourceSets BUILD_TYPE === abc"
}else {
java.srcDirs = ["src/main/java",'src/efg/java']
println "sourceSets BUILD_TYPE === efg"
}
}
}
abc目录下的包名类名同名类Tool
package com.sample.tools;
public class Tool {
public static String getStr(){
return "src/abc/java";
}
}
efg目录下的包名类名同名类Tool
package com.sample.tools;
public class Tool {
public static String getStr(){
return "src/efg/java";
}
}
Activity中的调用代码:
tv = findViewById(R.id.tv);
tv.setText(Tool.getStr());
运行效果如下所示:
也可以通过自定义配置文件方式来做:
先在build.gradle同级目录下新建config.properties文件
内容如下:
#使用abc或者efg
BUILD_TYPE=abc
build.gradle关键代码:
sourceSets {
Properties properties = new Properties()
def file = file('config.properties')
def build_type = "abc"
if (file.canRead()) {
properties.load(new FileInputStream(file))
build_type = properties.get("BUILD_TYPE")
println "file.canRead build_type = $build_type"
}
println "result build_type = $build_type"
main{
java.srcDirs = ["src/main/java","src/$build_type/java"]
}
}
全量代码如下:
MainActivity
package com.sample.sourcesetssample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.sample.tools.Tool;
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
tv.setText(Tool.getStr());
}
}
build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.sample.sourcesetssample"
minSdk 25
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets {
def type = BUILD_TYPE as int
println "sourceSets BUILD_TYPE ===> " + type + " " + (type == 1)
main{
if(1 === type){
java.srcDirs = ["src/main/java",'src/abc/java']
println "sourceSets BUILD_TYPE === abc"
}else {
java.srcDirs = ["src/main/java",'src/efg/java']
println "sourceSets BUILD_TYPE === efg"
}
}
// Properties properties = new Properties()
// def file = file('config.properties')
// def build_type = "abc"
// if (file.canRead()) {
// properties.load(new FileInputStream(file))
// build_type = properties.get("BUILD_TYPE")
// println "file.canRead build_type = $build_type"
// }
// println "result build_type = $build_type"
//
// main{
// java.srcDirs = ["src/main/java","src/$build_type/java"]
// }
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}