設定Android Studio的rosjava開發環境

855 阅读1分钟

前言

因工作上的需求要撰寫rosjava相關的Android APP, 所以花了一點時間研究如何在Android Stuido上編寫ROS的程式(rosjava)。後來發現較少這方面的中文資訊(也有可能是我關鍵字下不好, 所以都找不太到XD), 覺得好像很少人使用(?)大部分應該都是用c++或是python撰寫ROS相關程式, 因此想記錄本文, 幫助也需要開發rosjava相關程式的朋友。

電腦環境

  • Computer
    • Ubuntu 16.04
    • Android Studio 4.1.1 / Gradle 5.4.1
    • ROS kinetic

設置 rosjava dependencies 於 Android Studio

Step 1. 增加下述設定於build.gradle檔案(project):

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        maven {
            url "https://github.com/rosjava/rosjava_mvn_repo/raw/master"
        }
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.5.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

buildscript {
    apply from: "https://github.com/rosjava/android_core/raw/kinetic/buildscript.gradle"
}

subprojects {
    apply plugin: 'ros-android'

    afterEvaluate { project ->
        android {
            // Exclude a few files that are duplicated across our dependencies and
            // prevent packaging Android applications.
            packagingOptions {
                exclude "META-INF/LICENSE.txt"
                exclude "META-INF/NOTICE.txt"
            }
        }
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Step 2. 增加下述設定於 build.grafle(app) 檔案:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
  
dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'org.ros.android_core:android_core_components:0.4.0'
    implementation 'com.github.rosjava.android_remocons:common_tools:[0.3,0.4)'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

附註: 如果遇到此error:

Manifest merger failed : Attribute application@icon value=(@mipmap/ic_launcher)
from AndroidManifest.xml:10:9-43 is also present at 
[org.ros.android_core:android_10:0.3.3] AndroidManifest.xml:19:9-36 value=(@mipmap/icon).

請修改 AndroidManifest.xml :

<application xmlns:tools="http://schemas.android.com/tools"
         ... 
     tools:replace="android:icon">
        ...
</application>

Step 3. 設定APP的網路使用權限和進入APP時開啟ROS master chooser

請增加下述於 AndroidManifest.xml:

  • For internet
<uses-permission android:name="android.permission.INTERNET" />  
  • For ROS master chooser
<activity android:name="org.ros.android.MasterChooser" />  
<service android:name="org.ros.android.NodeMainExecutorService" >  
   <intent-filter>
     <action android:name="org.ros.android.NodeMainExecutorService" />  
   </intent-filter>
</service>

以上這樣就是設定完成啦~可以開始撰寫你的rosjava程式了!! 如果文章有錯誤的地方, 歡迎留言告訴我, 非常感謝(ˉ︶ˉ)