且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android Studio中构建sipdroid?

更新时间:2023-01-02 22:26:57

首先,您始终可以使用命令行构建jni-part,方法是:

At first, you always can build jni-part with command line, doing this way:

$ export NDK_PROJECT_PATH=/opt/umake/AndroidStudioProjects/Sipdroid/app/src/main
$ /opt/umake/android/android-ndk/ndk-build

并使用以下命令创建gradle.properties文件:

and create gradle.properties file with

android.useDeprecatedNdk=true

位于项目的根部.

使用新的 build.gradle (模块:app)将是:

And with new build.gradle (Module: app) will be this:

apply plugin: 'com.android.application'


android {
    compileSdkVersion 21
    buildToolsVersion "21.0.1"

    defaultConfig {
        applicationId "org.sipdroid.sipua"
        minSdkVersion 5
        targetSdkVersion 21

        ndk {
            moduleName "OSNetworkSystem"
        }
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets.main.jni.srcDirs = [] // disable automatic ndk-build call, which ignore our Android.mk
    sourceSets.main.jniLibs.srcDir 'src/main/libs'
}

它对我有用.

通常,我使用实验性Android Gradle插件,它对于本机项目要好得多.将 sipdroid 导入Android Studio后,您需要将 build.gradle (模块:应用)更改为:

Usually I use Experimental Android Gradle Plugin, it much better for native projects. After import sipdroid to Android Studio you need to change build.gradle (Module: app) to this:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 21
        buildToolsVersion = "21.0.1"

        defaultConfig.with {
            applicationId = "org.sipdroid.sipua"
            minSdkVersion.apiLevel = 10
            targetSdkVersion.apiLevel = 21
        }
    }
    compileOptions.with {
        sourceCompatibility=JavaVersion.VERSION_1_7
        targetCompatibility=JavaVersion.VERSION_1_7
    }
    /*
     * native build settings
     */
    android.ndk {
        moduleName = "OSNetworkSystem"
        /*
         * Other ndk flags configurable here are
         * cppFlags.add("-fno-rtti")
         * cppFlags.add("-fno-exceptions")
         * ldLibs.addAll(["android", "log"])
         * stl       = "system"
         */
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
        }
    }
    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
        }
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
        create("arm8") {
            ndk.abiFilters.add("arm64-v8a")
        }
        create("x86") {
            ndk.abiFilters.add("x86")
        }
        create("x86-64") {
            ndk.abiFilters.add("x86_64")
        }
        create("mips") {
            ndk.abiFilters.add("mips")
        }
        create("mips-64") {
            ndk.abiFilters.add("mips64")
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }
}

并从以下位置更改build.gradle(整个项目,根目录)中的类路径:

and change classpath in build.gradle (whole project, root directory) from

 'com.android.tools.build:gradle:1.5.0'

'com.android.tools.build:gradle-experimental:0.4.0'

并在项目根目录中使用'android.useDeprecatedNdk = true'删除gradle.properties文件.

and delete gradle.properties file with 'android.useDeprecatedNdk=true' at the root of the project.

构建后,Android Studio将通过 Gradle Build Messages 消除包含源代码中的许多错误,并通过单击相应的消息来定位它们.

After build Android Studio will eliminate with Gradle Build Messages lots of errors in sources with includes, and locate them by clicking at corresponding message.

我看不到带有实验性Android Gradle插件的任何其他变体,而是更改了cpp和c文件中的包含内容,如下所示:

I don't see any other variants with Experimental Android Gradle Plugin, rather than to change includes in cpp and c-files like this:

例如 ndk/silk/src/SKP_Silk_main_FIX.h :

#include "SKP_Silk_typedef.h"

#include "../interface/SKP_Silk_typedef.h"

可悲的是,此片段与Gradle实验插件非常感谢,但没有效果.

It's sad that this fragment synch with Gradle Experimental Plugin gratefully, but hasn't effect.

android.sources {
    main {
        jni {
            source {
                srcDirs = []
            }
        }
    }
}

有来自的解决方案 如何在新的gradle构建系统中使用自定义Android.mk

android.sources{
    main.jni {
        source {
            srcDirs = []
        }
    }
    main.jniLibs {
        source {
            srcDirs = ['src/main/libs']
        }
    }
}

似乎插件 gradle-experimental:0.4.0 中的 bug 会引发此错误

It seems a bug in plugin gradle-experimental:0.4.0 throws this error

BError:Attempt to read a write only view of model of type 'java.lang.Object' given to rule 'model.android.sources'

但如果删除 jni 并仅保存 libs 目录,则 gradle-experimental:0.3.0-alpha7 可以正常工作.

but gradle-experimental:0.3.0-alpha7 works fine if remove jni and save only libs directory.