无法使用react-native-navigation构建react-native

时间:2019-10-19 04:44:02

标签: android react-native gradle

无法使用react-native-navigation构建新的react-native项目, 我遇到错误:

> Configure project :react-native-navigation
downloadRobolectricDependencies into E:\mobileapps\reactnative\wingley\android\build\robolectric-3.5.1-dependencies

> Task :expo-constants:compileDebugJavaWithJavac FAILED
E:\mobileapps\reactnative\wingley\node_modules\expo-constants\android\src\main\java\expo\modules\constants\ConstantsService.java:8: error: package android.support.annotation does not exist
import android.support.annotation.Nullable;
                                 ^
E:\mobileapps\reactnative\wingley\node_modules\expo-constants\android\src\main\java\expo\modules\constants\ConstantsService.java:56: error: cannot find symbol
  @Nullable
   ^
  symbol:   class Nullable
  location: class ConstantsService
Note: E:\mobileapps\reactnative\wingley\node_modules\expo-constants\android\src\main\java\expo\modules\constants\ConstantsService.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':expo-constants:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

如果有人遇到相同的错误,请提供帮助,我该怎么办。 我已经在线尝试了一些解决方案,但是仍然无法构建,这就是为什么我在下面提供所有项目详细信息的原因。 也许我做的事情不对。

项目详细信息:

android studio: 3.5.1
android SDK Platform-tools 29.0.4
node version: v10.16.3
JAVA_HOME: C:\Program Files\Java\jdk1.8.0_231
java version: 1.8.0_231
git version: 2.23.0.windows.1

package.json:

    "axios": "^0.19.0",
    "expo": "^35.0.0",
    "react": "16.8.3",
    "react-dom": "16.8.3",
    "react-native": "0.59.10",
    "react-native-gesture-handler": "~1.3.0",
    "react-native-navigation": "^2.27.9",
    "react-native-reanimated": "~1.2.0",
    "react-native-screens": "1.0.0-alpha.23",
    "react-native-unimodules": "~0.5.4",
    "react-native-web": "^0.11.7",
    "react-redux": "^7.1.1",
    "redux": "^4.0.4",
    "redux-promise": "^0.6.0"
  },
  "devDependencies": {
    "babel-preset-expo": "^7.0.0"
  },

settings.gradle:

apply from: '../node_modules/react-native-unimodules/gradle.groovy'
include ':react-native-screens'
project(':react-native-screens').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-screens/android')
include ':react-native-reanimated'
project(':react-native-reanimated').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-reanimated/android')
include ':react-native-gesture-handler'
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')
include ':react-native-navigation'
project(':react-native-navigation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-navigation/lib/android/app/')
includeUnimodulesProjects()

rootProject.name = 'projectname'

include ':app'

gradle.properties:

android.useAndroidX=true
android.enableJetifier=true

build.gradle:

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 19
        compileSdkVersion = 28
        targetSdkVersion = 27
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        mavenLocal()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
        maven { url 'https://jitpack.io' }
    }
}


task wrapper(type: Wrapper) {
    gradleVersion = '4.7'
    distributionUrl = distributionUrl.replace("bin", "all")
}

subprojects { subproject ->
    afterEvaluate {
        if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
            android {
                variantFilter { variant ->
                    def names = variant.flavors*.name
                    if (names.contains("reactNative51") || names.contains("reactNative55") || names.contains("reactNative56") || names.contains("reactNative57") ) {
                        setIgnore(true)
                    }
                }
            }
        }
    }
}

gradle / wrapper / gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

app / build.gradle

apply plugin: "com.android.application"

import com.android.build.OutputFile


project.ext.react = [
    entryFile: "index.js"
]

apply from: '../../node_modules/react-native-unimodules/gradle.groovy'
apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "com.projectname"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        missingDimensionStrategy "RNN.reactNativeVersion", "reactNative57_5" // See note below!
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support' && requested.name != 'multidex') {
            details.useVersion "${rootProject.ext.supportLibVersion}"
        }
    }
}

dependencies {
    implementation project(':react-native-screens')
    implementation project(':react-native-reanimated')
    implementation project(':react-native-gesture-handler')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:design:${rootProject.ext.supportLibVersion}"
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.android.support:support-v4:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation project(':react-native-navigation')
    addUnimodulesDependencies()
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

app / src / main / java / com / projectname / MainActivity.java

package com.projectname;

import com.reactnativenavigation.NavigationActivity;


public class MainActivity extends NavigationActivity {

}

app / src / main / java / com / projectname / MainApplication.java

package com.projectname;

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.NavigationReactNativeHost;
import com.reactnativenavigation.react.ReactGateway;

import com.wingleysetiajaya.generated.BasePackageList;
import com.swmansion.reanimated.ReanimatedPackage;
import com.swmansion.rnscreens.RNScreensPackage;
import com.swmansion.gesturehandler.react.RNGestureHandlerPackage;

import org.unimodules.adapters.react.ReactAdapterPackage;
import org.unimodules.adapters.react.ModuleRegistryAdapter;
import org.unimodules.adapters.react.ReactModuleRegistryProvider;
import org.unimodules.core.interfaces.Package;
import org.unimodules.core.interfaces.SingletonModule;
import expo.modules.constants.ConstantsPackage;
import expo.modules.permissions.PermissionsPackage;
import expo.modules.filesystem.FileSystemPackage;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends NavigationApplication {
  @Override
    protected ReactGateway createReactGateway() {
        ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
            @Override
            protected String getJSMainModuleName() {
                return "index";
            }
        };
        return new ReactGateway(this, isDebug(), host);
    }

    @Override
    public boolean isDebug() {
        return BuildConfig.DEBUG;
    }

    protected List<ReactPackage> getPackages() {
       // Add additional packages you require here
       // No need to add RnnPackage and MainReactPackage
       return Arrays.<ReactPackage>asList(
           // eg. new VectorIconsPackage()
       );
  }

    @Override
  public List<ReactPackage> createAdditionalReactPackages() {
    return getPackages();
  }

}

index.js

import { AppRegistry } from 'react-native';
import App from './App';

AppRegistry.registerComponent('projectname', () => App);

3 个答案:

答案 0 :(得分:0)

您确定您的React Native项目是最新的吗?您的React本机版本不是最新的。尝试更新您的react native cli,然后创建一个新的应用程序,然后处理源代码。我不认为Jettifier的版本低于<0.60。

答案 1 :(得分:0)

在app / build.gradle上添加此行

dependencies {
    ...
    implementation 'com.android.support:support-annotations:+'
}

答案 2 :(得分:0)

感谢您的所有答复,在寻找答案之后,现在我可以构建项目了。

我得到的解决方案是:

我需要使用react-native cli进行安装,所以我获得了最新的react-native = 0.61.2,

然后我需要安装RNN v3.0.0-alpha2。

在buildscript中添加supportLibVersion =“ 28.0.0”并在build.gradle中的存储库中添加mavenCentral()

app / build.gradle 在依赖项中添加实施项目(':react-native-navigation')

在defaultconfig中添加missingDimensionStrategy“ RNN.reactNativeVersion”,“ reactNative60”

其余文件我都遵循RNN教程。

仅次于index.js,我只输入:import'./App';

谢谢