Uniapp calls java code

Preface

This intern believes that for computer majors like us, the main thing is that the predecessors plant trees and the descendants can enjoy the shade. I have also read various documents on the Internet, searched for various error reports, and acted as a CV engineer. Under the documentation of predecessors, I have avoided a lot of detours and written a lot of code. It can be said that we enjoyed a lot of shade under the trees planted by our predecessors. I also wrote a document and shared a problem and task I encountered during my internship.

Some time ago, due to work problems, my boss gave me a task, how to call the back-end code on the uniapp front-end. After my study and use, I found that the principle is to use Android Studio to package Java code into an aar file that can be called by uniapp.

I am a back-end developer, but the knowledge here does not actually involve mainstream back-end Java development. This kind of operation is relatively niche, so many documents on the Internet are not detailed, so I compiled one myself.
In the process of searching for documents, I encountered many errors. Some of them even troubled me for several days. Because the documents on the Internet were not detailed and accurate, I summarized my development process and the errors I encountered in the process. and its solutions, organized into development documents. This development document has more detailed steps than other documents. Each step has a picture description, and it is organized in many ways, including error reports and very detailed solutions. I hope it can help everyone.

1. Development environment

1.JAVA environment jdk1.8

2.Android Studio download address: https://developer.android.google.cn/studio/index.html

3.App offline SDK download: https://nativesupport.dcloud.net.cn/AppDocs/download/android

4. HBuilderX download address: HBuilderX – Efficient Geek Techniques HBuilderX – Efficient Geek Techniques HBuilderX – Efficient Geek Techniques

5.Uniapp official documentation: Introduction | uni applet SDK

2. Unzip the SDK compressed package

3. Import the UniPlugin-Hello-AS project

(If an error is reported after opening, as shown below)

(If an error is reported, please modify the Gradle Version to the appropriate version)

(The picture below shows a successful demonstration)

4. Switch to project display

5. Provided demo file

Remove build.gradle plugin

Delete settings.gradle settings

Refresh the project

6. Create a new module and Add components in the build.gradle file

(If the build below in Android Studio still reports an error, for example)

(Solution: It is possible that the version configuration of the current project is incorrect:)

Modify it to

(After the error is resolved, add the plug-in)

7. Modify the test-module project file

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

(Modifytest-module – AndroidManifest.xml)

8. Create a new UniTestModule.java

9. Compile the project and generate the arr package

(If the package file does not appear, you can refresh the project)

10. The packaging is completed. test-module-release.arr is the plug-in you packaged, which can be transplanted and used on uniapp.

syntaxbug.com © 2021 All Rights Reserved.