NDK Debugging with gradle-experimental plugin

亡梦爱人 提交于 2019-12-01 11:52:43

问题


I'm trying to add native debugging to a project that is an Android Studio NDK project. In the past I just used gradle to kick off a shell script, which built the NDK lib. Now I'm trying to move to use the gradle-experimental plugin.

I've scoured the net for what little info there is, (mostly here, Android Tools Site - Gradle Experimental), about using gradle-experimental with the NDK and I've put together this build.gradle file which is using the preview NDK support for doing the NDK build inline with the Java build.

After finally getting this together from bits-and-pieces of info, I managed to get the NDK portion building, but now it fails to include the httpmime-4.4-beta1.jar file that is clearly included in the dependencies, and I've tried many different permutations of it such as in:

compile files("libs/httpmime-4.4.jar")

But regardless, the errors for the missing symbols from the Jar file still appear.

build.gradle source

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

    String APP_PACKAGE_NAME = 'com.obfuscated.app',
           VERSION_NAME = '3.0',
           TOOLS_VERSION = '23.0.2'

    int VERSION_CODE = 15,
        MIN_SDK_VERSION = 13,
        TARGET_SDK_VERSION = 19,
        COMPILE_SDK_VERSION = 23

    model {
        repositories {
            libs(PrebuiltLibraries) {
                // prebuilt binaries mirroring Android.mk
                libstuff {
                    headers.srcDirs.add(file("jni/stuff/include/stuff"))
                    binaries.withType(SharedLibraryBinary) {
                        sharedLibraryFile = file("jni/stuff/lib/libstuff.so")
                    }
                }
                // ...several more of these actually exist in build.gradle and are working
                cares {
                    headers.srcDirs.add(file("jni/c-ares/include"))
                    binaries.withType(SharedLibraryBinary) { 
                        // StaticLibraryBinary and staticLibraryFile doesnt work despite sample code, at least not for com.android.tools.build:gradle-experimental:0.6.0-alpha5, this builds even though its a static-lib
                        sharedLibraryFile = file("jni/c-ares/lib/libcaresARM.a")
                    }
                }
            }
        }
        android {
            compileSdkVersion = COMPILE_SDK_VERSION
            buildToolsVersion = TOOLS_VERSION

            defaultConfig.with {
                applicationId = APP_PACKAGE_NAME
                minSdkVersion.apiLevel = MIN_SDK_VERSION
                targetSdkVersion.apiLevel = TARGET_SDK_VERSION
                versionCode = VERSION_CODE
                versionName = VERSION_NAME

                buildConfigFields {
                    create() {
                        type "int"
                        name "VALUE"
                        value "1"
                    }
                }

                compileOptions.with {
                    sourceCompatibility = JavaVersion.VERSION_1_7
                    targetCompatibility = JavaVersion.VERSION_1_7
                }
            }

            signingConfigs {
                create("appRelease") {
                    storeFile file('sign.jks')
                    storePassword '...'
                    keyAlias '...'
                    keyPassword '...'
                    storeType "jks"
                }
            }
        } // end android

        android.lintOptions {
            abortOnError = false
        }

        android.packagingOptions {
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/services/javax.annotation.processing.Processor'
        }

        android.ndk {
            moduleName = "native"
            toolchain "clang"
            toolchainVersion "3.5"
            platformVersion = MIN_SDK_VERSION

            ldLibs.addAll('atomic', 'android', 'log', 'OpenSLES')
            abiFilters.addAll(["armeabi", "armeabi-v7a"])
            CFlags.addAll(["-mfloat-abi=softfp", "-mfpu=neon", "-O3", "-DCARES_STATICLIB", "-Wno-c++11-long-long"])
            cppFlags.addAll(["-I${file("jni")}".toString(),
                             "-I${file("jni/c-ares/include")}".toString(),
                             "-I${file("jni/coffeecatch")}".toString()])
            stl = "stlport_shared"
        }

        android.sources {
            main {
                jniLibs {
                    dependencies {

                    }
                }
                jni {
                    dependencies {
                        library "libstuff"
                        library "cares"
                        // ... 
                    }
                    source {
                        srcDir "jni"
                    }
                }
                // java {
                    // dependencies {
                    //     compile files("libs/httpmime-4.4-beta1.jar")
                    //     compile files("libs/FlurryAnalytics-5.1.0.jar")
                    // }
                // }
            }
        }

        android.buildTypes {
            debug {
                ndk.with {
                    debuggable = true
                }
            }
            release {
                minifyEnabled = false
                ndk.with {
                    debuggable = true
                }
            }
        }

        android.productFlavors {
            create("arm") {
                ndk.with {
                    abiFilters.add("armeabi-v7a")

                    ldLibs.addAll([file("jni/stuff/lib/libstuff.so").toString(),
                                            file("jni/c-ares/lib/libcaresARM.a").toString()])
                }
            }
            create("fat") {
                // compile and package all supported ABI
            }
        }

    } // end model

    repositories {
        mavenCentral()
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])

        compile 'com.android.support:support-v4:23.+'
        compile 'com.android.support:appcompat-v7:23.+'
        compile 'com.android.support:support-v13:23.+'
        compile 'com.android.support:support-annotations:23.+'

        compile 'com.squareup:otto:1.3.8'
        compile 'com.github.machinarius:preferencefragment:0.1.1'
        compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
        compile 'com.fasterxml.jackson.core:jackson-core:2.5.+'
        compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.+'
        compile 'com.fasterxml.jackson.core:jackson-databind:2.5.+'

        compile 'com.jakewharton:butterknife:7.0.1'
        compile 'com.google.guava:guava:19.0'
    }


    allprojects {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked"
        }
    }

