Migrate Android.mk project to gradle project (apkization)

Background of the project:

Android.mkFiles located jni/in subdirectories of the project directory describe source files and shared libraries to the build system.

Gradle is an open source tool for project automation and build based on the concepts of Apache Ant and Apache Maven.

Code debugging of MK projects usually uses commands to compile, which sometimes takes a long time and cannot be debugged. By migrating the Android.mk project to a gradle project, you can debug it on Android Studio, which improves the debugging code speed to a certain extent.

Clarify the content of the assignment:

1. Convert mk to gradle

2. Solve compilation errors:

Resource error; Java file cannot be found error (identify dependencies, and create and import jar packages)

mk——gradle

1. Define compilation results

plugins {
    id 'com.android.application'
}
id 'com.android.application' compile and generate apk
id 'com.android.library' Compile and generate aar
id 'maven-publish' 生成maven

2. Define constants

ext {
    APPS_TOOLS_MODEL = "BASIO"
    URBANO = "URBANO"
    EXPLORER = "EXPLORER"
    MIRAIE = "MIRAIE"
    RAFRE = "RAFRE"
    TORQUE = "TORQUE"
    BASIO = "BASIO"
}

Sometimes, the code warehouse of a module contains codes for multiple models. Although the amount of code is relatively large, it is easier to maintain. Defining module names in mk and gradle can control code selection.

3. Configure SDK version

    compileSdk 31 //SDK编译版本

    defaultConfig {
        applicationId "xxx.xxx.xxxx"
        minSdkVersion 31 //最小SDK版本
        targetSdkVersion 31  //系统兼容
        versionCode 1
        versionName "0001.0001"
    }

4.apk packaging

The apk can be installed on the mobile phone through packaging

   signingConfigs {
        debug {
            storeFile file('/home/tsdl/Project/AndroidStudioProject/MkToApk/android_debug_key/platform.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
        config {
            storeFile file('../../android_debug_key/platform.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }

5. Build type

buildTypes {
        release {
            minifyEnabled false
            signingConfig signingConfigs.config //指定release时的signingConfigs对应的配置名
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        debug {
            minifyEnabled false
            signingConfig signingConfigs.config
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        applicationVariants.all {
            variant ->
                variant.outputs.all {
                    output ->
                        if (variant.buildType.name == "release") {
                            outputFileName = "DeskClock-${defaultConfig.versionName}.apk"
                        }
                }
        }

    }

6. Configure java version

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

7. Resource settings

sourceSets {
        main{
            java.srcDirs = ["src/main/java"]
            java.srcDirs += ["src/main/java/MODEL"]
            assets.srcDirs = ["src/main/assets"]
            res.srcDirs = ["src/main/res"]
            manifest.srcFile "src/main/AndroidManifest.xml"

            if (APPS_TOOLS_MODEL == EXPLORER) {
//                res.srcDirs += ["src/main/MODEL/EXPLORER/res"]
//                java.srcDirs += ["src/main/java/EXPLORER"]
                manifest.srcFile "src/main/MODEL/AndroidManifest.xml"
            }
            ...
            else if (APPS_TOOLS_MODEL == BASIO){
                res.srcDirs += ["src/main/MODEL/BASIO/res"]
                java.srcDirs += ["src/main/java/BASIO"]
                manifest.srcFile "src/main/MODEL/BASIO/AndroidManifest.xml"
            }else {
//                res.srcDirs += ["src/main/MODEL/BASE/res"]
//                java.srcDirs += ["src/main/java/BASE"]
            }

            aidl.srcDirs = ["src/main/java/com/android/alarmclock/IRegisterReceiverService.aidl",
                            "src/main/java/com/android/deskclock/IAlarmInitShutdownService.aidl"]

        }
    }

Different gradle versions have slightly different writing methods. Pay attention to the file paths (project structure) of java, res, etc.

Since there are codes for multiple models in my code warehouse, I distinguished them in sourceSets. However, resources cannot be repeated when gradle is compiled. In order to prevent this type of error, I first removed the codes for other models. , and then invest in it when making other models. In fact, the problem of resource duplication can be solved.

release.resources.srcDirs=['src/main/xxxxx']

By setting the priority, choose which res resource to call

8.lint configuration

    lintOptions {
        // true--错误发生后停止gradle构建
        abortOnError false
    }

9. Import resource package

dependencies {
    //noinspection GradleCompatible
    implementation 'com.android.support:support-v13:28.0.0'
    if (APPS_TOOLS_MODEL == EXPLORER ||
            APPS_TOOLS_MODEL == CONCOURSE ||
            APPS_TOOLS_MODEL == URBANO ||
            APPS_TOOLS_MODEL == CEIJI){
        implementation 'designlib_kon:0001.0001'
    }else if (APPS_TOOLS_MODEL == MIRAIE){
        implementation 'designlib_blue35:0001.0001'
    }else if (APPS_TOOLS_MODEL == BASIO){
        implementation 'designlib_biro:0001.0001'
    }
    implementation fileTree(dir: "libs", include: ["*.aar"])
    implementation fileTree(dir: "libs",include: ['*.jar'])
    implementation 'com.android.support:support-v4:28.0.0'
}

In the mk file, which resource libraries are introduced through LOCAL_STATIC_JAVA_LIBRARIES and include xxx/xxx/xxx.mk, and introduced in gradle in the corresponding way.

Compilation error - resource error

Duplicate resources errors are usually reported because gradle cannot compile and the names of the two resources cannot be the same. Weigh it yourself and delete one of them.

In addition, mobile phone manufacturers have their own customized themes. If they are not imported, an error will be reported. However, the change of this theme will not affect the debugging of bugs. You can find a theme to replace it, or if it is not used, just comment it out.

Compilation error - java file was not imported successfully

Generally, this kind of package import error and some methods not found are due to referencing other groups (such as: framework, etc. API)

There are usually several ways to import these resources:

1. Look at the mk file of the dependent code and compile the generated stuff. If lib or jar is generated, compile it directly, then put the generated package in the libs folder of the project, and introduce this package in gradle's dependencies.

2. If it is a code warehouse that generates apk, you need to convert mk to gradle in the same way, generate the arr package, and then import it into the libs folder of the project.

3. Package the project through maven (recommended when dependency nesting is serious; if the project uses this package frequently, maven is not recommended)

Guess you like

Origin blog.csdn.net/m0_50408097/article/details/123088576