问题编译应用程序 - React Native

时间:2017-08-27 19:06:33

标签: java android react-native

我正在尝试编译我正在处理的react本机模块。我认为问题在于/android/src/main/java...而不是/android/TeamCode/src/main/java /android/TeamCode/src。如果是这种情况,我将如何制作它以便在/android/src而不是- module --- index.js --- package.json --- android //this is where the below screenshot is from ----- build.gradle ----- TeamCode ------- build.gradle //apply from: '../build.common.gradle' --- Example //this is the app I am trying to compile ----- index.android.js ----- android ----- package-lock.json ----- other stuff 中反映原生的内容?但是我不知道这是不是问题,我可能完全错了。

结构

以下是我的项目:

:app:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
/Users/zoe/saas/robotics/ftcnative/Example/android/app/src/main/java/com/example/MainApplication.java:6: error: package com.qualcomm.ftcrobotcontroller does not exist
import com.qualcomm.ftcrobotcontroller.MyPackage;
                                      ^
/Users/zoe/saas/robotics/ftcnative/Example/android/app/src/main/java/com/example/MainApplication.java:27: error: cannot find symbol
            new MyPackage()
                ^
  symbol: class MyPackage
2 errors
:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

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

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

BUILD FAILED

错误

{
    "name": "Example",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "start": "node node_modules/react-native/local-cli/cli.js start",
        "test": "jest"
    },
    "dependencies": {
        "react": "16.0.0-alpha.12",
        "react-native": "0.47.2",
        "ftcnative": "file:../"
    },
    "devDependencies": {
        "babel-jest": "20.0.3",
        "babel-preset-react-native": "2.0.1",
        "jest": "20.0.4",
        "react-test-renderer": "16.0.0-alpha.12"
    },
    "jest": {
        "preset": "react-native"
    }
}

截图

enter image description here

模块/示例/的package.json

import {NativeModules} from 'react-native';
module.exports = NativeModules.MyModule;

模块/ index.js

import java.util.regex.Pattern

apply plugin: 'com.android.application'

android {

    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    signingConfigs {
        debug {
            keyAlias 'androiddebugkey'
            keyPassword 'android'
            storeFile rootProject.file('libs/ftc.debug.keystore')
            storePassword 'android'
        }
    }

    defaultConfig {
        applicationId 'com.qualcomm.ftcrobotcontroller'
        minSdkVersion 19
        targetSdkVersion 19

        /**
         * We keep the versionCode and versionName of robot controller applications in sync with
         * the master information published in the AndroidManifest.xml file of the FtcRobotController
         * module. This helps avoid confusion that might arise from having multiple versions of
         * a robot controller app simultaneously installed on a robot controller device.
         *
         * We accomplish this with the help of a funky little Groovy script that maintains that
         * correspondence automatically.
         *
         * @see <a href="http://developer.android.com/tools/building/configuring-gradle.html">Configure Your Build</a>
         * @see <a href="http://developer.android.com/tools/publishing/versioning.html">Versioning Your App</a>
         */
        def manifestFile = project(':FtcRobotController').file('src/main/AndroidManifest.xml');
        def manifestText = manifestFile.getText()
        //
        def vCodePattern = Pattern.compile("versionCode=\"(\\d+(\\.\\d+)*)\"")
        def matcher = vCodePattern.matcher(manifestText)
        matcher.find()
        def vCode = Integer.parseInt(matcher.group(1))
        //
        def vNamePattern = Pattern.compile("versionName=\"(.*)\"")
        matcher = vNamePattern.matcher(manifestText);
        matcher.find()
        def vName = matcher.group(1)
        //
        versionCode vCode
        versionName vName
    }

    // Advanced user code might just want to use Vuforia directly, so we set up the libs as needed
    buildTypes {
        release {
            // Disable debugging for release versions so it can be uploaded to Google Play.
            //debuggable true
            ndk {
                abiFilters "armeabi-v7a"
            }
        }
        debug {
            debuggable true
            ndk {
                abiFilters "armeabi-v7a"
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets.main {
        jni.srcDirs = []
        jniLibs.srcDir rootProject.file('libs')
    }
}

repositories {
    flatDir {
        dirs rootProject.file('libs')
    }
}

apply from: 'build.release.gradle'

机器人/ build.common.gradle

/**
 * Top-level build file for ftc_app project.
 *
 * It is extraordinarily rare that you will ever need to edit this file.
 */

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    lintOptions {
        abortOnError false
    }
    sourceSets {
        main {
            manifest.srcFile 'TeamCode/src/main/AndroidManifest.xml'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.facebook.react:react-native:+'
}

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

机器人/的build.gradle

java.srcDirs += 'android/TeamCode/src/main/java'

编辑:

  • 当我添加java.srcDirs += 'android/TeamCode/src/main/java'时,我得到:

    失败:构建因异常而失败。

    • 出了什么问题: 任务执行失败':ftcnative:compileReleaseJavaWithJavac'。
        

      无法找到源java类:'/ Users / zoe / saas /robotics / ftcnative / android / TeamCode / src / main / java / orgirstinspires / ftc / teamcode / MyModule.java',因为它不属于任何源目录:'/ / Users / zoe / saas / robotics / ftcnative / Example / node_modules / ftcnative / android / src / main / java,/ Users / zoe / saas / robotics / ftcnative / Example / node_modules / ftcnative / android / TeamCode / src / main / java,/ Users / zoe / saas / robotics / ftcnative / Example / node_modules / ftcnative / android / src / release / java,/ Users / zoe / saas / robotics / ftcnative / Example / node_modules / ftcnative / android / build / generated / source / r / release,/ Users / zoe / saas / robotics / ftcnative / Example / node_modules / ftcnative / android / build / generated / source / buildConfig / release,/ Users / zoe / saas / robotics / ftcnative / example / node_modules / ftcnative / android / build / generated / source / aidl / release,/ Users / zoe / saas / robotics / ftcnative / Example / node_modules / ftcnative / android / build / generated / source / rs / release ]'

  • 当我添加wherePivot()时,我仍然收到第一个错误。

2 个答案:

答案 0 :(得分:0)

我不知道React Native,但在Android中,要做到这一点,你必须更改build.gradle。找到android部分,然后添加sourceSets部分,如下所示:

android {
    //...
    sourceSets {
        main {
            // Like this...
            java.srcDirs += 'TeamCode/src/main/java'
            // ...or maybe, like that...
            java.srcDirs += 'android/TeamCode/src/main/java'
            // Dirty, extreme alternative...
            java.srcDirs += '/Users/zoe/saas/robotics/ftcnative/android/TeamCode/src/main/java'
        }
    }
}

请注意,这是添加其他源目录(因为+=)。

答案 1 :(得分:0)

更改一个Java方法之一的名称后,我遇到了同样的问题。

我通过以下方式解决了该问题:

  

删除我的android build文件夹,然后再次运行react-native run-android