maoruibin / maoruibin/maoruibin.github.com

获取 AndroidStudio 项目中主 module 名称

Open
#54 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

utils
Dominant language
HTML
Stars
22
Forks
4
PR merge metrics
No merged PRs in 30d

Description

    /**
     * 获取根目录下的主 APP 目录
     * @param mRoot 项目根目录
     * @return 主 module 名称
     */
    public static String getMainModuleName(File mRoot) {
        if (mRoot == null) {
            return null;
        }
        File setting = new File(mRoot.getPath(), Constant.settingGradle);

        String content = loadContentByPath(setting.getPath());
        if (TextUtils.isEmpty(content)) {
            return null;
        }
        List<String> modules = fetchModules(content);

        for (String module : modules) {
            //获取对应 module  对应的 build.gradle 文件
            File moduleBuildGradle = new File(new File(mRoot.getPath(), module), "build.gradle");
            if (moduleBuildGradle.exists()) {
                if (isApplication(moduleBuildGradle)) {
                    return module;
                }
            }
        }
        return null;
    }

    /**
     * 获取 setting.gradle 文件中所有的 module
     * @param content setting.gradle 文件内容
     * @return module 集合
     */
    public static List<String> fetchModules(String content) {
        String regex = "':[a-zA-Z]{1,50}'"; //正则表达式
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(content);
        List<String> matchRegexList = new ArrayList<String>();
        while (m.find()) {
            String version = m.group();
            version = version.substring(version.indexOf(":") + 1, version.length() - 1);
            matchRegexList.add(version);
        }
        return matchRegexList;
    }

    /**
     * 根据文件路径获取文件内容
     * @param path 文件路径
     * @return 文件字符串
     */
    public static String loadContentByPath(String path) {
        InputStream is = null;
        StringBuffer buffer = new StringBuffer();
        try {
            is = new FileInputStream(path);
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = in.readLine()) != null) {
                buffer.append(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return buffer.toString();

    }

    /**
     * 根据 build.gradle 文件判断 module 是不是主 app 目录
     * @param gradleFile module 对应的 build.gradle 文件
     * @return 是否是主 module
     */
    public static boolean isApplication(File gradleFile) {
        InputStream is = null;
        try {
            is = new FileInputStream(gradleFile);
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line = "";
            while ((line = in.readLine()) != null) {
                if (line.contains("com.android.application")) {
                    return true;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;

    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided getMainModuleName entry point, then read fetchModules, loadContentByPath, and isApplication together. Check how setting.gradle and each module's build.gradle are identified, and verify that the result is the module using the com.android.application plugin, including the null cases shown in the snippet.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile-dev
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.