Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

image image image
// plugins/with-jpush.ts
import {
    withAndroidManifest,
    withProjectBuildGradle,
    withAppBuildGradle,
    AndroidConfig,
    ConfigPlugin,
} from '@expo/config-plugins';

type Options = { appKey: string; channel?: string };

const withJPush: ConfigPlugin<Options> = (config, { appKey, channel = 'developer-default' }) => {
    // 1) AndroidManifest 注入 <meta-data>(不依赖占位符,最稳)
    config = withAndroidManifest(config, (c) => {
        const app = AndroidConfig.Manifest.getMainApplicationOrThrow(c.modResults);
        AndroidConfig.Manifest.addMetaDataItemToMainApplication(app, 'JPUSH_APPKEY', appKey);
        AndroidConfig.Manifest.addMetaDataItemToMainApplication(app, 'JPUSH_CHANNEL', channel);
        return c;
    });

    // 2) project 级 build.gradle 添加极光仓库(如文档需要)
    config = withProjectBuildGradle(config, (c) => {
        const gradle = c.modResults.contents;
        if (!gradle.includes('https://sdkrepo.jiguang.cn/public/')) {
            c.modResults.contents = gradle.replace(
                /mavenCentral\(\)\n\s*\}/m,
                `mavenCentral()\n        maven { url "https://sdkrepo.jiguang.cn/public/" }\n    }`
            );
        }
        return c;
    });

    // 3) app 模块的依赖(若库**没有**自动链接时才需要)
    config = withAppBuildGradle(config, (c) => {
        let contents = c.modResults.contents;
        if (!contents.includes(`implementation project(':jpush-react-native')`)) {
            contents = contents.replace(
                /dependencies\s*\{/,
                `dependencies {\n    implementation project(':jpush-react-native')\n    implementation project(':jcore-react-native')`
            );
        }
        c.modResults.contents = contents;
        return c;
    });

    return config;
};

export default withJPush;

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.