2017-11-17 4 views
1

私はアンドロイドプロジェクトを1つのアンドロイドライブラリモジュールで持っていますが、従来の方法では使用したくありません。dex jarおよびcライブラリを抽出するためにgradleタスクを設定するにはどうすればよいですか?

今私が行っているのは、ライブラリー(のアセンブル gradleタスク)をアセンブルし、結果の* .aarファイルを後処理することです。

私はGradleのタスクを作成したいと思います:

  1. classes.jarとDEXを構築するには(... --dxをDEXと同様)、それは、S。
  2. ネイティブライブラリをビルドします。
  3. 私が選択できるフォルダ内のすべてを移動します。

残念ながら、私はちょうど(埋め込まれたGradleのタスクexternalNativeBuildReleaseで)ネイティブライブラリを構築する方法を発見しました。

誰でもこのグルーヴなグラデルの問題を手伝うことができますか?

答えて

2

から抽出したいもののほとんどではないにしても、ほとんどを行うファイルはbuild.gradleです。
this答えに使っています。
The Gradle build system- TutorialおよびWriting Custom Tasksも参照してください。ここで

は、定義されたタスクです:

  • タスクcopyClasses
  • タスクassembleExternalJar
  • タスクcopyJarToOutputs

それはあなたがやりたいためにインスピレーションを与える必要があります。

import org.apache.tools.ant.taskdefs.condition.Os 

//jrg 
apply plugin: 'com.android.library' 
//apply plugin: 'com.android.application' 

android { 
    compileSdkVersion Integer.parseInt(COMPILE_SDK) 
    buildToolsVersion BUILD_TOOLS_VERSION 

    defaultConfig { 
     targetSdkVersion Integer.parseInt(TARGET_SDK) 
     minSdkVersion Integer.parseInt(MIN_SDK) 
    } 
} 
// Add the main project as a dependency for our library 
dependencies { 
    compile project(':app') 
} 
// Define some tasks which are used in the build process 
task copyClasses(type: Copy) { // Copy the assembled *.class files for only the current namespace into a new directory 
    // get directory for current namespace 
    def namespacePath = PLUGIN_NAMESPACE.replaceAll("\\.","/") 
    // set source and destination directories 
    from "build/intermediates/classes/release/${namespacePath}/" 
    into "build/intermediates/dex/${namespacePath}/" 

    // exclude classes which don't have a corresponding entry in the source directory 
    def remExt = { name -> name.lastIndexOf('.').with {it != -1 ? name[0..<it] : name} } 
    eachFile {details -> 
     def thisFile = new File("${projectDir}/src/main/java/${namespacePath}/", remExt(details.name)+".java") 
     if (!(thisFile.exists())) { 
      details.exclude() 
     } 
    } 
} 

task assembleExternalJar << { 
    // Get the location of the Android SDK 
    ext.androidSdkDir = System.env.ANDROID_HOME 
    if(androidSdkDir == null) { 
     Properties localProps = new Properties() 
     localProps.load(new FileInputStream(file('local.properties'))) 
     ext.androidSdkDir = localProps['sdk.dir'] 
    } 
    // Make sure no existing jar file exists as this will cause dx to fail 
    new File("${buildDir}/intermediates/dex/${PLUGIN_NAMESPACE}.jar").delete(); 
    // Use command line dx utility to convert *.class files into classes.dex inside jar archive 
    String cmdExt = Os.isFamily(Os.FAMILY_WINDOWS) ? '.bat' : '' 
    exec { 
     commandLine "${androidSdkDir}/build-tools/${BUILD_TOOLS_VERSION}/dx${cmdExt}", '--dex', 
        "--output=${buildDir}/intermediates/dex/${PLUGIN_NAMESPACE}.jar", 
        "${buildDir}/intermediates/dex/" 
    } 
    copyJarToOutputs.execute() 
} 

task copyJarToOutputs(type: Copy) { 
    // Copy the built jar archive to the outputs folder 
    from 'build/intermediates/dex/' 
    into 'build/outputs/' 
    include '*.jar' 
} 


// Set the dependencies of the build tasks so that assembleExternalJar does a complete build 
copyClasses.dependsOn(assemble) 
assembleExternalJar.dependsOn(copyClasses) 
関連する問題