uniApp-Android原生插件编译

既android lib
Android Library在结构目录上与Andraoid App相同,能够包含构建App所需要的一切(源码、资源文件、AndroidManifest.xml)。Android App最终被编译打包成能在Android设备上运行的APK文件,Android Library可以被编译成供其他Android App依赖的aar文件。
1、创建新的项目
创建一个新的project最小的minSdkVersion建议为19
2、添加依赖包
在app->libs中添加创建插件必要的4个依赖包:
android-gif-drawable-release@1.2.17.aar、
lib.5plus.base-release.aar、
msa_mdid_1.0.13.aar、
uniapp-v8-release.aar
3、添加资源包
在app->src->main->新建一个assets包->将uniapp生成的app资源包以及data包放入其中
修改data文件中的dcloud_control.xml 将appid改为资源文件名称
4、修改build.gradle(app)

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "com.example.myuniapp"
        minSdkVersion 19
        targetSdkVersion 28

        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        ndk{
            abiFilters 'x86','armeabi-v7a'
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
            jniLibs.srcDirs = ['libs']
            jni.srcDirs = [] //不自动编译JNI目录,使用ndk-build手动编译
        }
    }
    //使用uniapp时,需复制下面代码
    /*代码开始*/
    aaptOptions {
        additionalParameters '--auto-add-overlay'
        /Compress 'foo', 'bar'
        ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
    }
    /*代码结束*/
    allprojects {
        repositories {
            google()
            mavenCentral()
//        jcenter() // Warning: this repository is going to shut down soon
        }
    }
}
repositories {
    flatDir {
        dirs 'libs'
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation fileTree(dir: 'libs', include: ['*.aar'])
    implementation "com.android.support:support-v4:28.0.0"
    implementation "com.android.support:appcompat-v7:28.0.0"
    /*uniapp所需库-----------------------开始*/
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.facebook.fresco:fresco:1.13.0'
    implementation "com.facebook.fresco:animated-gif:1.13.0"
    /*uniapp所需库-----------------------结束*/

    // 基座需要,必须添加
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'com.alibaba:fastjson:1.1.46.android'
}

5、删除不必要的文件夹
比如java文件包、res中的drawable文件夹、layout文件夹、等,只保留values文件夹
6、修改AndroidManifest.xml

<supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true"/>

    <!--Barcode(二维码) begin-->
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>

    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <!--Barcode(二维码) end-->

    <application
        android:name="io.dcloud.application.DCloudApplication"
        android:allowBackup="true"
        android:allowClearUserData="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:debuggable="true"
        android:supportsRtl="true">

        <!--应用入口-->
        <activity
            android:name="io.dcloud.PandoraEntry"
            android:theme="@style/TranslucentTheme"
            android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <data android:scheme="hbuilder"/>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE"/>
            </intent-filter>
            <intent-filter>
                <data android:mimeType="image/*"/>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data android:name="dcloud_uninview_background"
            android:value="true"/>
        <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.securitycebter.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="h5613bcf"/>
            </intent-filter>
        </activity>

        <provider
            android:authorities="com.HBuilder..UniPlugin.dc.fileprovider"
            android:name="io.dcloud.common.util.DCloud_FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/dcloud_file_provider"/>
        </provider>

</application>

7、新建module
New Module->Android Library Minimum SDK 建议为19
8、修改build.fradle(module)

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 28

        versionCode 1
        versionName "1.0"

        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'])

    implementation "com.android.support:support-v4:28.0.0"
    implementation "com.android.support:appcompat-v7:28.0.0"

    /*uniapp所需库-----------------------开始*/
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.facebook.fresco:fresco:1.13.0'
    implementation "com.facebook.fresco:animated-gif:1.13.0"
    /*uniapp所需库-----------------------结束*/

    // 基座需要,必须添加
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'com.alibaba:fastjson:1.1.72.android'
}

9、在新建的module的consumer-rules.pro和proguard-rule.pro中添加
-keep public class * extends io.dcloud.feature.uniapp.common.UniModule{*;}
10、在module中编辑java逻辑代码
11、修改build.gradle(app)
在gradle的dependencies中添加implementation project(':module名)
12、修改dcloud_uniplugins.json
添加:

{
      "plugins": [
        {
          "type": "module",   //类型,有module、component两种
          "name": "插件名称",  //插件名称(跟前端名称对应)
          "class": "com.example.java名"  //library的java包,对外暴露的类,需要继承uniapp封装的类
        }
      ]
},

13、打包成.arr包
android library 打包成.aar的包:
点击Gradle->Tasks->找到需要打包的library->build->assemble
双击assemble之后等待编译完成 bulid->outputs->aar文件夹下会生成debug.arr和release.aar两个包

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值