前言
本实习生认为,像我们这样计算机专业的,主打的就是一个前人栽树,后人乘凉。本人也在网上看过各种各样的文档,搜过各种各样的报错,扮演CV工程师,在前人的文档下,少走了很多弯路,少写了很多代码。可以说是在前人栽的树下乘了不少凉。我也将实习时遇到的一个问题和任务,写成了一个文档,分享出来。
前段时间因为工作问题,老板交给我一个任务, uniapp前端如何调用后端代码。经过我的学习使用之后,发现其原理就是,使用Android Studio,将Java代码打包成uniapp可以调用的aar文件。
本人是后端开发,但这里边的知识其实并不涉及到主流的后端Java开发,这种操作比较小众,所以网上的很多文档讲的并不详细,于是就自己整理出来一份。
在搜文档的过程中,遇到过很多报错,有的报错,甚至都困扰了我好几天,因为网上的文档并不详细和准确,所以本人就将自己的开发过程和中间遇到的报错及其解决,整理成开发文档。本开发文档比别的文档步骤更详细,每一步都有图片说明,而且整理的很多,报错以及很详细的解决,希望能帮助到大家。
1.开发环境
1.JAVA环境 jdk1.8
2.Android Studio 下载地址:https://developer.android.google.cn/studio/index.html
3.App离线SDK下载:https://nativesupport.dcloud.net.cn/AppDocs/download/android
4.HBuilderX 下载地址:HBuilderX-高效极客技巧HBuilderX-高效极客技巧HBuilderX-高效极客技巧
5.Uniapp官方文档:简介 | uni小程序SDK
2. 解压SDK压缩包
3. 导入UniPlugin-Hello-AS项目
(如果打开之后报错,如下图)
(如报错请将Gradle Version修改到适配版本)
(下图为成功展示)
4.切换为project显示
5. 可删除提供的demo文件
删除build.gradle插件
删除settings.gradle 设置
刷新一下项目
6. 新建module、并在build.gradle文件添加组件
(如果Android Studio中下方的 build 还报错,比如)
(解决方法:有可能是当前的项目的版本配置不对:)
将其修改为
(报错解决之后,添加插件)
7.修改test-module项目文件
plugins {
id 'com.android.library'
}
android {
namespace 'com.example.test_module'
compileSdkVersion 33
defaultConfig {
minSdkVersion 24
targetSdkVersion 28
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compileOnly fileTree(dir: 'libs', include: ['*.jar'])
compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar'])
compileOnly 'androidx.recyclerview:recyclerview:1.0.0'
compileOnly 'androidx.legacy:legacy-support-v4:1.0.0'
compileOnly 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.alibaba:fastjson:1.1.46.android'
implementation 'com.facebook.fresco:fresco:1.13.0'
// implementation 'androidx.appcompat:appcompat:1.6.1'
// implementation 'com.google.android.material:material:1.8.0'
// testImplementation 'junit:junit:4.13.2'
// androidTestImplementation 'androidx.test.ext:junit:1.1.5'
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
(修改test-module – AndroidManifest.xml)
8.新建UniTestModule.java
9. 编译项目、生成arr包
(如未出现打包文件,可以刷新一下项目)