Out of frustration, I switched back to the non-experimental branch, and even with the old build.gradle file it is now failing to find that same jar file. So is it a problem with Android Studio 2.0 Preview 6?

Has anyone else experienced this, or have a solution for it? It would be so convenient to finally have NDK debugging work right in Android Studio, and if it weren't for this last hurdle, I think I would be there.

Short of re-writing the code that depends on that jar file, I am at a loss for what else to try. I am also open to suggestions for the format of my build.gradle file above, since the documentation for these new features is very sparse still, and some of the samples seem to be already out-of-date with regard to the proper syntax.

WHAT AM I MISSING?

You can see that the C and Cpp (mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainCpp), steps are happening just fine, but then the Javac fails. This jar file approach has worked fine for the past 2 years or so for the http-mime lib from apache, so I don't understand why suddenly this is a problem.

    :mobile:mergeArmDebugAndroidTestAssets
    :mobile:generateArmDebugAndroidTestResValues UP-TO-DATE
    :mobile:generateArmDebugAndroidTestResources
    :mobile:mergeArmDebugAndroidTestResources
    :mobile:processArmDebugAndroidTestResources
    :mobile:generateArmDebugAndroidTestSources
    :mobile:copyArmeabi-v7aDebugArmSharedLibraryStlSo
    :mobile:compileNativeArmeabi-v7aDebugArmSharedLibraryNativeMainC
    :mobile:compileNativeArmeabi-v7aDebugArmSharedLibraryNativeMainCpp
    :mobile:linkNativeArmeabi-v7aDebugArmSharedLibrary
    :mobile:nativeArmeabi-v7aDebugArmSharedLibrary
    :mobile:stripSymbolsArmeabi-v7aDebugArmSharedLibrary
    :mobile:ndkBuildArmeabi-v7aDebugArmSharedLibrary
    :mobile:ndkBuildArmeabi-v7aDebugArmStaticLibrary UP-TO-DATE
    :mobile:copyArmeabiDebugArmSharedLibraryStlSo
    :mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainC
    :mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainCpp
    :mobile:linkNativeArmeabiDebugArmSharedLibrary
    :mobile:nativeArmeabiDebugArmSharedLibrary
    :mobile:stripSymbolsArmeabiDebugArmSharedLibrary
    :mobile:ndkBuildArmeabiDebugArmSharedLibrary
    :mobile:ndkBuildArmeabiDebugArmStaticLibrary UP-TO-DATE
    :mobile:processAndroidArmDebugMainJniLibs UP-TO-DATE
    :mobile:androidArmDebug
    :mobile:compileArmDebugJavaWithJavac
    :mobile:compileArmDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).

Yes, I know the apache libs are deprecated, but this is legacy code that should work despite that fact, and will be updated in the future.


回答1:


A general way to do the include you're looking for is this in the dependencies. compile fileTree(dir: 'libs', include: ['*.jar'])

However, I'm not certain that will solve this particular problem. I've always had success with putting the jar in the libs directory at the top of the directory structure. If you need to have the jar in a different location, then this works for me:

repositories {
    flatDir {
        dirs '<relativePathToJar>'
    }
}

model { ... }

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}


来源:https://stackoverflow.com/questions/34959013/ndk-debugging-with-gradle-experimental-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!