User Tools

Site Tools


Writing /net/www/auesnard/teaching/data/cache/c/c44412bcd364481bb150cf7c45d9d538.metadata failed
Writing /net/www/auesnard/teaching/data/meta/projtec/android.meta failed
projtec:android

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
projtec:android [2017/01/18 16:26] – [Tutoriels] orelprojtec:android [2024/03/18 15:06] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Android ======+====== Portage SDL2 sous Android ======
  
-L'objectif de ce petit tutoriel est de développer des applications Android en ligne de commande... 
  
-==== Installation ==== +==== Démo ====
- +
-Nous allons commencer par installer le kit de développement Android, les [[ https://developer.android.com/studio/index.html | Tools ]], le[[ https://developer.android.com/ndk/downloads/index.html | NDK ]] et [[ https://gradle.org/gradle-download/ | Gradle]].  +
- +
-  mkdir /opt/Android/ +
-  cd /opt/Android +
-  wget https://dl.google.com/android/repository/tools_r25.2.3-linux.zip +
-  wget https://dl.google.com/android/repository/android-ndk-r13b-linux-x86_64.zip +
-  wget https://services.gradle.org/distributions/gradle-3.3-all.zip +
-  unzip *.zip +
- +
-Nous allons maintenant poursuivre l'installation avec le //SDK Manager//, en installant principalement le SDK (API 25). Attention à ne pas oublier d'accepter les licences au moment de l'installation ! +
- +
-  tools/android sdk &   +
- +
-Puis, on positionne toutes les variables d'environnement nécessaires : //source env.sh//. +
- +
-<code bash env.sh> +
-ROOT=/opt/Android +
-export ANDROID_HOME=$ROOT +
-export GRADLE_HOME=$ROOT/gradle-3.3 +
-export ANDROID_NDK_HOME=$ROOT/android-ndk-r13b +
-export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$ANDROID_NDK_HOME:$GRADLE_HOME/bin +
-</code> +
- +
-==== Installation avec Android Studio ==== +
- +
-Une alternative facile à tout ce merdier consiste à installer [[ https://developer.android.com/studio/index.html | Android Studio ]], dont le //SDK Manager// (plus puissant) permet d'installer tous les outils nécessaires comme //Gradle, NDK, CMake, etc.// +
- +
-  mkdir /opt/Android/ +
-  cd /opt/Android +
-  wget https://dl.google.com/dl/android/studio/ide-zips/2.2.3.0/android-studio-ide-145.3537739-linux.zip +
-  unzip android-studio-ide-*-linux.zip +
-  cd android-studio/bin +
-  ./studio.sh              +
- +
-Une fois l'IDE installée, si l'on souhaite utiliser le NDK (Native Development Kit), il faut installer quelques outils supplémentaires via le menu Tools/Android/SDK Manager : LLDB, CMake et NDK. Puis, on positionne toutes les variables d'environnement nécessaires : //source env.sh//. +
- +
-<code bash env.sh> +
-ROOT=/opt/Android +
-export ANDROID_HOME=$ROOT/sdk +
-export ANDROID_NDK_HOME=$ROOT/sdk/ndk-bundle +
-export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$ANDROID_NDK_HOME +
-</code> +
- +
- +
-==== Tutoriels ==== +
- +
-  * [[android:install | Installation]] +
-  * [[android:helloworld | Hello World]] +
-  * (...) +
-  * [[android:misc | Misc]] +
- +
-==== Hello World from Scratch ==== +
- +
-Considérons un petit exemple Android minimaliste, //HelloWorld// ! Je me suis librement inspiré de plusieurs tutoriels :  +
-  * http://czak.pl/2016/01/13/minimal-android-project.html +
-  * https://spring.io/guides/gs/gradle-android +
-  * https://code.tutsplus.com/tutorials/android-from-scratch-building-your-first-android-application--cms-26024 +
-  * https://developer.android.com/training/basics/firstapp/index.html +
- +
- +
-Notre projet //HelloWorld// nécessite quelques fichiers organisés selon l'arborescence suivante ({{android:HelloWorld-v1.zip}}) : +
- +
-<code> +
-├── build.gradle +
-└── src +
-    └── main +
-        ├── AndroidManifest.xml +
-        ├── java +
-        │   └── org +
-        │       └── hello +
-        │           └── HelloActivity.java +
-        └── res +
-            ├── drawable +
-            │   └── icon.png +
-            ├── layout +
-            │   └── mylayout.xml +
-            └── values +
-                └── strings.xml +
-</code> +
- +
-Le fichier //AndroidManifest.xml// contient les informations de haut-niveau de l'application Android. Dans ce cas, l'application a pour //label// la valeur définie dans le fichier ressource "res/values/strings.xml", c'est-à-dire "MyHelloWorld". Et son icône est définie par le fichier ressource "res/drawable/icon.png" (au format hdpi, 72 x 72 px). Son //activity// est défini dans la classe //HelloActivity// du package //org.hello//. La section //intent-filter// précise qu'il s'agit de l'activity principale initiant notre application ("android.intent.action.MAIN"), qui sera affichée dans le //launcher// du smartphone ("android.intent.category.LAUNCHER"). +
- +
-<code xml src/main/AndroidManifest.xml> +
-<?xml version="1.0" encoding="utf-8"?> +
-<manifest xmlns:android="http://schemas.android.com/apk/res/android" +
-    package="org.hello" +
-    android:versionCode="1" +
-    android:versionName="1.0.0"+
- +
-    <application android:label="@string/app_name" android:icon="@drawable/icon"> +
-        <activity android:name=".HelloActivity"> +
-            <intent-filter> +
-                <action android:name="android.intent.action.MAIN" /> +
-                <category android:name="android.intent.category.LAUNCHER" /> +
-            </intent-filter> +
-        </activity> +
-    </application> +
-</manifest> +
-</code> +
- +
-<code xml res/values/strings.xml> +
-<?xml version="1.0" encoding="utf-8"?> +
-<resources> +
-    <string name="app_name">MyHelloWorld</string> +
-</resources> +
-</code> +
- +
-Le fichier //mylayout.xml// est un fichier XML qui décrit le //layout// de votre //activity//, c'est-à-dire l'agencement de l'interface graphique de votre application. Celle-ci contient essentiellement composé d'un //TextView// et d'un //Button//. Le champs //id// permet de faire référence au //TextView// dans le code grâce à la méthode //findViewById()//. Concernant le //Button//, on définit une methode callback //sayHello()// qui sera appelé sur l'évènement //onClick//+
- +
-<code xml src/main/res/layout/mylayout.xml> +
-<?xml version="1.0" encoding="utf-8"?> +
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" +
-    android:orientation="vertical" +
-    android:layout_width="fill_parent" +
-    android:layout_height="fill_parent" +
-    > +
-<TextView   +
-    android:id="@+id/mytextview" +
-    android:layout_centerInParent="true" +
-    android:layout_width="fill_parent"  +
-    android:layout_height="wrap_content" +
-    android:text="Click the Button" +
-    /> +
-<Button +
-    android:layout_centerInParent="true" +
-    android:layout_width="wrap_content" +
-    android:layout_height="wrap_content" +
-    android:layout_centerHorizontal="true" +
-    android:text="Button" +
-    android:onClick="sayHello"/>  +
-</LinearLayout> +
-</code> +
- +
-La classe //HelloActivity// (du fichier //HelloActivity.java//) définit le point d'entrée de notre application Android (ou plutôt de son //activity// principale). La compilation génère une classe //R.java// qui fait référence le layout //R.layout.mylayout// et ses identifiants comme //R.id.mytextview//. Notons que tous les éléments visuels comme //Button// ou //TextView// hérite de la classe //View//. +
-  +
-<code java src/main/java/org/hello/HelloActivity.java> +
-package org.hello; +
- +
-import android.app.Activity; +
-import android.os.Bundle; +
-import android.widget.TextView; +
-import android.view.View; +
- +
-public class HelloActivity extends Activity { +
- +
-    private TextView textview; +
- +
-    @Override +
-    public void onCreate(Bundle savedInstanceState) { +
-        super.onCreate(savedInstanceState); +
-        setContentView(R.layout.mylayout); +
-        textview = (TextView) findViewById(R.id.mytextview); +
-    } +
- +
-    @Override +
-    public void onStart() { +
-        super.onStart(); +
-    } +
- +
-    public void sayHello(View view) { +
-        textview.setText("Hello World!"); +
-    } +
-     +
-+
-</code> +
- +
- +
-__Nota Bene __ : +
- +
-  * Structure du Projet : https://developer.android.com/studio/projects/index.html +
-  * Version du plugin Gradle : https://developer.android.com/studio/releases/gradle-plugin.html +
-  * Syntaxe du fichier //build.gradle// : https://developer.android.com/studio/build/index.html +
-  * Intro au fichier //AndroidManifest.xml// : https://developer.android.com/guide/topics/manifest/manifest-intro.html +
-  * Les ressources : https://developer.android.com/guide/topics/resources/providing-resources.html +
- +
-==== Compilation avec Gradle ==== +
- +
-A la racine du projet, le fichier //build.gradle// permet à l'outil //Gradle// de construire votre application Android. Voici donc le fichier minimaliste mis à jour pour utiliser la version du SDK (25), des build-tools (25.0.2) et du plugin Gradle 2.2.3 compatible à la version de Gradle que j'ai installé (3.3)  : +
- +
-<code java build.gradle> +
-buildscript { +
-    repositories { +
-        jcenter() +
-    } +
- +
-    dependencies { +
-        classpath 'com.android.tools.build:gradle:2.2.3' +
-    } +
-+
- +
-apply plugin: 'com.android.application' +
- +
-android { +
-    compileSdkVersion 25 +
-    buildToolsVersion "25.0.2" +
-+
-</code> +
- +
- +
-Pour compiler tout ça et le déployer sur un émulateur //MyPhone// (en supposant que vous avez déjà configuré le device //MyPhone// grâce à la commande //android avd//), il faut : +
- +
-  emulator -avd MyPhone        +
-  gradle installDebug +
-   +
-Il ne reste plus qu'à démarrer l'application HelloWorld sur votre émulateur... Et c'est tout ! +
- +
-Afin de pouvoir compiler l'application sans //Gradle// installé sur une machine, il est recommandé d'utiliser //Gradle Wrapper// qui génére un script d'installation portable (//gradlew//). +
- +
-  gradle wrapper +
-  ./gradlew installDebug +
- +
-==== Compilation avec Ant ==== +
- +
-Si l'on préfère utiliser l'outil //Ant// plutôt que //Gradle// pour compiler son application //Android//, il n'y a pas grand chose qui change... En gros, il faut écrire un fichier //build.xml// pour Ant (à la place de //build.gradle//) et modifier un peu l'organisation du projet, comme indiqué ci-dessous ({{android:HelloWorldAnt-v1.zip}}). +
- +
-<code> +
-├── AndroidManifest.xml +
-├── build.xml +
-├── res +
-│   ├── drawable +
-│   │   └── icon.png +
-│   ├── layout +
-│   │   └── mylayout.xml +
-│   └── values +
-│       └── strings.xml +
-└── src +
-    └── org +
-        └── hello +
-            └── HelloActivity.java +
-</code> +
- +
-Voici donc un fichier //build.xml// minimaliste pour notre projet : +
- +
-<code xml build.xml> +
-<?xml version="1.0" encoding="UTF-8"?> +
-<project name="HelloWorldAnt" default="help"> +
- +
-  <property name="target" value="android-25" /> +
- +
-  <property environment="env" /> +
-  <property name="sdk.dir" value="${env.ANDROID_HOME}" /> +
- +
-  <import file="${sdk.dir}/tools/ant/build.xml" /> +
- +
-</project> +
-</code> +
- +
-Et c'est tout... Il ne reste plus qu'à compiler ce projet. Attention à ne pas oublier de positionner la variable d'environnement ANDROID_HOME. +
- +
-  ant debug install +
-   +
-==== Pour aller plus loin ====+
  
 +  * la démo SDL2 de base : {{:projtec:sdl2-demo.zip|}}
 +  * le portage sous Android : {{:projtec:android-sdl2-demo.zip|}}
 +  * [[projtec:readme.md| README]]
  
 +==== Documentation ====
  
 +  * [[android:index | Diverses Notes sur Android]]
  
projtec/android.1484756819.txt.gz · Last modified: 2024/03/18 15:05 (external edit)