【Uniapp】Uniapp 使用 Android Studio 实现离线打包

1、方式一:使用 Uniapp 默认示例(不推荐)

Uniapp 的官方文档有点简洁,对于没有接触过安卓的开发者来说,无疑是晴天霹雳,由于公司的App项目需要对接硬件,所以打包方式被迫变为了Android Studio 离线打包 Uniapp,这里我详细记录一下打包过程,方便以后使用。官方提供的App离线打包可以作为参考

(1)相关环境准备

(2)导入打包项目

下载的App离线SDK下载提供了一个示例项目
在这里插入图片描述
导入示例项目,本文于2021年7月17日12:08:25编写,下载的都是最新的软件,编译项目时遇到一个问题,使用Android API 31的版本,总是报错
在这里插入图片描述
我直接把31改成了29就OK了,应该是官方的一个Bug
在这里插入图片描述
刷新一下配置,导入OK
在这里插入图片描述

(3)打包Uniapp为App资源

uniapp打包自己的项目为App资源:HBuilder X > 发行 > 原生App-本地打包 > 生成本地打包App资源
在这里插入图片描述

(4)配置详细过程

将uniapp打包后的静态资源复制到这里替换示例项目的
在这里插入图片描述
修改dcloud_control.xml配置的appid
在这里插入图片描述
然后详细阅读文章

得到了自己的mykey.keystore文件,并且拿到了SHA1秘钥,后面要使用到

登录 DCloud开发者中心,找到自己创建的Uniapp项目的appID,当然,通常情况下,公司开发的项目基本上都是管理人创建的,这里肯定不会有当前要打包的项目,所有直接新建一个应用,然后复制appID,修改自己项目里的appID即可(提示,manifest.json打开源码视图修改即可)

在这里插入图片描述
点击进入需要打包的项目,进行配置之后,拿到appKey
在这里插入图片描述
将生成的AppKey配置到项目的AndroidManifest.xml中
在这里插入图片描述
然后构建打包App
在这里插入图片描述
构建完成后,直接点击local就会自动打开构建后的安装包

在这里插入图片描述
中途遇到饶人的问题为打包之后,提示“appKey未定义或配置错误”,详细阅读我的文章,并仔细对比,你就会发现这个问题解决啦。到此,构建完成啦!

2、方式二:自定义 Android 项目(推荐)

(1)创建空项目

在这里插入图片描述
在这里插入图片描述

(2)修改 build.gradle

ext {
    
    
//    sourceCompatibility = sourceCompatibility
//    targetCompatibility = targetCompatibility
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    
    
    repositories {
    
    
        jcenter()
        maven {
    
    
            url "https://maven.aliyun.com/repository/google"
        }
        maven {
    
    
            url "http://maven.aliyun.com/nexus/content/repositories/releases"
        }
        maven {
    
     url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven {
    
     url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven {
    
    
            url 'https://jitpack.io'
        }
    }
    dependencies {
    
    
        classpath 'com.android.tools.build:gradle:4.2.2'

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

allprojects {
    
    
    repositories {
    
    
        jcenter()
        maven {
    
    
            url "https://maven.aliyun.com/repository/google"
        }
        maven {
    
    
            url "http://maven.aliyun.com/nexus/content/repositories/releases"
        }
        maven {
    
     url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven {
    
     url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven {
    
    
            url 'https://jitpack.io'
        }
    }
}

task clean(type: Delete) {
    
    
    delete rootProject.buildDir
}

(3)修改gradle.properties

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
android.enableJetifier=true

(4)修改app/build.gradle

plugins {
    
    
    id 'com.android.application'
}

android {
    
    
    compileSdkVersion 30
    buildToolsVersion '30.0.2'

    defaultConfig {
    
    
        applicationId "com.zhhy.spotorder.app.cashier"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
    
    
        config {
    
    
            keyAlias 'test'
            keyPassword '111111'
            storeFile file('mykey.keystore')
            storePassword '111111'
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }
    buildTypes {
    
    
        debug {
    
    
            signingConfig signingConfigs.config
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
    
    
            signingConfig signingConfigs.config
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    //使用uniapp时,需复制下面代码
    aaptOptions {
    
    
        additionalParameters '--auto-add-overlay'
        //noCompress 'foo', 'bar'
        ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
    }
    compileOptions {
    
    
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}

dependencies {
    
    

    implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.2.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'


    implementation group: 'androidx.recyclerview', name: 'recyclerview', version: '1.2.0'
    implementation 'com.facebook.fresco:fresco:1.13.0'
    implementation "com.facebook.fresco:animated-gif:1.13.0"
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'com.alibaba:fastjson:1.1.46.android'
    implementation 'com.gitee.Agx58694:sunmi-printing:1.0.9'
}

(5)准备证书文件 mykey.keystore

(6)修改app/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.zhhy.spotorder.app.cashier">

    <application
        android:name=".MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/icon_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Spotorderappcashierandroid"
        tools:replace="android:name">

        <activity
            android:name="io.dcloud.PandoraEntry"
            android:configChanges="orientation|keyboardHidden|keyboard|navigation"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:hardwareAccelerated="true"
            android:theme="@style/TranslucentTheme"
            android:screenOrientation="user"
            android:windowSoftInputMode="adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="io.dcloud.PandoraEntryActivity"
            android:launchMode="singleTask"
            android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard"
            android:hardwareAccelerated="true"
            android:permission="com.miui.securitycenter.permission.AppPermissionsEditor"
            android:screenOrientation="user"
            android:theme="@style/DCloudTheme"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="h56131bcf" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="dcloud_appkey"
            android:value="你的key" />

    </application>
</manifest>

(7)修改 dcloud_control.xml

<hbuilder>
<apps>
    <app appid="你的appId" appver=""/>
</apps>
</hbuilder>

(8)复制打包资源

使用 HbuilderX 生成 App 打包资源
在这里插入图片描述
复制到 main/assets/apps 下面
在这里插入图片描述

(9)连接测试

先用USB连接的你手机,或者需要调试的终端,直接运行看效果,通常情况下都没有问题,最常见的问就是appKey 未配置或配置错误,那么请参照第一节:配置详细过程仔细检查你的配置,那就OK了

微信公众号

每天Get一个小技巧

Guess you like

Origin blog.csdn.net/qq_38762237/article/details/118854555