通过 gradle sourceSets gradle.properties实现通过配置字动态加载不同路径同名类

该文章展示了如何在Android项目中通过gradle.properties或config.properties配置文件动态切换sourceSets,根据BUILD_TYPE选择加载abc或efg目录下的代码,实现不同构建类型的功能差异。
摘要由CSDN通过智能技术生成

项目结构图如下

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'
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值