关于android:gradlew更改React Native构建的jcenter()URL

gradlew change jcenter() URL for React Native build

我试图为Android构建React Native应用程序,但是在我的企业中,外部存储库被阻止(不允许外部网络)。

因此,我们使用内部Maven存储库来获取工件。
但是我在构建React Native应用程序时遇到了一些问题,因为具有节点模块依赖项的项目具有要引用jcenter()存储库的本机Android代码(我不想覆盖node_modules / react * / android / build.gradle手动)。

因此有一种方法可以用gradle覆盖jcenter URL?

我已经在我的用户主目录中尝试过init脚本init.gradle(从doc中得到启发):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
apply plugin:EnterpriseRepositoryPlugin

class EnterpriseRepositoryPlugin implements Plugin<Gradle> {

    private static String ENTERPRISE_REPOSITORY_URL ="https://my.enterprise.repo"

    void apply(Gradle gradle) {
        // ONLY USE ENTERPRISE REPO FOR DEPENDENCIES
        gradle.allprojects{ project ->
            project.repositories {

                // Remove all repositories not pointing to the enterprise repository url
                all { ArtifactRepository repo ->
                    project.logger.lifecycle"DEBUG : Repository ${repo.url}"
                    if (!(repo instanceof MavenArtifactRepository) ||
                          repo.url.toString() != ENTERPRISE_REPOSITORY_URL) {
                        project.logger.lifecycle"Repository ${repo.url} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed"
                        remove repo
                    }
                }

                // add the enterprise repository
                jcenter {
                    name"BintrayJCenter"
                    url ENTERPRISE_REPOSITORY_URL
                }
            }
        }
    }
}

但是当我开始gradlew时,我得到的输出是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'AppliTest'.
> Could not resolve all dependencies for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:2.2.3.
     Required by:
         :AppliLabChatbot:unspecified
      > Could not resolve com.android.tools.build:gradle:2.2.3.
         > Could not get resource 'https://repo1.maven.org/maven2/com/android/tools/build/gradle/2.2.3/gradle-2.2.3.pom'.
            > Could not HEAD 'https://repo1.maven.org/maven2/com/android/tools/build/gradle/2.2.3/gradle-2.2.3.pom'.
               > repo1.maven.org
      > Could not resolve com.android.tools.build:gradle:2.2.3.
         > Could not get resource 'https://jcenter.bintray.com/com/android/tools/build/gradle/2.2.3/gradle-2.2.3.pom'.
            > Could not HEAD 'https://jcenter.bintray.com/com/android/tools/build/gradle/2.2.3/gradle-2.2.3.pom'.
               > jcenter.bintray.com
   > Could not resolve de.undercouch:gradle-download-task:3.1.2.
     Required by:
         :AppliLabChatbot:unspecified
      > Could not resolve de.undercouch:gradle-download-task:3.1.2.
         > Could not get resource 'https://repo1.maven.org/maven2/de/undercouch/gradle-download-task/3.1.2/gradle-download-task-3.1.2.pom'.
            > Could not HEAD 'https://repo1.maven.org/maven2/de/undercouch/gradle-download-task/3.1.2/gradle-download-task-3.1.2.pom'.
               > repo1.maven.org
      > Could not resolve de.undercouch:gradle-download-task:3.1.2.
         > Could not get resource 'https://jcenter.bintray.com/de/undercouch/gradle-download-task/3.1.2/gradle-download-task-3.1.2.pom'.
            > Could not HEAD 'https://jcenter.bintray.com/de/undercouch/gradle-download-task/3.1.2/gradle-download-task-3.1.2.pom'.
               > jcenter.bintray.com

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

插件似乎没有拦截jcenter存储库...


在初始化脚本中,您正在项目的存储库上设置约束。但是,正如您在错误消息中看到的那样,Gradle仍在尝试从" https://repo1.maven.org / ..."而不是从您的企业仓库下载某些依赖项:我想这些是您所用插件的依赖项正在应用于您的构建脚本。
因此,除了项目的依赖项存储库之外,还必须配置项目的buildscript存储库。
以下代码应该可以工作(但是我无法测试):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
apply plugin: EnterpriseRepositoryPlugin

class EnterpriseRepositoryPlugin implements Plugin<Gradle> {

    private static String ENTERPRISE_REPOSITORY_URL ="https://repo.gradle.org/gradle/repo"

    def configRepositories = { ->
        // Remove all repositories not pointing to the enterprise repository url
        all { ArtifactRepository repo ->
            if (!(repo instanceof MavenArtifactRepository) ||
                    repo.url.toString() != ENTERPRISE_REPOSITORY_URL) {
                println"Repository ${repo.name} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed"
                remove repo
            }
        }
        // add the enterprise repository
        jcenter {
            name"BintrayJCenter"
            url ENTERPRISE_REPOSITORY_URL
        }
    }

    void apply(Gradle gradle) {    

        gradle.allprojects { project ->
            // ONLY USE ENTERPRISE REPO FOR DEPENDENCIES
            project.repositories(configRepositories)
            // Only use enterprise repo for plugins classpath as well.
            project.buildscript.repositories(configRepositories)
        }
    }

}