Android Studioã§ã©ã¤ãã©ãªããã¸ã§ã¯ããä½æãã
Android Studioã§ã©ã¤ãã©ãªããã¸ã§ã¯ããä½æãããã¨ãããæ°è¦ããã¸ã§ã¯ãä½æã§ã©ã¤ãã©ãªãé¸æã§ããªãã®ã§è©°ãã ã
ã©ã¤ãã©ãªããã¸ã§ã¯ãã¨ããã®ã¯ãã¢ããªã±ã¼ã·ã§ã³ãããªãã¦ã©ã¤ãã©ãªãä½æããããã¸ã§ã¯ãã ã¨æã£ã¦ãããã ãã©ãAndroid Studioã§ã¯ããã¸ã§ã¯ãã®æ°è¦ä½æã§ã©ã¤ãã©ãªããã¸ã§ã¯ãã¨ãããã®ã¯é¸æã§ããªãããªãã§ã ããã¨æåã¯æã£ããã ãã©ã
ã©ã¤ãã©ãªåä½ããåä½ç¢ºèªã§ããªãã ãããã±ã
ã¨ããã¡ãã»ã¼ã¸ãè¾¼ãããã¦ããã«éããªããããæããã¨ã«ãã¦ãã¢ããªã±ã¼ã·ã§ã³ããã¸ã§ã¯ãã«ã©ã¤ãã©ãªã¢ã¸ã¥ã¼ã«ã追å ããæ¹æ³ãé¸æããã
ç°å¢
OS | Windows 8.1(x64) |
JDK | 1.8.0(64bit) |
Android Studio | 1.2.2 |
Android Gradle plugin | 1.2.3 |
Gradle | 2.2.1 |
æ°è¦ããã¸ã§ã¯ãã®ä½æ
ã¾ãã¯æ®éã«ããã¸ã§ã¯ããä½æãããããã§ã¯ããã¸ã§ã¯ãåããMyLibAppãã¨ããã
ããã¾ã§ã¯æ®éã«ã¢ããªã±ã¼ã·ã§ã³ç¨ã®ããã¸ã§ã¯ãä½æãAndroid Studioã使ããã¦ã§ããããããªãã£ã¦ãªãã£ããã ãã©ããå®ã¯ãã®æç¹ã§ããã¸ã§ã¯ãå ã«ã¢ããªã±ã¼ã·ã§ã³ã¢ã¸ã¥ã¼ã«ãappããããã®ã§ããã
æ°è¦ã¢ã¸ã¥ã¼ã«ã®è¿½å
ä½æããããã¸ã§ã¯ãã«å¯¾ãã¦æ°è¦ã¢ã¸ã¥ã¼ã«ã追å ããã
ã¢ã¸ã¥ã¼ã«ã®ã¿ã¤ãããAndroid Libraryãã§é¸æããã
ã©ã¤ãã©ãªåã¨ã¢ã¸ã¥ã¼ã«åã¨ãã決ãããããã§ã¯ã¢ã¸ã¥ã¼ã«åããmylibraryãã¨ããã
ããã¸ã§ã¯ãä½æã®æã¨åæ§ã«è¿½å ããActivityã®ã¿ã¤ããé¸æããç»é¢ãã§ãã®ã§ããAdd No Activityããé¸æ*1ã
ããã¸ã§ã¯ãå ã«ãmylibraryãã¨ããã¢ã¸ã¥ã¼ã«ãå¢ãã¦ããã
ã©ã¤ãã©ãªãä½ã£ã¦ä½¿ã£ã¦ã¿ã
ãã£ããã©ã¤ãã©ãªã¢ã¸ã¥ã¼ã«ãã¢ããªã±ã¼ã·ã§ã³ã¢ã¸ã¥ã¼ã«ããå©ç¨ãããµã³ãã«ãæ¸ãã¦ã¿ãã
ã©ã¤ãã©ãªã¢ã¸ã¥ã¼ã«
ã¾ãã¯ã©ã¤ãã©ãªå´ã
ã½ã¼ã¹
ãµã³ãã«ãªã®ã§ã©ã¤ãã©ãªã«ããæå³ããªããããªPOJOã¯ã©ã¹ãã§ãããã ãã ã¨ã¤ã¾ããªãã®ã§ç¡é§ã«å¤é¨ã©ã¤ãã©ãªã使ã£ã¦ã¿ããã¤ãã§ã«ãããã°ãã«ãã®ã¨ãã ãæå¹ãªã³ã¼ããå ¥ãããã£ã¦ãããã°ãã°ã ãã©ã
- MyLibApp/mylibrary/src/main/java/osa030/hatenablog/com/mylibrary/Hoge.java
package osa030.hatenablog.com.mylibrary; import android.util.Log; import com.google.gson.Gson; public class Hoge { private int id; private String name; public Hoge(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Hoge{" + "id=" + id + ", name='" + name + '\'' + '}'; } // DEBUG log private static void log( String fmt, Object ... args ){ if( BuildConfig.DEBUG ){ Log.d("OSA", String.format(fmt, args)); } } // Object -> JSON String public String encode(){ Gson gson = new Gson(); String json = gson.toJson(this); Hoge.log("encode Object[%s] -> JSON[%s]", this.toString(), json); return json; } // JSON String -> Hoge Object public static Hoge decode( String json ){ Gson gson = new Gson(); Hoge obj = gson.fromJson(json, Hoge.class); Hoge.log("decode JSON[%s] -> Object[%s]", json, obj.toString()); return obj; } }
buid.gradle
ã¢ã¸ã¥ã¼ã«ä½ææã«èªåçæããããã¡ã¤ã«ã«gsonã©ã¤ãã©ãªã使ãè¨å®ã追å
google-gson - A Java library to convert JSON to Java objects and vice-versa - Google Project Hosting
- MyLibApp/mylibrary/build.gradle
apply plugin: 'com.android.library' android { publishNonDefault true compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { minSdkVersion 14 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.google.code.gson:gson:2.2.4' }
ã¢ããªã±ã¼ã·ã§ã³ã¢ã¸ã¥ã¼ã«
ç¶ãã¦ã¢ããªã±ã¼ã·ã§ã³å´ã
ã½ã¼ã¹
ã¢ã¯ãã£ããã£ã®çææã«ã©ã¤ãã©ãªå´ã®ã¯ã©ã¹ãå¼ãã§ã¿ãã ãã
- MyLibApp/app/src/main/java/osa030/hatenablog/com/mylibapp/MainActivity.java
// æç² public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Hoge hoge = new Hoge(1, "osa"); String json = hoge.encode(); Hoge hoge2 = Hoge.decode(json); Log.i("OSA", String.format("JSON:[%s]", json)); Log.i("OSA", String.format("Hoge:[%s]", hoge2)); }
buid.gradle
ã¢ã¸ã¥ã¼ã«ä½ææã«èªåçæããããã¡ã¤ã«ã«ã©ã¤ãã©ãªã¢ã¸ã¥ã¼ã«ãåç §ããããã«è¿½å ããã ãã
- MyLibApp/app/build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "osa030.hatenablog.com.mylibapp2" minSdkVersion 14 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' compile project(':mylibrary') }
å®è¡ãã¦ã¿ã
ãã«ããã¦å®è¡ã
06-12 05:32:10.226 1375-1375/osa030.hatenablog.com.mylibapp2 I/OSAï¹ JSON:[{"name":"osa","id":1}] 06-12 05:32:10.226 1375-1375/osa030.hatenablog.com.mylibapp2 I/OSAï¹ Hoge:[Hoge{id=1, name='osa'}]
ã§ããããã°ãã°ãã§ã¾ããã
$ ls mylibrary/build/outputs/aar/ mylibrary-release.aar
ã©ã¤ãã©ãªã®aarãã¡ã¤ã«ããªãªã¼ã¹ããä½ããã¦ãªããããã¯ã¢ãã«ã¼ã³ã
ä¿®æ£ãã¦ã¿ã
build.gradleãã¢ããªã±ã¼ã·ã§ã³ã¢ã¸ã¥ã¼ã«ã®ãã«ãã¿ã¤ãã§é£åããããã«ããªãã¨ã ããããã
ã©ã¤ãã©ãªã¢ã¸ã¥ã¼ã«
buid.gradle
- MyLibApp/mylibrary/build.gradle
--- build.gradle.org Fri Jun 12 18:35:40 2015 +++ build.gradle Fri Jun 12 18:35:18 2015 @@ -1,6 +1,7 @@ apply plugin: 'com.android.library' android { + publishNonDefault true compileSdkVersion 22 buildToolsVersion "22.0.1"
ã¢ããªã±ã¼ã·ã§ã³ã¢ã¸ã¥ã¼ã«
buid.gradle
- MyLibApp/app/build.gradle
--- build.gradle.org Fri Jun 12 18:38:37 2015 +++ build.gradle Fri Jun 12 18:38:12 2015 @@ -22,5 +22,6 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' - compile project(':mylibrary') + releaseCompile project(path: ':mylibrary', configuration: 'release') + debugCompile project(path: ':mylibrary', configuration: 'debug') }
ã¾ãå®è¡ãã¦ã¿ã
ãSync Project with Gradle filesããã¦ãããã«ããç´ãã¦å®è¡ã
06-12 05:40:32.958 1453-1453/osa030.hatenablog.com.mylibapp2 D/OSAï¹ encode Object[Hoge{id=1, name='osa'}] -> JSON[{"name":"osa","id":1}] 06-12 05:40:32.962 1453-1453/osa030.hatenablog.com.mylibapp2 D/OSAï¹ decode JSON[{"name":"osa","id":1}] -> Object[Hoge{id=1, name='osa'}] 06-12 05:40:32.962 1453-1453/osa030.hatenablog.com.mylibapp2 I/OSAï¹ JSON:[{"name":"osa","id":1}] 06-12 05:40:32.962 1453-1453/osa030.hatenablog.com.mylibapp2 I/OSAï¹ Hoge:[Hoge{id=1, name='osa'}]
ã§ã¾ããã
$ ls mylibrary/build/outputs/aar/
mylibrary-debug.aar
mylibrary-release.aar
ä»åº¦ã¯ãªãªã¼ã¹/ãããã°ã®ä¸¡æ¹ã®aarãã¡ã¤ã«ãçæãããããã«ãªã£ãããããã¢ããªå´ã®ãã«ãã¿ã¤ãã«å¿ãã¦ä½¿ãåãã¦ãã£ã¦ããæãã§ããªãä»åã¯é¢åãããã®ã§è©¦ãã¦ãªããã©ãã¢ããªã±ã¼ã·ã§ã³ã¢ã¸ã¥ã¼ã«ããªãªã¼ã¹ã§ãã«ãããã°ã©ã¤ãã©ãªã®ãããã°ãã°ã¯åºåãããªããã¯ãã
Android Studioã§ã¯ããã ç°¡åAndroidã¢ããªéçº
- ä½è : æå±±åäº
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2014/11/21
- ã¡ãã£ã¢: 大åæ¬
- ãã®ååãå«ãããã°ãè¦ã
*1:ä½æããã©ã¤ãã©ãªã«ããã®ãããããªããã©ãç»é¢ç¡ãã®ã©ã¤ãã©ãªãªããããé¸æããã°åé¡ãªãã¯ã