Android本地打包指南Android studio 离线打包uniapp

作者 : 小优 本文共6292个字,预计阅读时间需要16分钟 发布时间: 2021-10-21 共2.17K人阅读

  由于项目需求,使用了hbuilderx 他们家的uniapp框架,这个框架是基于微信小程序和vue衍生出来的,只要你会微信小程序和vue基本上都是很容易上手,关于他们的api,可以自行去官网查看,这里只是为了记录如何降低打包出来的apk的大小

你会发现,直接使用hbuilderx提供的sdk文件下的Hbuilder-Hello文件夹进行打包,如果你是直接用使用的hbuilderx uniapp的模板,打包出来的apk大约在50M左右,因为hbuilder-hello下几乎把所有的第三方的包给你放进去了,在libs里面,所以导致你实际开发的时候,包会越来越大,如何减低apk的大小呢

第一步,打开Android studio ,点击File - New - New Project 来到如下界面

1156842-20200702094415110-880852601

选择 No Activity

1156842-20200702094451520-451423412

不选择 Empty  Activity 的原因是打包之后你会有两个包一个是hello Android 一个是你真正的包,所以这里推荐选择 No Activity

第二步,点击next

1156842-20200702094836242-743589468

注意:离线打包不支持kotlin 所以要选择java,其余的应用名称,包名,可以自己定

然后点击finish,你会来到如下界面

1156842-20200702095018725-96713528

为了统一官网配置,这里点击project,你会看到你的目录结构和Hbuilder-Hello的差不多

第三步开始配置

将你下载的离线打包的sdk中的HBuilder-Helloapplibs下的

android-gif-drawable-release@1.2.17.aar

lib.5plus.base-release.aar

miit_mdid_1.0.10.aar

uniapp-release.aar

,这四个文件放到你新建的Android项目下的libs下面

1156842-20200702095419993-250214026

注意对比目录结构

接着,在app-src-main下新建assets文件夹,将HBuilder-Hello/app/src/main/assets文件夹下的data文件夹整个copy到新建的assets文件夹下

1156842-20200702095710769-943174027

注意:不要管data文件夹下有什么不同,因为可能最新的sdk删除了上图中的两个dat文件,你不用管,只用直接复制就好了

第四步 对项目进行打包资源

使用hbuilderx本地打包app资源,或者使用vscode直接yarn run build:app-plus进行打包资源,通过vscode打包的需要你自己改两个名字,一个是将dist下的build改成的应用id,将app-plus改成www,如果是hbuilderx工具生成的app资源是无需做修改的,等到的目录结构如下

1156842-20200702100419063-604177176

然后在 上面新建的assets下新建apps文件夹,将上面的包放在assets/apps文件夹下

1156842-20200702100735415-1955926605

开始配置

要确保这三处的id一致

1156842-20200702100919398-1768437195

1156842-20200702100954302-531934100

然后打开app/build.gradle文件

1156842-20200702101112331-1787691711

在dependencies下新增

implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.alibaba:fastjson:1.1.46.android'
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 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

在 Android{}添加

aaptOptions {

    additionalParameters '--auto-add-overlay'

    ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"

}

然后修改对应的版本,完整文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.2"
    aaptOptions.cruncherEnabled = false
    aaptOptions.useNewCruncher = false
    defaultConfig {
        applicationId "com.manager.xxx"
        minSdkVersion 25
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        config {
            keyAlias 'apk.keystore'
            keyPassword '9999'
            storeFile file('apk.keystore')
            storePassword '9999'
            v1SigningEnabled true //兼容v1
            v2SigningEnabled true //兼容v2
        }
    }


    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'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.alibaba:fastjson:1.1.46.android'
    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 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

修改之后记得 Sync now

1156842-20200702101405025-1840951381

然后在app→src→main下配置AndroidManifest.xml文件

1156842-20200702102758496-1061399011

修改为

1156842-20200702102837128-1864035274

将application节点里的内容替换为

<!--  添加内容-->
<activity
    android:name="io.dcloud.PandoraEntry"
    android:configChanges="orientation|keyboardHidden|keyboard|navigation"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:hardwareAccelerated="true"
    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: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="替换为自己申请的Appkey" />

可以去官网直接复制,以下是完整代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manager.xxx">

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:supportsRtl="true">

        <!--  添加内容-->
        <activity
            android:name="io.dcloud.PandoraEntry"
            android:configChanges="orientation|keyboardHidden|keyboard|navigation"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:hardwareAccelerated="true"
            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: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="替换为自己申请的Appkey" />

    </application>>
</manifest>

然后点击右上角的运行按钮运行,就可以快了的玩耍了

1156842-20200702103145442-1949414183

然后点击 build APK

1156842-20200702103205332-803373179

打包成功之后,你会发现你的包小了很多

常见问题

Q: 安卓打包apk出错AAPT: error: failed to read PNG signature: file does not start with PNG signature.

A:  在app/build.gradle文件中加以下代码后,重构项目

aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false

 

Q: 未配置appkey或配置错误!怎么解决

A: 官方文档少说了一步,研究好一会,巨坑爹。 要把之前生成的keystore文件放在app目录下

然后在build.gradle(app)加这个(和defaultConfig同级):

资源下载说明

请使用百度网盘下载资源,请不要在线解压!

1.人民币与U币汇率为1比10,即1元=10U币.有任何疑问请联系客服处理!
2.请记住本站永久网址发布页:http://uuscw.com
3.本站资源大多存储在云盘,如发现链接失效请在下方评论留言,作者看到后会第一时间更新补链。
4.关于解压密码错误或者资源损坏,请查看解压教程:[必看]关于资源教程及常见问题
5.本站大部分资源解压密码均为: uuscw.com 如密码有误,请查看原帖的资源说明!

免责申明:
1.如果服务器暂不能下载请稍后重试!总是不能下载,请联系客服,谢谢合作!
2.本站所有资源(包括写真、模板、素材、软件、字体等)仅供学习与参考,请勿用于商业用途。
3.本站所有素材来源于用户上传和网络,如有侵权请请联系客服处理!
4.本站资源售价只是赞助,收取费用仅维持本站的日常运营所需。
博创云素材 » Android本地打包指南Android studio 离线打包uniapp

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
提示下载完但解压或打开不了?
最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们。

发表评论