在react-native和native-android之间设置桥梁之后,apk不起作用。但是虚拟完成了

时间:2019-04-19 02:58:11

标签: react-native react-native-android

我应该从native-android调用function到react-native,所以我设置了bridge。在设置网桥之前,apk运行和虚拟运行均运行良好。

但是设置后,垂直运行也可以,但是apk运行不起作用。

我找不到原因。

KeyStoreApi.java

package com.tovidapp;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.IllegalViewOperationException;

public class KeyStoreApi extends ReactContextBaseJavaModule {
    public KeyStoreApi(ReactApplicationContext reactContext){
        super(reactContext);
    }

    @Override
    public String getName(){
        return "KeyStoreApi";
    }

    @ReactMethod
    public void sayHi(Callback errCallBack, Callback successCallBack){
        try{
            System.out.println("Hello, i am from java");
            successCallBack.invoke("Callback: Greeting from java");;
        }catch (IllegalViewOperationException e){
            errCallBack.invoke(e.getMessage());
        }
    }
}

KeyStoreApiPackage.java

package com.tovidapp;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class KeyStoreApiPackage implements ReactPackage {

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext){
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext){
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new KeyStoreApi(reactContext));
        return modules;
    }
}

build.grade

apply plugin: "com.android.application"

import com.android.build.OutputFile

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

apply from: "../../node_modules/react-native/react.gradle"


def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.tovidapp"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    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"
            signingConfig signingConfigs.release
        }
    }
    // 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
            }
        }
    }
}

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


// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

call.js

import React, {Component} from 'react';
import {View, Text, NativeModules, TouchableOpacity,} from 'react-native';


const keyStoreApi = NativeModules.KeyStoreApi;


class Account extends Component {

    async sayHiFromJava() {
        keyStoreApi.sayHi(err => {
            alert(err);
        }, msg => {
            alert(msg);
        })
    }

    render() {
        return (
            <View>
                <TouchableOpacity onPress={this.sayHiFromJava}>
                    <Text>TEST</Text>
                </TouchableOpacity>
            </View>
        );
    }
}

export default Account;

package.json中的脚本

"android-apk": "react-native run-android --variant=release",
"android-virtual": "react-native run-android",
"android-build": "rm android/app/build/outputs/apk/release/app-release.apk & cd android && ./gradlew assembleRelease",

结果图片

  • 在apk

a busy cat

-在简单的虚拟机中

a busy cat

0 个答案:

没有答案