美文网首页
react-native 更改android包名

react-native 更改android包名

作者: xiaotimel | 来源:发表于2020-03-28 17:44 被阅读0次

    把原生项目改造成react-native项目
    1、直接创建一个空白的的react-native
    2、把老的原生项目直接拷贝到相应的文件夹下
    3、更改settings.gradle文件

    apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
    

    直接拷贝到setting.gradle文件
    4、在app中添加react native 的依赖

    api ("com.facebook.react:react-native:0.60.5"){
            exclude group: 'com.android.support'
        }
    

    自行选择版本
    5、在app的build.gradle添加

    project.ext.react = [
            entryFile   : "index.tsx",
            enableHermes: false,
    ]
    apply from: "../../node_modules/react-native/react.gradle"
    
    
    def enableProguardInReleaseBuilds = false
    def jscFlavor = 'org.webkit:android-jsc:+'
    def enableHermes = project.ext.react.get("enableHermes", false);
    
    
    android {
        compileSdkVersion rootProject.ext.android.compileSdkVersion
        buildToolsVersion rootProject.ext.android.buildToolsVersion
    
        defaultConfig {
            applicationId "com.you.react.native"
            minSdkVersion rootProject.ext.android.minSdkVersion
            targetSdkVersion rootProject.ext.android.targetSdkVersion
            versionCode rootProject.ext.android.versionCode
            versionName rootProject.ext.android.versionName
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
            javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
    
            ndk {
                //设置支持的SO库架构
                abiFilters "armeabi-v7a"
            }
        }
    
        dexOptions {
            jumboMode = true
            javaMaxHeapSize "4g"
        }
    
        dataBinding {
            enabled = true
        }
        kapt {
            generateStubs = true
        }
        signingConfigs {
    
            config {
                storeFile file('../your.jks')
                storePassword "pwd"
                keyAlias "signed_key"
                keyPassword "pwd"
                v1SigningEnabled true
                v2SigningEnabled true
            }
        }
    
        buildTypes {
            release {
                minifyEnabled true
                shrinkResources true
                signingConfig signingConfigs.config
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
    
            debug {
                minifyEnabled false
                shrinkResources false
                signingConfig signingConfigs.config
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        lintOptions {
            disable 'MissingTranslation'
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    def releaseTime() {
        return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
    }
    dependencies {
        api fileTree(include: ['*.jar'], dir: 'libs')
        testImplementation 'junit:junit:4.12'
        androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        if (enableHermes) {
            def hermesPath = "../../node_modules/hermesvm/android/";
            debugImplementation files(hermesPath + "hermes-debug.aar")
            releaseImplementation files(hermesPath + "hermes-release.aar")
        } else {
            implementation jscFlavor
        }
    }
    
    apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
    
    // 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'
    }
    repositories {
        mavenCentral()
    }
    
    

    6、新建BUCK文件

    # To learn about Buck see [Docs](https://buckbuild.com/).
    # To run your application with Buck:
    # - install Buck
    # - `npm start` - to start the packager
    # - `cd android`
    # - `keytool -genkey -v -keystore keystores/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"`
    # - `./gradlew :app:copyDownloadableDepsToLibs` - make all Gradle compile dependencies available to Buck
    # - `buck install -r android/app` - compile, install and run application
    #
    
    load(":build_defs.bzl", "create_aar_targets", "create_jar_targets")
    
    lib_deps = []
    
    create_aar_targets(glob(["libs/*.aar"]))
    
    create_jar_targets(glob(["libs/*.jar"]))
    
    android_library(
        name = "all-libs",
        exported_deps = lib_deps,
    )
    
    android_library(
        name = "app-code",
        srcs = glob([
            "src/main/java/**/*.java",
        ]),
        deps = [
            ":all-libs",
            ":build_config",
            ":res",
        ],
    )
    
    android_build_config(
        name = "build_config",
        package = "对应你的包名",
    )
    
    android_resource(
        name = "res",
        package = "对应你的包名",
        res = "src/main/res",
    )
    
    android_binary(
        name = "app",
        keystore = "//android/keystores:debug",
        manifest = "src/main/AndroidManifest.xml",
        package_type = "debug",
        deps = [
            ":app-code",
        ],
    )
    
    

    原生项目改造成react native项目

    相关文章

      网友评论

          本文标题:react-native 更改android包名

          本文链接:https://www.haomeiwen.com/subject/iejsuhtx.html